Skip to content

Commit 34f9501

Browse files
authored
Rollup 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 3f3ac16 + 25883aa commit 34f9501

29 files changed

+322
-30
lines changed

compiler/rustc_mir/src/const_eval/eval_queries.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use crate::interpret::{
66
ScalarMaybeUninit, StackPopCleanup,
77
};
88

9+
use crate::util::pretty::display_allocation;
910
use rustc_hir::def::DefKind;
1011
use rustc_middle::mir;
1112
use rustc_middle::mir::interpret::ErrorHandled;
@@ -192,7 +193,7 @@ fn validate_and_turn_into_const<'tcx>(
192193
let is_static = tcx.is_static(def_id);
193194
let ecx = mk_eval_cx(tcx, tcx.def_span(key.value.instance.def_id()), key.param_env, is_static);
194195
let val = (|| {
195-
let mplace = ecx.raw_const_to_mplace(constant)?;
196+
let mplace = ecx.raw_const_to_mplace(&constant)?;
196197

197198
// FIXME do not validate promoteds until a decision on
198199
// https://github.com/rust-lang/rust/issues/67465 is made
@@ -225,6 +226,13 @@ fn validate_and_turn_into_const<'tcx>(
225226
let err = ConstEvalErr::new(&ecx, error, None);
226227
err.struct_error(ecx.tcx, "it is undefined behavior to use this value", |mut diag| {
227228
diag.note(note_on_undefined_behavior_error());
229+
diag.note(&format!(
230+
"the raw bytes of the constant ({}",
231+
display_allocation(
232+
*ecx.tcx,
233+
ecx.tcx.global_alloc(constant.alloc_id).unwrap_memory()
234+
)
235+
));
228236
diag.emit();
229237
})
230238
})

compiler/rustc_mir/src/interpret/eval_context.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -922,7 +922,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
922922
// that problem, but we never run validation to show an error. Can we ensure
923923
// this does not happen?
924924
let val = self.tcx.const_eval_raw(param_env.and(gid))?;
925-
self.raw_const_to_mplace(val)
925+
self.raw_const_to_mplace(&val)
926926
}
927927

928928
#[must_use]

compiler/rustc_mir/src/interpret/place.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1120,7 +1120,7 @@ where
11201120

11211121
pub fn raw_const_to_mplace(
11221122
&self,
1123-
raw: RawConst<'tcx>,
1123+
raw: &RawConst<'tcx>,
11241124
) -> InterpResult<'tcx, MPlaceTy<'tcx, M::PointerTag>> {
11251125
// This must be an allocation in `tcx`
11261126
let _ = self.tcx.global_alloc(raw.alloc_id);

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
@@ -39,6 +42,9 @@ LL | const I32_REF_U64_UNION: u64 = unsafe { Nonsense { int_32_ref: &3 }.uin
3942
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered pointer to alloc22, but expected initialized plain (non-pointer) bytes
4043
|
4144
= 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.
45+
= note: the raw bytes of the constant (size: 8, align: 8) {
46+
╾───────alloc22───────╼ │ ╾──────╼
47+
}
4248

4349
error[E0080]: it is undefined behavior to use this value
4450
--> $DIR/const-pointer-values-in-various-types.rs:40:5
@@ -47,6 +53,9 @@ LL | const I32_REF_U128_UNION: u128 = unsafe { Nonsense { int_32_ref: &3 }.u
4753
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered uninitialized bytes, but expected initialized plain (non-pointer) bytes
4854
|
4955
= 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.
56+
= note: the raw bytes of the constant (size: 16, align: 8) {
57+
__ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ │ ░░░░░░░░░░░░░░░░
58+
}
5059

5160
error: any use of this value will cause an error
5261
--> $DIR/const-pointer-values-in-various-types.rs:43:43
@@ -79,6 +88,9 @@ LL | const I32_REF_I64_UNION: i64 = unsafe { Nonsense { int_32_ref: &3 }.int
7988
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered pointer to alloc47, but expected initialized plain (non-pointer) bytes
8089
|
8190
= 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.
91+
= note: the raw bytes of the constant (size: 8, align: 8) {
92+
╾───────alloc47───────╼ │ ╾──────╼
93+
}
8294

8395
error[E0080]: it is undefined behavior to use this value
8496
--> $DIR/const-pointer-values-in-various-types.rs:55:5
@@ -87,6 +99,9 @@ LL | const I32_REF_I128_UNION: i128 = unsafe { Nonsense { int_32_ref: &3 }.i
8799
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered uninitialized bytes, but expected initialized plain (non-pointer) bytes
88100
|
89101
= 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.
102+
= note: the raw bytes of the constant (size: 16, align: 8) {
103+
__ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ │ ░░░░░░░░░░░░░░░░
104+
}
90105

91106
error: any use of this value will cause an error
92107
--> $DIR/const-pointer-values-in-various-types.rs:58:45
@@ -103,6 +118,9 @@ LL | const I32_REF_F64_UNION: f64 = unsafe { Nonsense { int_32_ref: &3 }.flo
103118
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered pointer to alloc62, but expected initialized plain (non-pointer) bytes
104119
|
105120
= 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.
121+
= note: the raw bytes of the constant (size: 8, align: 8) {
122+
╾───────alloc62───────╼ │ ╾──────╼
123+
}
106124

107125
error: any use of this value will cause an error
108126
--> $DIR/const-pointer-values-in-various-types.rs:64:47
@@ -151,6 +169,9 @@ LL | const STR_U64_UNION: u64 = unsafe { Nonsense { stringy: "3" }.uint_64 }
151169
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered pointer to alloc86, but expected initialized plain (non-pointer) bytes
152170
|
153171
= 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.
172+
= note: the raw bytes of the constant (size: 8, align: 8) {
173+
╾───────alloc86───────╼ │ ╾──────╼
174+
}
154175

155176
error: any use of this value will cause an error
156177
--> $DIR/const-pointer-values-in-various-types.rs:82:43
@@ -191,6 +212,9 @@ LL | const STR_I64_UNION: i64 = unsafe { Nonsense { stringy: "3" }.int_64 };
191212
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered pointer to alloc101, but expected initialized plain (non-pointer) bytes
192213
|
193214
= 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.
215+
= note: the raw bytes of the constant (size: 8, align: 8) {
216+
╾──────alloc101───────╼ │ ╾──────╼
217+
}
194218

195219
error: any use of this value will cause an error
196220
--> $DIR/const-pointer-values-in-various-types.rs:97:43
@@ -215,6 +239,9 @@ LL | const STR_F64_UNION: f64 = unsafe { Nonsense { stringy: "3" }.float_64
215239
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered pointer to alloc110, but expected initialized plain (non-pointer) bytes
216240
|
217241
= 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.
242+
= note: the raw bytes of the constant (size: 8, align: 8) {
243+
╾──────alloc110───────╼ │ ╾──────╼
244+
}
218245

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

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

+6
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ LL | | )};
88
| |___^ type validation failed: encountered 0x05 at .1.<deref>.<enum-tag>, but expected a valid enum tag
99
|
1010
= 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.
11+
= note: the raw bytes of the constant (size: 16, align: 8) {
12+
╾───────alloc4────────╼ ╾───────alloc4────────╼ │ ╾──────╼╾──────╼
13+
}
1114

1215
error[E0080]: it is undefined behavior to use this value
1316
--> $DIR/double_check2.rs:20:1
@@ -16,6 +19,9 @@ LL | static FOO2: (&Foo, &Bar) = unsafe {(std::mem::transmute(&BAR), std::mem::t
1619
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered 0x05 at .1.<deref>.<enum-tag>, but expected a valid enum tag
1720
|
1821
= 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.
22+
= note: the raw bytes of the constant (size: 16, align: 8) {
23+
╾───────alloc4────────╼ ╾───────alloc4────────╼ │ ╾──────╼╾──────╼
24+
}
1925

2026
error: aborting due to 2 previous errors
2127

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)