Skip to content

Commit c3ec0ad

Browse files
committed
Add allocation information to undefined behaviour errors.
1 parent 6ff482b commit c3ec0ad

29 files changed

+351
-33
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

+12
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ 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
3437
--> $DIR/invalid-patterns.rs:39:14
@@ -37,6 +40,9 @@ 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
4248
--> $DIR/invalid-patterns.rs:41:14
@@ -45,6 +51,9 @@ 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
5059
--> $DIR/invalid-patterns.rs:41:47
@@ -53,6 +62,9 @@ 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

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

+3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ 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: 8, align: 8) {
9+
__ __ __ __ __ __ __ __ │ ░░░░░░░░
10+
}
811

912
error: aborting due to previous error
1013

src/test/ui/consts/const-eval/const-pointer-values-in-various-types.stderr

+27
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ LL | const I32_REF_USIZE_UNION: usize = unsafe { Nonsense { int_32_ref: &3 }
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered pointer to alloc2, 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: 8, align: 8) {
9+
╾───────alloc2────────╼ │ ╾──────╼
10+
}
811

912
error: any use of this value will cause an error
1013
--> $DIR/const-pointer-values-in-various-types.rs:28:43
@@ -47,6 +50,9 @@ LL | const I32_REF_U64_UNION: u64 = unsafe { Nonsense { int_32_ref: &3 }.uin
4750
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered pointer to alloc18, but expected initialized plain (non-pointer) bytes
4851
|
4952
= 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.
53+
= note: the raw bytes of the constant (size: 8, align: 8) {
54+
╾───────alloc18───────╼ │ ╾──────╼
55+
}
5056

5157
error[E0080]: it is undefined behavior to use this value
5258
--> $DIR/const-pointer-values-in-various-types.rs:43:5
@@ -55,6 +61,9 @@ LL | const I32_REF_U128_UNION: u128 = unsafe { Nonsense { int_32_ref: &3 }.u
5561
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered uninitialized bytes, but expected initialized plain (non-pointer) bytes
5662
|
5763
= 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.
64+
= note: the raw bytes of the constant (size: 16, align: 8) {
65+
__ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ │ ░░░░░░░░░░░░░░░░
66+
}
5867

5968
error: any use of this value will cause an error
6069
--> $DIR/const-pointer-values-in-various-types.rs:46:43
@@ -96,6 +105,9 @@ LL | const I32_REF_I64_UNION: i64 = unsafe { Nonsense { int_32_ref: &3 }.int
96105
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered pointer to alloc38, but expected initialized plain (non-pointer) bytes
97106
|
98107
= 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.
108+
= note: the raw bytes of the constant (size: 8, align: 8) {
109+
╾───────alloc38───────╼ │ ╾──────╼
110+
}
99111

100112
error[E0080]: it is undefined behavior to use this value
101113
--> $DIR/const-pointer-values-in-various-types.rs:61:5
@@ -104,6 +116,9 @@ LL | const I32_REF_I128_UNION: i128 = unsafe { Nonsense { int_32_ref: &3 }.i
104116
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered uninitialized bytes, but expected initialized plain (non-pointer) bytes
105117
|
106118
= 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.
119+
= note: the raw bytes of the constant (size: 16, align: 8) {
120+
__ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ │ ░░░░░░░░░░░░░░░░
121+
}
107122

108123
error: any use of this value will cause an error
109124
--> $DIR/const-pointer-values-in-various-types.rs:64:45
@@ -123,6 +138,9 @@ LL | const I32_REF_F64_UNION: f64 = unsafe { Nonsense { int_32_ref: &3 }.flo
123138
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered pointer to alloc50, but expected initialized plain (non-pointer) bytes
124139
|
125140
= 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.
141+
= note: the raw bytes of the constant (size: 8, align: 8) {
142+
╾───────alloc50───────╼ │ ╾──────╼
143+
}
126144

127145
error: any use of this value will cause an error
128146
--> $DIR/const-pointer-values-in-various-types.rs:71:47
@@ -186,6 +204,9 @@ LL | const STR_U64_UNION: u64 = unsafe { Nonsense { stringy: "3" }.uint_64 }
186204
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered pointer to alloc71, but expected initialized plain (non-pointer) bytes
187205
|
188206
= 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.
207+
= note: the raw bytes of the constant (size: 8, align: 8) {
208+
╾───────alloc71───────╼ │ ╾──────╼
209+
}
189210

190211
error: any use of this value will cause an error
191212
--> $DIR/const-pointer-values-in-various-types.rs:94:43
@@ -238,6 +259,9 @@ LL | const STR_I64_UNION: i64 = unsafe { Nonsense { stringy: "3" }.int_64 };
238259
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered pointer to alloc86, but expected initialized plain (non-pointer) bytes
239260
|
240261
= 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.
262+
= note: the raw bytes of the constant (size: 8, align: 8) {
263+
╾───────alloc86───────╼ │ ╾──────╼
264+
}
241265

242266
error: any use of this value will cause an error
243267
--> $DIR/const-pointer-values-in-various-types.rs:113:43
@@ -268,6 +292,9 @@ LL | const STR_F64_UNION: f64 = unsafe { Nonsense { stringy: "3" }.float_64
268292
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered pointer to alloc95, but expected initialized plain (non-pointer) bytes
269293
|
270294
= 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.
295+
= note: the raw bytes of the constant (size: 8, align: 8) {
296+
╾───────alloc95───────╼ │ ╾──────╼
297+
}
271298

272299
error: any use of this value will cause an error
273300
--> $DIR/const-pointer-values-in-various-types.rs:124:43

src/test/ui/consts/const-eval/heap/alloc_intrinsic_uninit.stderr

+3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ LL | const BAR: &i32 = unsafe { &*(intrinsics::const_allocate(4, 4) as *mut i32)
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered uninitialized bytes at .<deref>, 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: 8, align: 8) {
9+
╾───────alloc1────────╼ │ ╾──────╼
10+
}
811

912
error: aborting due to previous error
1013

src/test/ui/consts/const-eval/ref_to_int_match.stderr

+3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ LL | const BAR: Int = unsafe { Foo { r: &42 }.f };
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered pointer to alloc2, 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: 8, align: 8) {
9+
╾───────alloc2────────╼ │ ╾──────╼
10+
}
811

912
error: could not evaluate constant pattern
1013
--> $DIR/ref_to_int_match.rs:7:14

src/test/ui/consts/const-eval/transmute-const.stderr

+3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ LL | static FOO: bool = unsafe { mem::transmute(3u8) };
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered 0x03, but expected a boolean
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: 1, align: 1) {
9+
03 │ .
10+
}
811

912
error: aborting due to previous error
1013

0 commit comments

Comments
 (0)