Skip to content

Commit 107d480

Browse files
committed
improve error notes for packed struct reference diagnostic
1 parent f495605 commit 107d480

File tree

8 files changed

+55
-27
lines changed

8 files changed

+55
-27
lines changed

compiler/rustc_mir_transform/src/check_packed_ref.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,11 @@ impl<'tcx> Visitor<'tcx> for PackedRefChecker<'_, 'tcx> {
5656
"reference to packed field is unaligned"
5757
)
5858
.note(
59-
"fields of packed structs are not properly aligned, and creating \
60-
a misaligned reference is undefined behavior (even if that \
59+
"packed structs are only aligned by one byte, and many modern architectures \
60+
penalize unaligned field accesses"
61+
)
62+
.note(
63+
"creating a misaligned reference is undefined behavior (even if that \
6164
reference is never dereferenced)",
6265
).help(
6366
"copy the field contents to a local variable, or replace the \

tests/ui/binding/issue-53114-safety-checks.stderr

+12-6
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ error[E0793]: reference to packed field is unaligned
44
LL | let _ = &p.b;
55
| ^^^^
66
|
7-
= note: fields of packed structs are not properly aligned, and creating a misaligned reference is undefined behavior (even if that reference is never dereferenced)
7+
= note: packed structs are only aligned by one byte, and many modern architectures penalize unaligned field accesses
8+
= note: creating a misaligned reference is undefined behavior (even if that reference is never dereferenced)
89
= 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)
910

1011
error[E0793]: reference to packed field is unaligned
@@ -13,7 +14,8 @@ error[E0793]: reference to packed field is unaligned
1314
LL | let (_,) = (&p.b,);
1415
| ^^^^
1516
|
16-
= note: fields of packed structs are not properly aligned, and creating a misaligned reference is undefined behavior (even if that reference is never dereferenced)
17+
= note: packed structs are only aligned by one byte, and many modern architectures penalize unaligned field accesses
18+
= note: creating a misaligned reference is undefined behavior (even if that reference is never dereferenced)
1719
= 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)
1820

1921
error[E0793]: reference to packed field is unaligned
@@ -22,7 +24,8 @@ error[E0793]: reference to packed field is unaligned
2224
LL | let _: _ = &p.b;
2325
| ^^^^
2426
|
25-
= 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+
= note: packed structs are only aligned by one byte, and many modern architectures penalize unaligned field accesses
28+
= note: creating a misaligned reference is undefined behavior (even if that reference is never dereferenced)
2629
= 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)
2730

2831
error[E0793]: reference to packed field is unaligned
@@ -31,7 +34,8 @@ error[E0793]: reference to packed field is unaligned
3134
LL | let (_,): _ = (&p.b,);
3235
| ^^^^
3336
|
34-
= 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+
= note: packed structs are only aligned by one byte, and many modern architectures penalize unaligned field accesses
38+
= note: creating a misaligned reference is undefined behavior (even if that reference is never dereferenced)
3539
= 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)
3640

3741
error[E0793]: reference to packed field is unaligned
@@ -40,7 +44,8 @@ error[E0793]: reference to packed field is unaligned
4044
LL | match &p.b { _ => { } }
4145
| ^^^^
4246
|
43-
= note: fields of packed structs are not properly aligned, and creating a misaligned reference is undefined behavior (even if that reference is never dereferenced)
47+
= note: packed structs are only aligned by one byte, and many modern architectures penalize unaligned field accesses
48+
= note: creating a misaligned reference is undefined behavior (even if that reference is never dereferenced)
4449
= 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)
4550

4651
error[E0793]: reference to packed field is unaligned
@@ -49,7 +54,8 @@ error[E0793]: reference to packed field is unaligned
4954
LL | match (&p.b,) { (_,) => { } }
5055
| ^^^^
5156
|
52-
= note: fields of packed structs are not properly aligned, and creating a misaligned reference is undefined behavior (even if that reference is never dereferenced)
57+
= note: packed structs are only aligned by one byte, and many modern architectures penalize unaligned field accesses
58+
= note: creating a misaligned reference is undefined behavior (even if that reference is never dereferenced)
5359
= 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)
5460

5561
error[E0133]: access to union field is unsafe and requires unsafe function or block

tests/ui/closures/2229_closure_analysis/diagnostics/repr_packed.stderr

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ error[E0793]: reference to packed field is unaligned
44
LL | println!("{}", foo.x);
55
| ^^^^^
66
|
7-
= note: fields of packed structs are not properly aligned, and creating a misaligned reference is undefined behavior (even if that reference is never dereferenced)
7+
= note: packed structs are only aligned by one byte, and many modern architectures penalize unaligned field accesses
8+
= note: creating a misaligned reference is undefined behavior (even if that reference is never dereferenced)
89
= 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)
910
= note: this error originates in the macro `$crate::format_args_nl` which comes from the expansion of the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info)
1011

tests/ui/lint/unaligned_references.stderr

+20-10
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ error[E0793]: reference to packed field is unaligned
44
LL | &self.x;
55
| ^^^^^^^
66
|
7-
= note: fields of packed structs are not properly aligned, and creating a misaligned reference is undefined behavior (even if that reference is never dereferenced)
7+
= note: packed structs are only aligned by one byte, and many modern architectures penalize unaligned field accesses
8+
= note: creating a misaligned reference is undefined behavior (even if that reference is never dereferenced)
89
= 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)
910

1011
error[E0793]: reference to packed field is unaligned
@@ -13,7 +14,8 @@ error[E0793]: reference to packed field is unaligned
1314
LL | let _ = &good.ptr;
1415
| ^^^^^^^^^
1516
|
16-
= note: fields of packed structs are not properly aligned, and creating a misaligned reference is undefined behavior (even if that reference is never dereferenced)
17+
= note: packed structs are only aligned by one byte, and many modern architectures penalize unaligned field accesses
18+
= note: creating a misaligned reference is undefined behavior (even if that reference is never dereferenced)
1719
= 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)
1820

1921
error[E0793]: reference to packed field is unaligned
@@ -22,7 +24,8 @@ error[E0793]: reference to packed field is unaligned
2224
LL | let _ = &good.data;
2325
| ^^^^^^^^^^
2426
|
25-
= 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+
= note: packed structs are only aligned by one byte, and many modern architectures penalize unaligned field accesses
28+
= note: creating a misaligned reference is undefined behavior (even if that reference is never dereferenced)
2629
= 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)
2730

2831
error[E0793]: reference to packed field is unaligned
@@ -31,7 +34,8 @@ error[E0793]: reference to packed field is unaligned
3134
LL | let _ = &good.data as *const _;
3235
| ^^^^^^^^^^
3336
|
34-
= 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+
= note: packed structs are only aligned by one byte, and many modern architectures penalize unaligned field accesses
38+
= note: creating a misaligned reference is undefined behavior (even if that reference is never dereferenced)
3539
= 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)
3640

3741
error[E0793]: reference to packed field is unaligned
@@ -40,7 +44,8 @@ error[E0793]: reference to packed field is unaligned
4044
LL | let _: *const _ = &good.data;
4145
| ^^^^^^^^^^
4246
|
43-
= note: fields of packed structs are not properly aligned, and creating a misaligned reference is undefined behavior (even if that reference is never dereferenced)
47+
= note: packed structs are only aligned by one byte, and many modern architectures penalize unaligned field accesses
48+
= note: creating a misaligned reference is undefined behavior (even if that reference is never dereferenced)
4449
= 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)
4550

4651
error[E0793]: reference to packed field is unaligned
@@ -49,7 +54,8 @@ error[E0793]: reference to packed field is unaligned
4954
LL | let _ = good.data.clone();
5055
| ^^^^^^^^^^^^^^^^^
5156
|
52-
= note: fields of packed structs are not properly aligned, and creating a misaligned reference is undefined behavior (even if that reference is never dereferenced)
57+
= note: packed structs are only aligned by one byte, and many modern architectures penalize unaligned field accesses
58+
= note: creating a misaligned reference is undefined behavior (even if that reference is never dereferenced)
5359
= 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)
5460

5561
error[E0793]: reference to packed field is unaligned
@@ -58,7 +64,8 @@ error[E0793]: reference to packed field is unaligned
5864
LL | let _ = &good.data2[0];
5965
| ^^^^^^^^^^^^^^
6066
|
61-
= note: fields of packed structs are not properly aligned, and creating a misaligned reference is undefined behavior (even if that reference is never dereferenced)
67+
= note: packed structs are only aligned by one byte, and many modern architectures penalize unaligned field accesses
68+
= note: creating a misaligned reference is undefined behavior (even if that reference is never dereferenced)
6269
= 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)
6370

6471
error[E0793]: reference to packed field is unaligned
@@ -67,7 +74,8 @@ error[E0793]: reference to packed field is unaligned
6774
LL | let _ = &packed2.x;
6875
| ^^^^^^^^^^
6976
|
70-
= note: fields of packed structs are not properly aligned, and creating a misaligned reference is undefined behavior (even if that reference is never dereferenced)
77+
= note: packed structs are only aligned by one byte, and many modern architectures penalize unaligned field accesses
78+
= note: creating a misaligned reference is undefined behavior (even if that reference is never dereferenced)
7179
= 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)
7280

7381
error[E0793]: reference to packed field is unaligned
@@ -76,7 +84,8 @@ error[E0793]: reference to packed field is unaligned
7684
LL | let _ref = &m1.1.a;
7785
| ^^^^^^^
7886
|
79-
= note: fields of packed structs are not properly aligned, and creating a misaligned reference is undefined behavior (even if that reference is never dereferenced)
87+
= note: packed structs are only aligned by one byte, and many modern architectures penalize unaligned field accesses
88+
= note: creating a misaligned reference is undefined behavior (even if that reference is never dereferenced)
8089
= 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)
8190

8291
error[E0793]: reference to packed field is unaligned
@@ -85,7 +94,8 @@ error[E0793]: reference to packed field is unaligned
8594
LL | let _ref = &m2.1.a;
8695
| ^^^^^^^
8796
|
88-
= note: fields of packed structs are not properly aligned, and creating a misaligned reference is undefined behavior (even if that reference is never dereferenced)
97+
= note: packed structs are only aligned by one byte, and many modern architectures penalize unaligned field accesses
98+
= note: creating a misaligned reference is undefined behavior (even if that reference is never dereferenced)
8999
= 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)
90100

91101
error: aborting due to 10 previous errors

tests/ui/lint/unaligned_references_external_macro.stderr

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ LL | | }
99
LL | | }
1010
| |_^
1111
|
12-
= note: fields of packed structs are not properly aligned, and creating a misaligned reference is undefined behavior (even if that reference is never dereferenced)
12+
= note: packed structs are only aligned by one byte, and many modern architectures penalize unaligned field accesses
13+
= note: creating a misaligned reference is undefined behavior (even if that reference is never dereferenced)
1314
= 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)
1415
= note: this error originates in the macro `unaligned_references_external_crate::mac` (in Nightly builds, run with -Z macro-backtrace for more info)
1516

tests/ui/packed/issue-27060.stderr

+8-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ error[E0793]: reference to packed field is unaligned
44
LL | let _ = &good.data;
55
| ^^^^^^^^^^
66
|
7-
= note: fields of packed structs are not properly aligned, and creating a misaligned reference is undefined behavior (even if that reference is never dereferenced)
7+
= note: packed structs are only aligned by one byte, and many modern architectures penalize unaligned field accesses
8+
= note: creating a misaligned reference is undefined behavior (even if that reference is never dereferenced)
89
= 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)
910

1011
error[E0793]: reference to packed field is unaligned
@@ -13,7 +14,8 @@ error[E0793]: reference to packed field is unaligned
1314
LL | let _ = &good.data2[0];
1415
| ^^^^^^^^^^^^^^
1516
|
16-
= note: fields of packed structs are not properly aligned, and creating a misaligned reference is undefined behavior (even if that reference is never dereferenced)
17+
= note: packed structs are only aligned by one byte, and many modern architectures penalize unaligned field accesses
18+
= note: creating a misaligned reference is undefined behavior (even if that reference is never dereferenced)
1719
= 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)
1820

1921
error[E0793]: reference to packed field is unaligned
@@ -22,7 +24,8 @@ error[E0793]: reference to packed field is unaligned
2224
LL | let _ = &good.data;
2325
| ^^^^^^^^^^
2426
|
25-
= 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+
= note: packed structs are only aligned by one byte, and many modern architectures penalize unaligned field accesses
28+
= note: creating a misaligned reference is undefined behavior (even if that reference is never dereferenced)
2629
= 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)
2730

2831
error[E0793]: reference to packed field is unaligned
@@ -31,7 +34,8 @@ error[E0793]: reference to packed field is unaligned
3134
LL | let _ = &good.data2[0];
3235
| ^^^^^^^^^^^^^^
3336
|
34-
= 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+
= note: packed structs are only aligned by one byte, and many modern architectures penalize unaligned field accesses
38+
= note: creating a misaligned reference is undefined behavior (even if that reference is never dereferenced)
3539
= 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)
3640

3741
error: aborting due to 4 previous errors

tests/ui/packed/packed-struct-borrow-element-64bit.stderr

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ error[E0793]: reference to packed field is unaligned
44
LL | let brw = &foo.baz;
55
| ^^^^^^^^
66
|
7-
= note: fields of packed structs are not properly aligned, and creating a misaligned reference is undefined behavior (even if that reference is never dereferenced)
7+
= note: packed structs are only aligned by one byte, and many modern architectures penalize unaligned field accesses
8+
= note: creating a misaligned reference is undefined behavior (even if that reference is never dereferenced)
89
= 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)
910

1011
error: aborting due to previous error

0 commit comments

Comments
 (0)