Skip to content

Commit 49a31cd

Browse files
authored
Rollup merge of #95372 - RalfJung:unaligned_references, r=oli-obk
make unaligned_references lint deny-by-default This lint has been warn-by-default for a year now (since #82525), so I think it is time to crank it up a bit. Code that triggers the lint causes UB (without `unsafe`) when executed, so we really don't want people to write code like this.
2 parents 6dd92bf + 1a6c2ff commit 49a31cd

17 files changed

+490
-41
lines changed

Diff for: compiler/rustc_data_structures/src/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
#![feature(thread_id_value)]
2828
#![feature(vec_into_raw_parts)]
2929
#![allow(rustc::default_hash_types)]
30-
#![deny(unaligned_references)]
3130
#![allow(rustc::potential_query_instability)]
3231

3332
#[macro_use]

Diff for: compiler/rustc_lint_defs/src/builtin.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -1110,8 +1110,6 @@ declare_lint! {
11101110
/// ### Example
11111111
///
11121112
/// ```rust,compile_fail
1113-
/// #![deny(unaligned_references)]
1114-
///
11151113
/// #[repr(packed)]
11161114
/// pub struct Foo {
11171115
/// field1: u64,
@@ -1139,10 +1137,11 @@ declare_lint! {
11391137
/// [undefined behavior]: https://doc.rust-lang.org/reference/behavior-considered-undefined.html
11401138
/// [issue #82523]: https://github.com/rust-lang/rust/issues/82523
11411139
pub UNALIGNED_REFERENCES,
1142-
Warn,
1140+
Deny,
11431141
"detects unaligned references to fields of packed structs",
11441142
@future_incompatible = FutureIncompatibleInfo {
11451143
reference: "issue #82523 <https://github.com/rust-lang/rust/issues/82523>",
1144+
reason: FutureIncompatibilityReason::FutureReleaseErrorReportNow,
11461145
};
11471146
report_in_external_macro
11481147
}

Diff for: src/test/ui/binding/issue-53114-safety-checks.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@ fn let_wild_gets_unsafe_field() {
2020
let u1 = U { a: I(0) };
2121
let u2 = U { a: I(1) };
2222
let p = P { a: &2, b: &3 };
23-
let _ = &p.b; //~ WARN reference to packed field
23+
let _ = &p.b; //~ ERROR reference to packed field
2424
//~^ WARN will become a hard error
2525
let _ = u1.a; // #53114: should eventually signal error as well
2626
let _ = &u2.a; //~ ERROR [E0133]
2727

2828
// variation on above with `_` in substructure
29-
let (_,) = (&p.b,); //~ WARN reference to packed field
29+
let (_,) = (&p.b,); //~ ERROR reference to packed field
3030
//~^ WARN will become a hard error
3131
let (_,) = (u1.a,); //~ ERROR [E0133]
3232
let (_,) = (&u2.a,); //~ ERROR [E0133]
@@ -36,13 +36,13 @@ fn match_unsafe_field_to_wild() {
3636
let u1 = U { a: I(0) };
3737
let u2 = U { a: I(1) };
3838
let p = P { a: &2, b: &3 };
39-
match &p.b { _ => { } } //~ WARN reference to packed field
39+
match &p.b { _ => { } } //~ ERROR reference to packed field
4040
//~^ WARN will become a hard error
4141
match u1.a { _ => { } } //~ ERROR [E0133]
4242
match &u2.a { _ => { } } //~ ERROR [E0133]
4343

4444
// variation on above with `_` in substructure
45-
match (&p.b,) { (_,) => { } } //~ WARN reference to packed field
45+
match (&p.b,) { (_,) => { } } //~ ERROR reference to packed field
4646
//~^ WARN will become a hard error
4747
match (u1.a,) { (_,) => { } } //~ ERROR [E0133]
4848
match (&u2.a,) { (_,) => { } } //~ ERROR [E0133]

Diff for: src/test/ui/binding/issue-53114-safety-checks.stderr

+58-6
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
warning: reference to packed field is unaligned
1+
error: reference to packed field is unaligned
22
--> $DIR/issue-53114-safety-checks.rs:23:13
33
|
44
LL | let _ = &p.b;
55
| ^^^^
66
|
7-
= note: `#[warn(unaligned_references)]` on by default
7+
= note: `#[deny(unaligned_references)]` on by default
88
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
99
= note: for more information, see issue #82523 <https://github.com/rust-lang/rust/issues/82523>
1010
= note: fields of packed structs are not properly aligned, and creating a misaligned reference is undefined behavior (even if that reference is never dereferenced)
1111
= help: copy the field contents to a local variable, or replace the reference with a raw pointer and use `read_unaligned`/`write_unaligned` (loads and stores via `*p` must be properly aligned even when using raw pointers)
1212

13-
warning: reference to packed field is unaligned
13+
error: reference to packed field is unaligned
1414
--> $DIR/issue-53114-safety-checks.rs:29:17
1515
|
1616
LL | let (_,) = (&p.b,);
@@ -21,7 +21,7 @@ LL | let (_,) = (&p.b,);
2121
= note: fields of packed structs are not properly aligned, and creating a misaligned reference is undefined behavior (even if that reference is never dereferenced)
2222
= help: copy the field contents to a local variable, or replace the reference with a raw pointer and use `read_unaligned`/`write_unaligned` (loads and stores via `*p` must be properly aligned even when using raw pointers)
2323

24-
warning: reference to packed field is unaligned
24+
error: reference to packed field is unaligned
2525
--> $DIR/issue-53114-safety-checks.rs:39:11
2626
|
2727
LL | match &p.b { _ => { } }
@@ -32,7 +32,7 @@ LL | match &p.b { _ => { } }
3232
= note: fields of packed structs are not properly aligned, and creating a misaligned reference is undefined behavior (even if that reference is never dereferenced)
3333
= help: copy the field contents to a local variable, or replace the reference with a raw pointer and use `read_unaligned`/`write_unaligned` (loads and stores via `*p` must be properly aligned even when using raw pointers)
3434

35-
warning: reference to packed field is unaligned
35+
error: reference to packed field is unaligned
3636
--> $DIR/issue-53114-safety-checks.rs:45:12
3737
|
3838
LL | match (&p.b,) { (_,) => { } }
@@ -99,6 +99,58 @@ LL | match (&u2.a,) { (_,) => { } }
9999
|
100100
= note: the field may not be properly initialized: using uninitialized data will cause undefined behavior
101101

102-
error: aborting due to 7 previous errors; 4 warnings emitted
102+
error: aborting due to 11 previous errors
103103

104104
For more information about this error, try `rustc --explain E0133`.
105+
Future incompatibility report: Future breakage diagnostic:
106+
error: reference to packed field is unaligned
107+
--> $DIR/issue-53114-safety-checks.rs:23:13
108+
|
109+
LL | let _ = &p.b;
110+
| ^^^^
111+
|
112+
= note: `#[deny(unaligned_references)]` on by default
113+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
114+
= note: for more information, see issue #82523 <https://github.com/rust-lang/rust/issues/82523>
115+
= note: fields of packed structs are not properly aligned, and creating a misaligned reference is undefined behavior (even if that reference is never dereferenced)
116+
= help: copy the field contents to a local variable, or replace the reference with a raw pointer and use `read_unaligned`/`write_unaligned` (loads and stores via `*p` must be properly aligned even when using raw pointers)
117+
118+
Future breakage diagnostic:
119+
error: reference to packed field is unaligned
120+
--> $DIR/issue-53114-safety-checks.rs:29:17
121+
|
122+
LL | let (_,) = (&p.b,);
123+
| ^^^^
124+
|
125+
= note: `#[deny(unaligned_references)]` on by default
126+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
127+
= note: for more information, see issue #82523 <https://github.com/rust-lang/rust/issues/82523>
128+
= note: fields of packed structs are not properly aligned, and creating a misaligned reference is undefined behavior (even if that reference is never dereferenced)
129+
= help: copy the field contents to a local variable, or replace the reference with a raw pointer and use `read_unaligned`/`write_unaligned` (loads and stores via `*p` must be properly aligned even when using raw pointers)
130+
131+
Future breakage diagnostic:
132+
error: reference to packed field is unaligned
133+
--> $DIR/issue-53114-safety-checks.rs:39:11
134+
|
135+
LL | match &p.b { _ => { } }
136+
| ^^^^
137+
|
138+
= note: `#[deny(unaligned_references)]` on by default
139+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
140+
= note: for more information, see issue #82523 <https://github.com/rust-lang/rust/issues/82523>
141+
= note: fields of packed structs are not properly aligned, and creating a misaligned reference is undefined behavior (even if that reference is never dereferenced)
142+
= help: copy the field contents to a local variable, or replace the reference with a raw pointer and use `read_unaligned`/`write_unaligned` (loads and stores via `*p` must be properly aligned even when using raw pointers)
143+
144+
Future breakage diagnostic:
145+
error: reference to packed field is unaligned
146+
--> $DIR/issue-53114-safety-checks.rs:45:12
147+
|
148+
LL | match (&p.b,) { (_,) => { } }
149+
| ^^^^
150+
|
151+
= note: `#[deny(unaligned_references)]` on by default
152+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
153+
= note: for more information, see issue #82523 <https://github.com/rust-lang/rust/issues/82523>
154+
= note: fields of packed structs are not properly aligned, and creating a misaligned reference is undefined behavior (even if that reference is never dereferenced)
155+
= help: copy the field contents to a local variable, or replace the reference with a raw pointer and use `read_unaligned`/`write_unaligned` (loads and stores via `*p` must be properly aligned even when using raw pointers)
156+

Diff for: src/test/ui/closures/2229_closure_analysis/diagnostics/repr_packed.rs

+3-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
// edition:2021
22

3-
// check-pass
4-
53
// Given how the closure desugaring is implemented (at least at the time of writing this test),
64
// we don't need to truncate the captured path to a reference into a packed-struct if the field
75
// being referenced will be moved into the closure, since it's safe to move out a field from a
@@ -11,9 +9,8 @@
119
// inlined we will truncate the capture to access just the struct regardless of if the field
1210
// might get moved into the closure.
1311
//
14-
// It is possible for someone to try writing the code that relies on the desugaring to access a ref
15-
// into a packed-struct without explicity using unsafe. Here we test that the compiler warns the
16-
// user that such an access is still unsafe.
12+
// It is possible for someone to try writing the code that relies on the desugaring to create a ref
13+
// into a packed-struct. Here we test that the compiler still detects that case.
1714
fn test_missing_unsafe_warning_on_repr_packed() {
1815
#[repr(packed)]
1916
struct Foo { x: String }
@@ -22,7 +19,7 @@ fn test_missing_unsafe_warning_on_repr_packed() {
2219

2320
let c = || {
2421
println!("{}", foo.x);
25-
//~^ WARNING: reference to packed field is unaligned
22+
//~^ ERROR: reference to packed field is unaligned
2623
//~| WARNING: this was previously accepted by the compiler but is being phased out
2724
let _z = foo.x;
2825
};
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,29 @@
1-
warning: reference to packed field is unaligned
2-
--> $DIR/repr_packed.rs:24:24
1+
error: reference to packed field is unaligned
2+
--> $DIR/repr_packed.rs:21:24
33
|
44
LL | println!("{}", foo.x);
55
| ^^^^^
66
|
7-
= note: `#[warn(unaligned_references)]` on by default
7+
= note: `#[deny(unaligned_references)]` on by default
88
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
99
= note: for more information, see issue #82523 <https://github.com/rust-lang/rust/issues/82523>
1010
= note: fields of packed structs are not properly aligned, and creating a misaligned reference is undefined behavior (even if that reference is never dereferenced)
1111
= help: copy the field contents to a local variable, or replace the reference with a raw pointer and use `read_unaligned`/`write_unaligned` (loads and stores via `*p` must be properly aligned even when using raw pointers)
12-
= note: this warning originates in the macro `$crate::format_args_nl` (in Nightly builds, run with -Z macro-backtrace for more info)
12+
= note: this error originates in the macro `$crate::format_args_nl` (in Nightly builds, run with -Z macro-backtrace for more info)
1313

14-
warning: 1 warning emitted
14+
error: aborting due to previous error
15+
16+
Future incompatibility report: Future breakage diagnostic:
17+
error: reference to packed field is unaligned
18+
--> $DIR/repr_packed.rs:21:24
19+
|
20+
LL | println!("{}", foo.x);
21+
| ^^^^^
22+
|
23+
= note: `#[deny(unaligned_references)]` on by default
24+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
25+
= note: for more information, see issue #82523 <https://github.com/rust-lang/rust/issues/82523>
26+
= note: fields of packed structs are not properly aligned, and creating a misaligned reference is undefined behavior (even if that reference is never dereferenced)
27+
= help: copy the field contents to a local variable, or replace the reference with a raw pointer and use `read_unaligned`/`write_unaligned` (loads and stores via `*p` must be properly aligned even when using raw pointers)
28+
= note: this error originates in the macro `$crate::format_args_nl` (in Nightly builds, run with -Z macro-backtrace for more info)
1529

Diff for: src/test/ui/derives/deriving-with-repr-packed.stderr

+64
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,67 @@ LL | #[derive(PartialEq)]
4545

4646
error: aborting due to 4 previous errors
4747

48+
Future incompatibility report: Future breakage diagnostic:
49+
error: `#[derive]` can't be used on a `#[repr(packed)]` struct with type or const parameters (error E0133)
50+
--> $DIR/deriving-with-repr-packed.rs:8:16
51+
|
52+
LL | #[derive(Copy, Clone, PartialEq, Eq)]
53+
| ^^^^^
54+
|
55+
note: the lint level is defined here
56+
--> $DIR/deriving-with-repr-packed.rs:1:9
57+
|
58+
LL | #![deny(unaligned_references)]
59+
| ^^^^^^^^^^^^^^^^^^^^
60+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
61+
= note: for more information, see issue #82523 <https://github.com/rust-lang/rust/issues/82523>
62+
= note: this error originates in the derive macro `Clone` (in Nightly builds, run with -Z macro-backtrace for more info)
63+
64+
Future breakage diagnostic:
65+
error: `#[derive]` can't be used on a `#[repr(packed)]` struct with type or const parameters (error E0133)
66+
--> $DIR/deriving-with-repr-packed.rs:8:23
67+
|
68+
LL | #[derive(Copy, Clone, PartialEq, Eq)]
69+
| ^^^^^^^^^
70+
|
71+
note: the lint level is defined here
72+
--> $DIR/deriving-with-repr-packed.rs:1:9
73+
|
74+
LL | #![deny(unaligned_references)]
75+
| ^^^^^^^^^^^^^^^^^^^^
76+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
77+
= note: for more information, see issue #82523 <https://github.com/rust-lang/rust/issues/82523>
78+
= note: this error originates in the derive macro `PartialEq` (in Nightly builds, run with -Z macro-backtrace for more info)
79+
80+
Future breakage diagnostic:
81+
error: `#[derive]` can't be used on a `#[repr(packed)]` struct that does not derive Copy (error E0133)
82+
--> $DIR/deriving-with-repr-packed.rs:16:10
83+
|
84+
LL | #[derive(PartialEq, Eq)]
85+
| ^^^^^^^^^
86+
|
87+
note: the lint level is defined here
88+
--> $DIR/deriving-with-repr-packed.rs:1:9
89+
|
90+
LL | #![deny(unaligned_references)]
91+
| ^^^^^^^^^^^^^^^^^^^^
92+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
93+
= note: for more information, see issue #82523 <https://github.com/rust-lang/rust/issues/82523>
94+
= note: this error originates in the derive macro `PartialEq` (in Nightly builds, run with -Z macro-backtrace for more info)
95+
96+
Future breakage diagnostic:
97+
error: `#[derive]` can't be used on a `#[repr(packed)]` struct that does not derive Copy (error E0133)
98+
--> $DIR/deriving-with-repr-packed.rs:25:10
99+
|
100+
LL | #[derive(PartialEq)]
101+
| ^^^^^^^^^
102+
|
103+
note: the lint level is defined here
104+
--> $DIR/deriving-with-repr-packed.rs:1:9
105+
|
106+
LL | #![deny(unaligned_references)]
107+
| ^^^^^^^^^^^^^^^^^^^^
108+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
109+
= note: for more information, see issue #82523 <https://github.com/rust-lang/rust/issues/82523>
110+
= note: this error originates in the derive macro `PartialEq` (in Nightly builds, run with -Z macro-backtrace for more info)
111+

0 commit comments

Comments
 (0)