Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 9671b44

Browse files
Add tests for packed borrows in unsafe fns
1 parent 925d5ac commit 9671b44

File tree

4 files changed

+140
-82
lines changed

4 files changed

+140
-82
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#![feature(unsafe_block_in_unsafe_fn)]
2+
3+
#[repr(packed)]
4+
pub struct Packed {
5+
data: &'static u32,
6+
}
7+
8+
const PACKED: Packed = Packed { data: &0 };
9+
10+
#[allow(safe_packed_borrows)]
11+
#[allow(unsafe_op_in_unsafe_fn)]
12+
unsafe fn allow_allow() {
13+
&PACKED.data; // allowed
14+
}
15+
16+
#[allow(safe_packed_borrows)]
17+
#[warn(unsafe_op_in_unsafe_fn)]
18+
unsafe fn allow_warn() {
19+
&PACKED.data; // allowed
20+
}
21+
22+
#[allow(safe_packed_borrows)]
23+
#[deny(unsafe_op_in_unsafe_fn)]
24+
unsafe fn allow_deny() {
25+
&PACKED.data; // allowed
26+
}
27+
28+
#[warn(safe_packed_borrows)]
29+
#[allow(unsafe_op_in_unsafe_fn)]
30+
unsafe fn warn_allow() {
31+
&PACKED.data; // allowed
32+
}
33+
34+
#[warn(safe_packed_borrows)]
35+
#[warn(unsafe_op_in_unsafe_fn)]
36+
unsafe fn warn_warn() {
37+
&PACKED.data; //~ WARN
38+
//~| WARNING this was previously accepted by the compiler but is being phased out
39+
}
40+
41+
#[warn(safe_packed_borrows)]
42+
#[deny(unsafe_op_in_unsafe_fn)]
43+
unsafe fn warn_deny() {
44+
&PACKED.data; //~ WARN
45+
//~| WARNING this was previously accepted by the compiler but is being phased out
46+
}
47+
48+
#[deny(safe_packed_borrows)]
49+
#[allow(unsafe_op_in_unsafe_fn)]
50+
unsafe fn deny_allow() {
51+
&PACKED.data; // allowed
52+
}
53+
54+
#[deny(safe_packed_borrows)]
55+
#[warn(unsafe_op_in_unsafe_fn)]
56+
unsafe fn deny_warn() {
57+
&PACKED.data; //~ WARN
58+
}
59+
60+
#[deny(safe_packed_borrows)]
61+
#[deny(unsafe_op_in_unsafe_fn)]
62+
unsafe fn deny_deny() {
63+
&PACKED.data; //~ ERROR
64+
//~| WARNING this was previously accepted by the compiler but is being phased out
65+
}
66+
67+
fn main() {}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
warning: borrow of packed field is unsafe and requires unsafe block (error E0133)
2+
--> $DIR/rfc-2585-safe_packed_borrows-in-unsafe-fn.rs:37:5
3+
|
4+
LL | &PACKED.data;
5+
| ^^^^^^^^^^^^ borrow of packed field
6+
|
7+
note: the lint level is defined here
8+
--> $DIR/rfc-2585-safe_packed_borrows-in-unsafe-fn.rs:34:8
9+
|
10+
LL | #[warn(safe_packed_borrows)]
11+
| ^^^^^^^^^^^^^^^^^^^
12+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
13+
= note: for more information, see issue #46043 <https://github.com/rust-lang/rust/issues/46043>
14+
= note: fields of packed structs might be misaligned: dereferencing a misaligned pointer or even just creating a misaligned reference is undefined behavior
15+
16+
warning: borrow of packed field is unsafe and requires unsafe block (error E0133)
17+
--> $DIR/rfc-2585-safe_packed_borrows-in-unsafe-fn.rs:44:5
18+
|
19+
LL | &PACKED.data;
20+
| ^^^^^^^^^^^^ borrow of packed field
21+
|
22+
note: the lint level is defined here
23+
--> $DIR/rfc-2585-safe_packed_borrows-in-unsafe-fn.rs:41:8
24+
|
25+
LL | #[warn(safe_packed_borrows)]
26+
| ^^^^^^^^^^^^^^^^^^^
27+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
28+
= note: for more information, see issue #46043 <https://github.com/rust-lang/rust/issues/46043>
29+
= note: fields of packed structs might be misaligned: dereferencing a misaligned pointer or even just creating a misaligned reference is undefined behavior
30+
31+
warning: borrow of packed field is unsafe and requires unsafe block (error E0133)
32+
--> $DIR/rfc-2585-safe_packed_borrows-in-unsafe-fn.rs:57:5
33+
|
34+
LL | &PACKED.data;
35+
| ^^^^^^^^^^^^ borrow of packed field
36+
|
37+
note: the lint level is defined here
38+
--> $DIR/rfc-2585-safe_packed_borrows-in-unsafe-fn.rs:55:8
39+
|
40+
LL | #[warn(unsafe_op_in_unsafe_fn)]
41+
| ^^^^^^^^^^^^^^^^^^^^^^
42+
= note: fields of packed structs might be misaligned: dereferencing a misaligned pointer or even just creating a misaligned reference is undefined behavior
43+
44+
error: borrow of packed field is unsafe and requires unsafe block (error E0133)
45+
--> $DIR/rfc-2585-safe_packed_borrows-in-unsafe-fn.rs:63:5
46+
|
47+
LL | &PACKED.data;
48+
| ^^^^^^^^^^^^ borrow of packed field
49+
|
50+
note: the lint level is defined here
51+
--> $DIR/rfc-2585-safe_packed_borrows-in-unsafe-fn.rs:60:8
52+
|
53+
LL | #[deny(safe_packed_borrows)]
54+
| ^^^^^^^^^^^^^^^^^^^
55+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
56+
= note: for more information, see issue #46043 <https://github.com/rust-lang/rust/issues/46043>
57+
= note: fields of packed structs might be misaligned: dereferencing a misaligned pointer or even just creating a misaligned reference is undefined behavior
58+
59+
error: aborting due to previous error; 3 warnings emitted
60+

src/test/ui/unsafe/rfc-2585-unsafe_op_in_unsafe_fn.rs

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,18 @@
11
#![feature(unsafe_block_in_unsafe_fn)]
22
#![deny(unsafe_op_in_unsafe_fn)]
33
#![deny(unused_unsafe)]
4-
#![deny(safe_packed_borrows)]
54

65
unsafe fn unsf() {}
76
const PTR: *const () = std::ptr::null();
87
static mut VOID: () = ();
98

10-
#[repr(packed)]
11-
pub struct Packed {
12-
data: &'static u32,
13-
}
14-
15-
const PACKED: Packed = Packed { data: &0 };
16-
179
unsafe fn deny_level() {
1810
unsf();
1911
//~^ ERROR call to unsafe function is unsafe and requires unsafe block
2012
*PTR;
2113
//~^ ERROR dereference of raw pointer is unsafe and requires unsafe block
2214
VOID = ();
2315
//~^ ERROR use of mutable static is unsafe and requires unsafe block
24-
&PACKED.data;
25-
//~^ ERROR borrow of packed field is unsafe and requires unsafe block
26-
//~| WARNING this was previously accepted by the compiler but is being phased out
2716
}
2817

2918
// Check that `unsafe_op_in_unsafe_fn` works starting from the `warn` level.
@@ -36,9 +25,6 @@ unsafe fn warning_level() {
3625
//~^ ERROR dereference of raw pointer is unsafe and requires unsafe block
3726
VOID = ();
3827
//~^ ERROR use of mutable static is unsafe and requires unsafe block
39-
&PACKED.data;
40-
//~^ ERROR borrow of packed field is unsafe and requires unsafe block
41-
//~| WARNING this was previously accepted by the compiler but is being phased out
4228
}
4329

4430
unsafe fn explicit_block() {
@@ -47,7 +33,6 @@ unsafe fn explicit_block() {
4733
unsf();
4834
*PTR;
4935
VOID = ();
50-
&PACKED.data;
5136
}
5237
}
5338

@@ -56,25 +41,12 @@ unsafe fn two_explicit_blocks() {
5641
//~^ ERROR unnecessary `unsafe` block
5742
}
5843

59-
#[warn(safe_packed_borrows)]
60-
unsafe fn warn_packed_borrows() {
61-
&PACKED.data;
62-
//~^ WARNING borrow of packed field is unsafe and requires unsafe block
63-
//~| WARNING this was previously accepted by the compiler but is being phased out
64-
}
65-
66-
#[allow(safe_packed_borrows)]
67-
unsafe fn allow_packed_borrows() {
68-
&PACKED.data; // `safe_packed_borrows` is allowed, no error
69-
}
70-
7144
#[allow(unsafe_op_in_unsafe_fn)]
7245
unsafe fn allow_level() {
7346
// lint allowed -> no error
7447
unsf();
7548
*PTR;
7649
VOID = ();
77-
&PACKED.data;
7850

7951
unsafe { unsf() }
8052
//~^ ERROR unnecessary `unsafe` block
@@ -87,7 +59,6 @@ unsafe fn nested_allow_level() {
8759
unsf();
8860
*PTR;
8961
VOID = ();
90-
&PACKED.data;
9162

9263
unsafe { unsf() }
9364
//~^ ERROR unnecessary `unsafe` block
Lines changed: 13 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: call to unsafe function is unsafe and requires unsafe block (error E0133)
2-
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:18:5
2+
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:10:5
33
|
44
LL | unsf();
55
| ^^^^^^ call to unsafe function
@@ -12,78 +12,53 @@ LL | #![deny(unsafe_op_in_unsafe_fn)]
1212
= note: consult the function's documentation for information on how to avoid undefined behavior
1313

1414
error: dereference of raw pointer is unsafe and requires unsafe block (error E0133)
15-
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:20:5
15+
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:12:5
1616
|
1717
LL | *PTR;
1818
| ^^^^ dereference of raw pointer
1919
|
2020
= note: raw pointers may be NULL, dangling or unaligned; they can violate aliasing rules and cause data races: all of these are undefined behavior
2121

2222
error: use of mutable static is unsafe and requires unsafe block (error E0133)
23-
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:22:5
23+
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:14:5
2424
|
2525
LL | VOID = ();
2626
| ^^^^^^^^^ use of mutable static
2727
|
2828
= note: mutable statics can be mutated by multiple threads: aliasing violations or data races will cause undefined behavior
2929

30-
error: borrow of packed field is unsafe and requires unsafe block (error E0133)
31-
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:24:5
32-
|
33-
LL | &PACKED.data;
34-
| ^^^^^^^^^^^^ borrow of packed field
35-
|
36-
note: the lint level is defined here
37-
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:4:9
38-
|
39-
LL | #![deny(safe_packed_borrows)]
40-
| ^^^^^^^^^^^^^^^^^^^
41-
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
42-
= note: for more information, see issue #46043 <https://github.com/rust-lang/rust/issues/46043>
43-
= note: fields of packed structs might be misaligned: dereferencing a misaligned pointer or even just creating a misaligned reference is undefined behavior
44-
4530
error: call to unsafe function is unsafe and requires unsafe block (error E0133)
46-
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:33:5
31+
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:22:5
4732
|
4833
LL | unsf();
4934
| ^^^^^^ call to unsafe function
5035
|
5136
note: the lint level is defined here
52-
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:31:8
37+
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:20:8
5338
|
5439
LL | #[deny(warnings)]
5540
| ^^^^^^^^
5641
= note: `#[deny(unsafe_op_in_unsafe_fn)]` implied by `#[deny(warnings)]`
5742
= note: consult the function's documentation for information on how to avoid undefined behavior
5843

5944
error: dereference of raw pointer is unsafe and requires unsafe block (error E0133)
60-
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:35:5
45+
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:24:5
6146
|
6247
LL | *PTR;
6348
| ^^^^ dereference of raw pointer
6449
|
6550
= note: raw pointers may be NULL, dangling or unaligned; they can violate aliasing rules and cause data races: all of these are undefined behavior
6651

6752
error: use of mutable static is unsafe and requires unsafe block (error E0133)
68-
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:37:5
53+
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:26:5
6954
|
7055
LL | VOID = ();
7156
| ^^^^^^^^^ use of mutable static
7257
|
7358
= note: mutable statics can be mutated by multiple threads: aliasing violations or data races will cause undefined behavior
7459

75-
error: borrow of packed field is unsafe and requires unsafe block (error E0133)
76-
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:39:5
77-
|
78-
LL | &PACKED.data;
79-
| ^^^^^^^^^^^^ borrow of packed field
80-
|
81-
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
82-
= note: for more information, see issue #46043 <https://github.com/rust-lang/rust/issues/46043>
83-
= note: fields of packed structs might be misaligned: dereferencing a misaligned pointer or even just creating a misaligned reference is undefined behavior
84-
8560
error: unnecessary `unsafe` block
86-
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:55:14
61+
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:40:14
8762
|
8863
LL | unsafe { unsafe { unsf() } }
8964
| ------ ^^^^^^ unnecessary `unsafe` block
@@ -96,49 +71,34 @@ note: the lint level is defined here
9671
LL | #![deny(unused_unsafe)]
9772
| ^^^^^^^^^^^^^
9873

99-
warning: borrow of packed field is unsafe and requires unsafe block (error E0133)
100-
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:61:5
101-
|
102-
LL | &PACKED.data;
103-
| ^^^^^^^^^^^^ borrow of packed field
104-
|
105-
note: the lint level is defined here
106-
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:59:8
107-
|
108-
LL | #[warn(safe_packed_borrows)]
109-
| ^^^^^^^^^^^^^^^^^^^
110-
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
111-
= note: for more information, see issue #46043 <https://github.com/rust-lang/rust/issues/46043>
112-
= note: fields of packed structs might be misaligned: dereferencing a misaligned pointer or even just creating a misaligned reference is undefined behavior
113-
11474
error: unnecessary `unsafe` block
115-
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:79:5
75+
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:51:5
11676
|
11777
LL | unsafe { unsf() }
11878
| ^^^^^^ unnecessary `unsafe` block
11979

12080
error: unnecessary `unsafe` block
121-
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:92:9
81+
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:63:9
12282
|
12383
LL | unsafe { unsf() }
12484
| ^^^^^^ unnecessary `unsafe` block
12585

12686
error[E0133]: call to unsafe function is unsafe and requires unsafe block
127-
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:98:5
87+
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:69:5
12888
|
12989
LL | unsf();
13090
| ^^^^^^ call to unsafe function
13191
|
13292
= note: consult the function's documentation for information on how to avoid undefined behavior
13393

13494
error[E0133]: call to unsafe function is unsafe and requires unsafe function or block
135-
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:102:9
95+
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:73:9
13696
|
13797
LL | unsf();
13898
| ^^^^^^ call to unsafe function
13999
|
140100
= note: consult the function's documentation for information on how to avoid undefined behavior
141101

142-
error: aborting due to 13 previous errors; 1 warning emitted
102+
error: aborting due to 11 previous errors
143103

144104
For more information about this error, try `rustc --explain E0133`.

0 commit comments

Comments
 (0)