Skip to content

Commit 23fa536

Browse files
committed
Auto merge of #76881 - hameerabbasi:issue-53325, r=oli-obk
Add allocation information to undefined behaviour errors. So far I'm looking on information on whether the error messages are suitable. Fixes #53325.
2 parents 36bcf40 + 49705e0 commit 23fa536

File tree

74 files changed

+1904
-167
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

74 files changed

+1904
-167
lines changed

compiler/rustc_mir/src/const_eval/eval_queries.rs

+10
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use crate::interpret::{
55
Immediate, InternKind, InterpCx, InterpResult, MPlaceTy, MemoryKind, OpTy, RefTracking, Scalar,
66
ScalarMaybeUninit, StackPopCleanup,
77
};
8+
use crate::util::pretty::display_allocation;
89

910
use rustc_errors::ErrorReported;
1011
use rustc_hir::def::DefKind;
@@ -360,6 +361,15 @@ pub fn eval_to_allocation_raw_provider<'tcx>(
360361
"it is undefined behavior to use this value",
361362
|mut diag| {
362363
diag.note(note_on_undefined_behavior_error());
364+
diag.note(&format!(
365+
"the raw bytes of the constant ({}",
366+
display_allocation(
367+
*ecx.tcx,
368+
ecx.tcx
369+
.global_alloc(mplace.ptr.assert_ptr().alloc_id)
370+
.unwrap_memory()
371+
)
372+
));
363373
diag.emit();
364374
},
365375
))

src/test/ui/const-generics/min_const_generics/invalid-patterns.stderr renamed to src/test/ui/const-generics/min_const_generics/invalid-patterns.32bit.stderr

+20-8
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,70 @@
11
error[E0308]: mismatched types
2-
--> $DIR/invalid-patterns.rs:28:21
2+
--> $DIR/invalid-patterns.rs:29:21
33
|
44
LL | get_flag::<false, 0xFF>();
55
| ^^^^ expected `char`, found `u8`
66

77
error[E0308]: mismatched types
8-
--> $DIR/invalid-patterns.rs:30:14
8+
--> $DIR/invalid-patterns.rs:31:14
99
|
1010
LL | get_flag::<7, 'c'>();
1111
| ^ expected `bool`, found integer
1212

1313
error[E0308]: mismatched types
14-
--> $DIR/invalid-patterns.rs:32:14
14+
--> $DIR/invalid-patterns.rs:33:14
1515
|
1616
LL | get_flag::<42, 0x5ad>();
1717
| ^^ expected `bool`, found integer
1818

1919
error[E0308]: mismatched types
20-
--> $DIR/invalid-patterns.rs:32:18
20+
--> $DIR/invalid-patterns.rs:33:18
2121
|
2222
LL | get_flag::<42, 0x5ad>();
2323
| ^^^^^ expected `char`, found `u8`
2424

2525
error[E0080]: it is undefined behavior to use this value
26-
--> $DIR/invalid-patterns.rs:37:21
26+
--> $DIR/invalid-patterns.rs:38:21
2727
|
2828
LL | get_flag::<false, { unsafe { char_raw.character } }>();
2929
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered uninitialized bytes, but expected a valid unicode scalar value (in `0..=0x10FFFF` but not in `0xD800..=0xDFFF`)
3030
|
3131
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior.
32+
= note: the raw bytes of the constant (size: 4, align: 4) {
33+
__ __ __ __ │ ░░░░
34+
}
3235

3336
error[E0080]: it is undefined behavior to use this value
34-
--> $DIR/invalid-patterns.rs:39:14
37+
--> $DIR/invalid-patterns.rs:40:14
3538
|
3639
LL | get_flag::<{ unsafe { bool_raw.boolean } }, 'z'>();
3740
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered 0x42, but expected a boolean
3841
|
3942
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior.
43+
= note: the raw bytes of the constant (size: 1, align: 1) {
44+
42 │ B
45+
}
4046

4147
error[E0080]: it is undefined behavior to use this value
42-
--> $DIR/invalid-patterns.rs:41:14
48+
--> $DIR/invalid-patterns.rs:42:14
4349
|
4450
LL | get_flag::<{ unsafe { bool_raw.boolean } }, { unsafe { char_raw.character } }>();
4551
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered 0x42, but expected a boolean
4652
|
4753
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior.
54+
= note: the raw bytes of the constant (size: 1, align: 1) {
55+
42 │ B
56+
}
4857

4958
error[E0080]: it is undefined behavior to use this value
50-
--> $DIR/invalid-patterns.rs:41:47
59+
--> $DIR/invalid-patterns.rs:42:47
5160
|
5261
LL | get_flag::<{ unsafe { bool_raw.boolean } }, { unsafe { char_raw.character } }>();
5362
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered uninitialized bytes, but expected a valid unicode scalar value (in `0..=0x10FFFF` but not in `0xD800..=0xDFFF`)
5463
|
5564
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior.
65+
= note: the raw bytes of the constant (size: 4, align: 4) {
66+
__ __ __ __ │ ░░░░
67+
}
5668

5769
error: aborting due to 8 previous errors
5870

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/invalid-patterns.rs:29:21
3+
|
4+
LL | get_flag::<false, 0xFF>();
5+
| ^^^^ expected `char`, found `u8`
6+
7+
error[E0308]: mismatched types
8+
--> $DIR/invalid-patterns.rs:31:14
9+
|
10+
LL | get_flag::<7, 'c'>();
11+
| ^ expected `bool`, found integer
12+
13+
error[E0308]: mismatched types
14+
--> $DIR/invalid-patterns.rs:33:14
15+
|
16+
LL | get_flag::<42, 0x5ad>();
17+
| ^^ expected `bool`, found integer
18+
19+
error[E0308]: mismatched types
20+
--> $DIR/invalid-patterns.rs:33:18
21+
|
22+
LL | get_flag::<42, 0x5ad>();
23+
| ^^^^^ expected `char`, found `u8`
24+
25+
error[E0080]: it is undefined behavior to use this value
26+
--> $DIR/invalid-patterns.rs:38:21
27+
|
28+
LL | get_flag::<false, { unsafe { char_raw.character } }>();
29+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered uninitialized bytes, but expected a valid unicode scalar value (in `0..=0x10FFFF` but not in `0xD800..=0xDFFF`)
30+
|
31+
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior.
32+
= note: the raw bytes of the constant (size: 4, align: 4) {
33+
__ __ __ __ │ ░░░░
34+
}
35+
36+
error[E0080]: it is undefined behavior to use this value
37+
--> $DIR/invalid-patterns.rs:40:14
38+
|
39+
LL | get_flag::<{ unsafe { bool_raw.boolean } }, 'z'>();
40+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered 0x42, but expected a boolean
41+
|
42+
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior.
43+
= note: the raw bytes of the constant (size: 1, align: 1) {
44+
42 │ B
45+
}
46+
47+
error[E0080]: it is undefined behavior to use this value
48+
--> $DIR/invalid-patterns.rs:42:14
49+
|
50+
LL | get_flag::<{ unsafe { bool_raw.boolean } }, { unsafe { char_raw.character } }>();
51+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered 0x42, but expected a boolean
52+
|
53+
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior.
54+
= note: the raw bytes of the constant (size: 1, align: 1) {
55+
42 │ B
56+
}
57+
58+
error[E0080]: it is undefined behavior to use this value
59+
--> $DIR/invalid-patterns.rs:42:47
60+
|
61+
LL | get_flag::<{ unsafe { bool_raw.boolean } }, { unsafe { char_raw.character } }>();
62+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered uninitialized bytes, but expected a valid unicode scalar value (in `0..=0x10FFFF` but not in `0xD800..=0xDFFF`)
63+
|
64+
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior.
65+
= note: the raw bytes of the constant (size: 4, align: 4) {
66+
__ __ __ __ │ ░░░░
67+
}
68+
69+
error: aborting due to 8 previous errors
70+
71+
Some errors have detailed explanations: E0080, E0308.
72+
For more information about an error, try `rustc --explain E0080`.

src/test/ui/const-generics/min_const_generics/invalid-patterns.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// stderr-per-bitwidth
12
use std::mem::transmute;
23

34
fn get_flag<const FlagSet: bool, const ShortName: char>() -> Option<char> {

src/test/ui/consts/const-err4.stderr renamed to src/test/ui/consts/const-err4.32bit.stderr

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
error[E0080]: it is undefined behavior to use this value
2-
--> $DIR/const-err4.rs:8:11
2+
--> $DIR/const-err4.rs:9:11
33
|
44
LL | Boo = [unsafe { Foo { b: () }.a }; 4][3],
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered uninitialized bytes, but expected initialized plain (non-pointer) bytes
66
|
77
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior.
8+
= note: the raw bytes of the constant (size: 4, align: 4) {
9+
__ __ __ __ │ ░░░░
10+
}
811

912
error: aborting due to previous error
1013

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error[E0080]: it is undefined behavior to use this value
2+
--> $DIR/const-err4.rs:9:11
3+
|
4+
LL | Boo = [unsafe { Foo { b: () }.a }; 4][3],
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered uninitialized bytes, but expected initialized plain (non-pointer) bytes
6+
|
7+
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior.
8+
= note: the raw bytes of the constant (size: 8, align: 8) {
9+
__ __ __ __ __ __ __ __ │ ░░░░░░░░
10+
}
11+
12+
error: aborting due to previous error
13+
14+
For more information about this error, try `rustc --explain E0080`.

src/test/ui/consts/const-err4.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// stderr-per-bitwidth
12
#[derive(Copy, Clone)]
23
union Foo {
34
a: isize,

0 commit comments

Comments
 (0)