Skip to content

Commit e30d6d9

Browse files
committed
make unaligned_references lint deny-by-default
1 parent de392c7 commit e30d6d9

13 files changed

+39
-41
lines changed

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]

compiler/rustc_lint_defs/src/builtin.rs

+1-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,7 +1137,7 @@ 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>",

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]

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

+6-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,6 @@ 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`.

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,15 @@
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
1515

src/test/ui/packed/issue-27060.rs

-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ pub struct Good {
55
aligned: [u8; 32],
66
}
77

8-
#[deny(unaligned_references)]
98
fn main() {
109
let good = Good {
1110
data: &0,

src/test/ui/packed/issue-27060.stderr

+5-9
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,17 @@
11
error: reference to packed field is unaligned
2-
--> $DIR/issue-27060.rs:16:13
2+
--> $DIR/issue-27060.rs:15:13
33
|
44
LL | let _ = &good.data;
55
| ^^^^^^^^^^
66
|
7-
note: the lint level is defined here
8-
--> $DIR/issue-27060.rs:8:8
9-
|
10-
LL | #[deny(unaligned_references)]
11-
| ^^^^^^^^^^^^^^^^^^^^
7+
= note: `#[deny(unaligned_references)]` on by default
128
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
139
= note: for more information, see issue #82523 <https://github.com/rust-lang/rust/issues/82523>
1410
= note: fields of packed structs are not properly aligned, and creating a misaligned reference is undefined behavior (even if that reference is never dereferenced)
1511
= 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)
1612

1713
error: reference to packed field is unaligned
18-
--> $DIR/issue-27060.rs:18:13
14+
--> $DIR/issue-27060.rs:17:13
1915
|
2016
LL | let _ = &good.data2[0];
2117
| ^^^^^^^^^^^^^^
@@ -26,7 +22,7 @@ LL | let _ = &good.data2[0];
2622
= 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)
2723

2824
error: reference to packed field is unaligned
29-
--> $DIR/issue-27060.rs:21:13
25+
--> $DIR/issue-27060.rs:20:13
3026
|
3127
LL | let _ = &good.data;
3228
| ^^^^^^^^^^
@@ -37,7 +33,7 @@ LL | let _ = &good.data;
3733
= 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)
3834

3935
error: reference to packed field is unaligned
40-
--> $DIR/issue-27060.rs:23:13
36+
--> $DIR/issue-27060.rs:22:13
4137
|
4238
LL | let _ = &good.data2[0];
4339
| ^^^^^^^^^^^^^^

src/test/ui/packed/packed-struct-address-of-element.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
// run-pass
22
#![allow(dead_code)]
3-
#![deny(unaligned_references)]
43
#![feature(raw_ref_op)]
54
// ignore-emscripten weird assertion?
65

src/test/ui/packed/packed-struct-borrow-element-64bit.rs

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ struct Foo4C {
99
baz: usize
1010
}
1111

12+
#[warn(unaligned_references)]
1213
pub fn main() {
1314
let foo = Foo4C { bar: 1, baz: 2 };
1415
let brw = &foo.baz; //~WARN reference to packed field is unaligned

src/test/ui/packed/packed-struct-borrow-element-64bit.stderr

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
warning: reference to packed field is unaligned
2-
--> $DIR/packed-struct-borrow-element-64bit.rs:14:15
2+
--> $DIR/packed-struct-borrow-element-64bit.rs:15:15
33
|
44
LL | let brw = &foo.baz;
55
| ^^^^^^^^
66
|
7-
= note: `#[warn(unaligned_references)]` on by default
7+
note: the lint level is defined here
8+
--> $DIR/packed-struct-borrow-element-64bit.rs:12:8
9+
|
10+
LL | #[warn(unaligned_references)]
11+
| ^^^^^^^^^^^^^^^^^^^^
812
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
913
= note: for more information, see issue #82523 <https://github.com/rust-lang/rust/issues/82523>
1014
= note: fields of packed structs are not properly aligned, and creating a misaligned reference is undefined behavior (even if that reference is never dereferenced)

src/test/ui/packed/packed-struct-borrow-element.rs

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ struct Foo4C {
2020
baz: usize
2121
}
2222

23+
#[warn(unaligned_references)]
2324
pub fn main() {
2425
let foo = Foo1 { bar: 1, baz: 2 };
2526
let brw = &foo.baz; //~WARN reference to packed field is unaligned

src/test/ui/packed/packed-struct-borrow-element.stderr

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
11
warning: reference to packed field is unaligned
2-
--> $DIR/packed-struct-borrow-element.rs:25:15
2+
--> $DIR/packed-struct-borrow-element.rs:26:15
33
|
44
LL | let brw = &foo.baz;
55
| ^^^^^^^^
66
|
7-
= note: `#[warn(unaligned_references)]` on by default
7+
note: the lint level is defined here
8+
--> $DIR/packed-struct-borrow-element.rs:23:8
9+
|
10+
LL | #[warn(unaligned_references)]
11+
| ^^^^^^^^^^^^^^^^^^^^
812
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
913
= note: for more information, see issue #82523 <https://github.com/rust-lang/rust/issues/82523>
1014
= note: fields of packed structs are not properly aligned, and creating a misaligned reference is undefined behavior (even if that reference is never dereferenced)
1115
= 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)
1216

1317
warning: reference to packed field is unaligned
14-
--> $DIR/packed-struct-borrow-element.rs:30:15
18+
--> $DIR/packed-struct-borrow-element.rs:31:15
1519
|
1620
LL | let brw = &foo.baz;
1721
| ^^^^^^^^

0 commit comments

Comments
 (0)