Skip to content

Commit 7d18a45

Browse files
committed
give more help in the unaligned_references lint
1 parent e250777 commit 7d18a45

File tree

8 files changed

+25
-0
lines changed

8 files changed

+25
-0
lines changed

compiler/rustc_mir_transform/src/check_packed_ref.rs

+5
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,11 @@ impl<'tcx> Visitor<'tcx> for PackedRefChecker<'_, 'tcx> {
105105
a misaligned reference is undefined behavior (even if that \
106106
reference is never dereferenced)",
107107
)
108+
.help(
109+
"copy the field contents to a local variable, or replace the \
110+
reference with a raw pointer and use `read_unaligned`/`write_unaligned` \
111+
(loads and stores via `*p` must be properly aligned even when using raw pointers)"
112+
)
108113
.emit()
109114
},
110115
);

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

+4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ LL | let _ = &p.b;
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)
11+
= 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)
1112

1213
warning: reference to packed field is unaligned
1314
--> $DIR/issue-53114-safety-checks.rs:29:17
@@ -18,6 +19,7 @@ LL | let (_,) = (&p.b,);
1819
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
1920
= note: for more information, see issue #82523 <https://github.com/rust-lang/rust/issues/82523>
2021
= note: fields of packed structs are not properly aligned, and creating a misaligned reference is undefined behavior (even if that reference is never dereferenced)
22+
= 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)
2123

2224
warning: reference to packed field is unaligned
2325
--> $DIR/issue-53114-safety-checks.rs:39:11
@@ -28,6 +30,7 @@ LL | match &p.b { _ => { } }
2830
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
2931
= note: for more information, see issue #82523 <https://github.com/rust-lang/rust/issues/82523>
3032
= note: fields of packed structs are not properly aligned, and creating a misaligned reference is undefined behavior (even if that reference is never dereferenced)
33+
= 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)
3134

3235
warning: reference to packed field is unaligned
3336
--> $DIR/issue-53114-safety-checks.rs:45:12
@@ -38,6 +41,7 @@ LL | match (&p.b,) { (_,) => { } }
3841
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
3942
= note: for more information, see issue #82523 <https://github.com/rust-lang/rust/issues/82523>
4043
= note: fields of packed structs are not properly aligned, and creating a misaligned reference is undefined behavior (even if that reference is never dereferenced)
44+
= 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)
4145

4246
error[E0133]: access to union field is unsafe and requires unsafe function or block
4347
--> $DIR/issue-53114-safety-checks.rs:26:13

src/test/ui/closures/2229_closure_analysis/diagnostics/repr_packed.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ LL | println!("{}", foo.x);
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)
11+
= 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)
1112
= note: this warning originates in the macro `$crate::format_args_nl` (in Nightly builds, run with -Z macro-backtrace for more info)
1213

1314
warning: 1 warning emitted

src/test/ui/lint/unaligned_references.stderr

+7
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ LL | #![deny(unaligned_references)]
1212
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
1313
= note: for more information, see issue #82523 <https://github.com/rust-lang/rust/issues/82523>
1414
= note: fields of packed structs are not properly aligned, and creating a misaligned reference is undefined behavior (even if that reference is never dereferenced)
15+
= 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)
1516

1617
error: reference to packed field is unaligned
1718
--> $DIR/unaligned_references.rs:24:17
@@ -22,6 +23,7 @@ LL | let _ = &good.data;
2223
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
2324
= note: for more information, see issue #82523 <https://github.com/rust-lang/rust/issues/82523>
2425
= note: fields of packed structs are not properly aligned, and creating a misaligned reference is undefined behavior (even if that reference is never dereferenced)
26+
= 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)
2527

2628
error: reference to packed field is unaligned
2729
--> $DIR/unaligned_references.rs:27:17
@@ -32,6 +34,7 @@ LL | let _ = &good.data as *const _;
3234
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
3335
= note: for more information, see issue #82523 <https://github.com/rust-lang/rust/issues/82523>
3436
= note: fields of packed structs are not properly aligned, and creating a misaligned reference is undefined behavior (even if that reference is never dereferenced)
37+
= 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)
3538

3639
error: reference to packed field is unaligned
3740
--> $DIR/unaligned_references.rs:29:27
@@ -42,6 +45,7 @@ LL | let _: *const _ = &good.data;
4245
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
4346
= note: for more information, see issue #82523 <https://github.com/rust-lang/rust/issues/82523>
4447
= note: fields of packed structs are not properly aligned, and creating a misaligned reference is undefined behavior (even if that reference is never dereferenced)
48+
= 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)
4549

4650
error: reference to packed field is unaligned
4751
--> $DIR/unaligned_references.rs:32:17
@@ -52,6 +56,7 @@ LL | let _ = good.data.clone();
5256
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
5357
= note: for more information, see issue #82523 <https://github.com/rust-lang/rust/issues/82523>
5458
= note: fields of packed structs are not properly aligned, and creating a misaligned reference is undefined behavior (even if that reference is never dereferenced)
59+
= 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)
5560

5661
error: reference to packed field is unaligned
5762
--> $DIR/unaligned_references.rs:35:17
@@ -62,6 +67,7 @@ LL | let _ = &good.data2[0];
6267
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
6368
= note: for more information, see issue #82523 <https://github.com/rust-lang/rust/issues/82523>
6469
= note: fields of packed structs are not properly aligned, and creating a misaligned reference is undefined behavior (even if that reference is never dereferenced)
70+
= 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)
6571

6672
error: reference to packed field is unaligned
6773
--> $DIR/unaligned_references.rs:45:17
@@ -72,6 +78,7 @@ LL | let _ = &packed2.x;
7278
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
7379
= note: for more information, see issue #82523 <https://github.com/rust-lang/rust/issues/82523>
7480
= note: fields of packed structs are not properly aligned, and creating a misaligned reference is undefined behavior (even if that reference is never dereferenced)
81+
= 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)
7582

7683
error: aborting due to 7 previous errors
7784

src/test/ui/lint/unaligned_references_external_macro.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ LL | | }
2424
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
2525
= note: for more information, see issue #82523 <https://github.com/rust-lang/rust/issues/82523>
2626
= 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)
2728
= note: this error originates in the macro `unaligned_references_external_crate::mac` (in Nightly builds, run with -Z macro-backtrace for more info)
2829

2930
error: aborting due to previous error

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

+4
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ LL | #[deny(unaligned_references)]
1212
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
1313
= note: for more information, see issue #82523 <https://github.com/rust-lang/rust/issues/82523>
1414
= note: fields of packed structs are not properly aligned, and creating a misaligned reference is undefined behavior (even if that reference is never dereferenced)
15+
= 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)
1516

1617
error: reference to packed field is unaligned
1718
--> $DIR/issue-27060.rs:18:13
@@ -22,6 +23,7 @@ LL | let _ = &good.data2[0];
2223
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
2324
= note: for more information, see issue #82523 <https://github.com/rust-lang/rust/issues/82523>
2425
= note: fields of packed structs are not properly aligned, and creating a misaligned reference is undefined behavior (even if that reference is never dereferenced)
26+
= 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)
2527

2628
error: reference to packed field is unaligned
2729
--> $DIR/issue-27060.rs:21:13
@@ -32,6 +34,7 @@ LL | let _ = &good.data;
3234
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
3335
= note: for more information, see issue #82523 <https://github.com/rust-lang/rust/issues/82523>
3436
= note: fields of packed structs are not properly aligned, and creating a misaligned reference is undefined behavior (even if that reference is never dereferenced)
37+
= 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)
3538

3639
error: reference to packed field is unaligned
3740
--> $DIR/issue-27060.rs:23:13
@@ -42,6 +45,7 @@ LL | let _ = &good.data2[0];
4245
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
4346
= note: for more information, see issue #82523 <https://github.com/rust-lang/rust/issues/82523>
4447
= note: fields of packed structs are not properly aligned, and creating a misaligned reference is undefined behavior (even if that reference is never dereferenced)
48+
= 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)
4549

4650
error: aborting due to 4 previous errors
4751

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

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ LL | let brw = &foo.baz;
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)
11+
= 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)
1112

1213
warning: 1 warning emitted
1314

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

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ LL | let brw = &foo.baz;
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)
11+
= 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)
1112

1213
warning: reference to packed field is unaligned
1314
--> $DIR/packed-struct-borrow-element.rs:30:15
@@ -18,6 +19,7 @@ LL | let brw = &foo.baz;
1819
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
1920
= note: for more information, see issue #82523 <https://github.com/rust-lang/rust/issues/82523>
2021
= note: fields of packed structs are not properly aligned, and creating a misaligned reference is undefined behavior (even if that reference is never dereferenced)
22+
= 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)
2123

2224
warning: 2 warnings emitted
2325

0 commit comments

Comments
 (0)