Add tests for packed borrows in unsafe fns
This commit is contained in:
parent
925d5ac45f
commit
9671b44609
4 changed files with 140 additions and 82 deletions
|
|
@ -0,0 +1,67 @@
|
|||
#![feature(unsafe_block_in_unsafe_fn)]
|
||||
|
||||
#[repr(packed)]
|
||||
pub struct Packed {
|
||||
data: &'static u32,
|
||||
}
|
||||
|
||||
const PACKED: Packed = Packed { data: &0 };
|
||||
|
||||
#[allow(safe_packed_borrows)]
|
||||
#[allow(unsafe_op_in_unsafe_fn)]
|
||||
unsafe fn allow_allow() {
|
||||
&PACKED.data; // allowed
|
||||
}
|
||||
|
||||
#[allow(safe_packed_borrows)]
|
||||
#[warn(unsafe_op_in_unsafe_fn)]
|
||||
unsafe fn allow_warn() {
|
||||
&PACKED.data; // allowed
|
||||
}
|
||||
|
||||
#[allow(safe_packed_borrows)]
|
||||
#[deny(unsafe_op_in_unsafe_fn)]
|
||||
unsafe fn allow_deny() {
|
||||
&PACKED.data; // allowed
|
||||
}
|
||||
|
||||
#[warn(safe_packed_borrows)]
|
||||
#[allow(unsafe_op_in_unsafe_fn)]
|
||||
unsafe fn warn_allow() {
|
||||
&PACKED.data; // allowed
|
||||
}
|
||||
|
||||
#[warn(safe_packed_borrows)]
|
||||
#[warn(unsafe_op_in_unsafe_fn)]
|
||||
unsafe fn warn_warn() {
|
||||
&PACKED.data; //~ WARN
|
||||
//~| WARNING this was previously accepted by the compiler but is being phased out
|
||||
}
|
||||
|
||||
#[warn(safe_packed_borrows)]
|
||||
#[deny(unsafe_op_in_unsafe_fn)]
|
||||
unsafe fn warn_deny() {
|
||||
&PACKED.data; //~ WARN
|
||||
//~| WARNING this was previously accepted by the compiler but is being phased out
|
||||
}
|
||||
|
||||
#[deny(safe_packed_borrows)]
|
||||
#[allow(unsafe_op_in_unsafe_fn)]
|
||||
unsafe fn deny_allow() {
|
||||
&PACKED.data; // allowed
|
||||
}
|
||||
|
||||
#[deny(safe_packed_borrows)]
|
||||
#[warn(unsafe_op_in_unsafe_fn)]
|
||||
unsafe fn deny_warn() {
|
||||
&PACKED.data; //~ WARN
|
||||
}
|
||||
|
||||
#[deny(safe_packed_borrows)]
|
||||
#[deny(unsafe_op_in_unsafe_fn)]
|
||||
unsafe fn deny_deny() {
|
||||
&PACKED.data; //~ ERROR
|
||||
//~| WARNING this was previously accepted by the compiler but is being phased out
|
||||
}
|
||||
|
||||
fn main() {}
|
||||
|
|
@ -0,0 +1,60 @@
|
|||
warning: borrow of packed field is unsafe and requires unsafe block (error E0133)
|
||||
--> $DIR/rfc-2585-safe_packed_borrows-in-unsafe-fn.rs:37:5
|
||||
|
|
||||
LL | &PACKED.data;
|
||||
| ^^^^^^^^^^^^ borrow of packed field
|
||||
|
|
||||
note: the lint level is defined here
|
||||
--> $DIR/rfc-2585-safe_packed_borrows-in-unsafe-fn.rs:34:8
|
||||
|
|
||||
LL | #[warn(safe_packed_borrows)]
|
||||
| ^^^^^^^^^^^^^^^^^^^
|
||||
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
|
||||
= note: for more information, see issue #46043 <https://github.com/rust-lang/rust/issues/46043>
|
||||
= note: fields of packed structs might be misaligned: dereferencing a misaligned pointer or even just creating a misaligned reference is undefined behavior
|
||||
|
||||
warning: borrow of packed field is unsafe and requires unsafe block (error E0133)
|
||||
--> $DIR/rfc-2585-safe_packed_borrows-in-unsafe-fn.rs:44:5
|
||||
|
|
||||
LL | &PACKED.data;
|
||||
| ^^^^^^^^^^^^ borrow of packed field
|
||||
|
|
||||
note: the lint level is defined here
|
||||
--> $DIR/rfc-2585-safe_packed_borrows-in-unsafe-fn.rs:41:8
|
||||
|
|
||||
LL | #[warn(safe_packed_borrows)]
|
||||
| ^^^^^^^^^^^^^^^^^^^
|
||||
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
|
||||
= note: for more information, see issue #46043 <https://github.com/rust-lang/rust/issues/46043>
|
||||
= note: fields of packed structs might be misaligned: dereferencing a misaligned pointer or even just creating a misaligned reference is undefined behavior
|
||||
|
||||
warning: borrow of packed field is unsafe and requires unsafe block (error E0133)
|
||||
--> $DIR/rfc-2585-safe_packed_borrows-in-unsafe-fn.rs:57:5
|
||||
|
|
||||
LL | &PACKED.data;
|
||||
| ^^^^^^^^^^^^ borrow of packed field
|
||||
|
|
||||
note: the lint level is defined here
|
||||
--> $DIR/rfc-2585-safe_packed_borrows-in-unsafe-fn.rs:55:8
|
||||
|
|
||||
LL | #[warn(unsafe_op_in_unsafe_fn)]
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^
|
||||
= note: fields of packed structs might be misaligned: dereferencing a misaligned pointer or even just creating a misaligned reference is undefined behavior
|
||||
|
||||
error: borrow of packed field is unsafe and requires unsafe block (error E0133)
|
||||
--> $DIR/rfc-2585-safe_packed_borrows-in-unsafe-fn.rs:63:5
|
||||
|
|
||||
LL | &PACKED.data;
|
||||
| ^^^^^^^^^^^^ borrow of packed field
|
||||
|
|
||||
note: the lint level is defined here
|
||||
--> $DIR/rfc-2585-safe_packed_borrows-in-unsafe-fn.rs:60:8
|
||||
|
|
||||
LL | #[deny(safe_packed_borrows)]
|
||||
| ^^^^^^^^^^^^^^^^^^^
|
||||
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
|
||||
= note: for more information, see issue #46043 <https://github.com/rust-lang/rust/issues/46043>
|
||||
= note: fields of packed structs might be misaligned: dereferencing a misaligned pointer or even just creating a misaligned reference is undefined behavior
|
||||
|
||||
error: aborting due to previous error; 3 warnings emitted
|
||||
|
||||
|
|
@ -1,19 +1,11 @@
|
|||
#![feature(unsafe_block_in_unsafe_fn)]
|
||||
#![deny(unsafe_op_in_unsafe_fn)]
|
||||
#![deny(unused_unsafe)]
|
||||
#![deny(safe_packed_borrows)]
|
||||
|
||||
unsafe fn unsf() {}
|
||||
const PTR: *const () = std::ptr::null();
|
||||
static mut VOID: () = ();
|
||||
|
||||
#[repr(packed)]
|
||||
pub struct Packed {
|
||||
data: &'static u32,
|
||||
}
|
||||
|
||||
const PACKED: Packed = Packed { data: &0 };
|
||||
|
||||
unsafe fn deny_level() {
|
||||
unsf();
|
||||
//~^ ERROR call to unsafe function is unsafe and requires unsafe block
|
||||
|
|
@ -21,9 +13,6 @@ unsafe fn deny_level() {
|
|||
//~^ ERROR dereference of raw pointer is unsafe and requires unsafe block
|
||||
VOID = ();
|
||||
//~^ ERROR use of mutable static is unsafe and requires unsafe block
|
||||
&PACKED.data;
|
||||
//~^ ERROR borrow of packed field is unsafe and requires unsafe block
|
||||
//~| WARNING this was previously accepted by the compiler but is being phased out
|
||||
}
|
||||
|
||||
// Check that `unsafe_op_in_unsafe_fn` works starting from the `warn` level.
|
||||
|
|
@ -36,9 +25,6 @@ unsafe fn warning_level() {
|
|||
//~^ ERROR dereference of raw pointer is unsafe and requires unsafe block
|
||||
VOID = ();
|
||||
//~^ ERROR use of mutable static is unsafe and requires unsafe block
|
||||
&PACKED.data;
|
||||
//~^ ERROR borrow of packed field is unsafe and requires unsafe block
|
||||
//~| WARNING this was previously accepted by the compiler but is being phased out
|
||||
}
|
||||
|
||||
unsafe fn explicit_block() {
|
||||
|
|
@ -47,7 +33,6 @@ unsafe fn explicit_block() {
|
|||
unsf();
|
||||
*PTR;
|
||||
VOID = ();
|
||||
&PACKED.data;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -56,25 +41,12 @@ unsafe fn two_explicit_blocks() {
|
|||
//~^ ERROR unnecessary `unsafe` block
|
||||
}
|
||||
|
||||
#[warn(safe_packed_borrows)]
|
||||
unsafe fn warn_packed_borrows() {
|
||||
&PACKED.data;
|
||||
//~^ WARNING borrow of packed field is unsafe and requires unsafe block
|
||||
//~| WARNING this was previously accepted by the compiler but is being phased out
|
||||
}
|
||||
|
||||
#[allow(safe_packed_borrows)]
|
||||
unsafe fn allow_packed_borrows() {
|
||||
&PACKED.data; // `safe_packed_borrows` is allowed, no error
|
||||
}
|
||||
|
||||
#[allow(unsafe_op_in_unsafe_fn)]
|
||||
unsafe fn allow_level() {
|
||||
// lint allowed -> no error
|
||||
unsf();
|
||||
*PTR;
|
||||
VOID = ();
|
||||
&PACKED.data;
|
||||
|
||||
unsafe { unsf() }
|
||||
//~^ ERROR unnecessary `unsafe` block
|
||||
|
|
@ -87,7 +59,6 @@ unsafe fn nested_allow_level() {
|
|||
unsf();
|
||||
*PTR;
|
||||
VOID = ();
|
||||
&PACKED.data;
|
||||
|
||||
unsafe { unsf() }
|
||||
//~^ ERROR unnecessary `unsafe` block
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
error: call to unsafe function is unsafe and requires unsafe block (error E0133)
|
||||
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:18:5
|
||||
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:10:5
|
||||
|
|
||||
LL | unsf();
|
||||
| ^^^^^^ call to unsafe function
|
||||
|
|
@ -12,7 +12,7 @@ LL | #![deny(unsafe_op_in_unsafe_fn)]
|
|||
= note: consult the function's documentation for information on how to avoid undefined behavior
|
||||
|
||||
error: dereference of raw pointer is unsafe and requires unsafe block (error E0133)
|
||||
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:20:5
|
||||
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:12:5
|
||||
|
|
||||
LL | *PTR;
|
||||
| ^^^^ dereference of raw pointer
|
||||
|
|
@ -20,36 +20,21 @@ LL | *PTR;
|
|||
= note: raw pointers may be NULL, dangling or unaligned; they can violate aliasing rules and cause data races: all of these are undefined behavior
|
||||
|
||||
error: use of mutable static is unsafe and requires unsafe block (error E0133)
|
||||
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:22:5
|
||||
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:14:5
|
||||
|
|
||||
LL | VOID = ();
|
||||
| ^^^^^^^^^ use of mutable static
|
||||
|
|
||||
= note: mutable statics can be mutated by multiple threads: aliasing violations or data races will cause undefined behavior
|
||||
|
||||
error: borrow of packed field is unsafe and requires unsafe block (error E0133)
|
||||
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:24:5
|
||||
|
|
||||
LL | &PACKED.data;
|
||||
| ^^^^^^^^^^^^ borrow of packed field
|
||||
|
|
||||
note: the lint level is defined here
|
||||
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:4:9
|
||||
|
|
||||
LL | #![deny(safe_packed_borrows)]
|
||||
| ^^^^^^^^^^^^^^^^^^^
|
||||
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
|
||||
= note: for more information, see issue #46043 <https://github.com/rust-lang/rust/issues/46043>
|
||||
= note: fields of packed structs might be misaligned: dereferencing a misaligned pointer or even just creating a misaligned reference is undefined behavior
|
||||
|
||||
error: call to unsafe function is unsafe and requires unsafe block (error E0133)
|
||||
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:33:5
|
||||
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:22:5
|
||||
|
|
||||
LL | unsf();
|
||||
| ^^^^^^ call to unsafe function
|
||||
|
|
||||
note: the lint level is defined here
|
||||
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:31:8
|
||||
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:20:8
|
||||
|
|
||||
LL | #[deny(warnings)]
|
||||
| ^^^^^^^^
|
||||
|
|
@ -57,7 +42,7 @@ LL | #[deny(warnings)]
|
|||
= note: consult the function's documentation for information on how to avoid undefined behavior
|
||||
|
||||
error: dereference of raw pointer is unsafe and requires unsafe block (error E0133)
|
||||
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:35:5
|
||||
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:24:5
|
||||
|
|
||||
LL | *PTR;
|
||||
| ^^^^ dereference of raw pointer
|
||||
|
|
@ -65,25 +50,15 @@ LL | *PTR;
|
|||
= note: raw pointers may be NULL, dangling or unaligned; they can violate aliasing rules and cause data races: all of these are undefined behavior
|
||||
|
||||
error: use of mutable static is unsafe and requires unsafe block (error E0133)
|
||||
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:37:5
|
||||
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:26:5
|
||||
|
|
||||
LL | VOID = ();
|
||||
| ^^^^^^^^^ use of mutable static
|
||||
|
|
||||
= note: mutable statics can be mutated by multiple threads: aliasing violations or data races will cause undefined behavior
|
||||
|
||||
error: borrow of packed field is unsafe and requires unsafe block (error E0133)
|
||||
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:39:5
|
||||
|
|
||||
LL | &PACKED.data;
|
||||
| ^^^^^^^^^^^^ borrow of packed field
|
||||
|
|
||||
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
|
||||
= note: for more information, see issue #46043 <https://github.com/rust-lang/rust/issues/46043>
|
||||
= note: fields of packed structs might be misaligned: dereferencing a misaligned pointer or even just creating a misaligned reference is undefined behavior
|
||||
|
||||
error: unnecessary `unsafe` block
|
||||
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:55:14
|
||||
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:40:14
|
||||
|
|
||||
LL | unsafe { unsafe { unsf() } }
|
||||
| ------ ^^^^^^ unnecessary `unsafe` block
|
||||
|
|
@ -96,35 +71,20 @@ note: the lint level is defined here
|
|||
LL | #![deny(unused_unsafe)]
|
||||
| ^^^^^^^^^^^^^
|
||||
|
||||
warning: borrow of packed field is unsafe and requires unsafe block (error E0133)
|
||||
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:61:5
|
||||
|
|
||||
LL | &PACKED.data;
|
||||
| ^^^^^^^^^^^^ borrow of packed field
|
||||
|
|
||||
note: the lint level is defined here
|
||||
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:59:8
|
||||
|
|
||||
LL | #[warn(safe_packed_borrows)]
|
||||
| ^^^^^^^^^^^^^^^^^^^
|
||||
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
|
||||
= note: for more information, see issue #46043 <https://github.com/rust-lang/rust/issues/46043>
|
||||
= note: fields of packed structs might be misaligned: dereferencing a misaligned pointer or even just creating a misaligned reference is undefined behavior
|
||||
|
||||
error: unnecessary `unsafe` block
|
||||
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:79:5
|
||||
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:51:5
|
||||
|
|
||||
LL | unsafe { unsf() }
|
||||
| ^^^^^^ unnecessary `unsafe` block
|
||||
|
||||
error: unnecessary `unsafe` block
|
||||
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:92:9
|
||||
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:63:9
|
||||
|
|
||||
LL | unsafe { unsf() }
|
||||
| ^^^^^^ unnecessary `unsafe` block
|
||||
|
||||
error[E0133]: call to unsafe function is unsafe and requires unsafe block
|
||||
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:98:5
|
||||
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:69:5
|
||||
|
|
||||
LL | unsf();
|
||||
| ^^^^^^ call to unsafe function
|
||||
|
|
@ -132,13 +92,13 @@ LL | unsf();
|
|||
= note: consult the function's documentation for information on how to avoid undefined behavior
|
||||
|
||||
error[E0133]: call to unsafe function is unsafe and requires unsafe function or block
|
||||
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:102:9
|
||||
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:73:9
|
||||
|
|
||||
LL | unsf();
|
||||
| ^^^^^^ call to unsafe function
|
||||
|
|
||||
= note: consult the function's documentation for information on how to avoid undefined behavior
|
||||
|
||||
error: aborting due to 13 previous errors; 1 warning emitted
|
||||
error: aborting due to 11 previous errors
|
||||
|
||||
For more information about this error, try `rustc --explain E0133`.
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue