Skip to content

Commit 280d697

Browse files
authored
Rollup merge of #114372 - RalfJung:const-pointer-as-int, r=oli-obk
const validation: point at where we found a pointer but expected an integer Instead of validation just printing "unable to turn pointer into bytes", make this a regular validation error that says where in the value the bad pointer was found. Also distinguish "expected integer, got pointer" from "expected pointer, got partial pointer or mix of pointers". To avoid duplicating things too much I refactored the diagnostics for validity a bit, so that "got uninit, expected X" and "got pointer, expected X" can share the "X" part. Also all the errors emitted for validation are now grouped under `const_eval_validation` so that they are in a single group in the ftl file. r? `@oli-obk`
2 parents 4027adb + 94969d1 commit 280d697

File tree

3 files changed

+7
-5
lines changed

3 files changed

+7
-5
lines changed

src/diagnostics.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,8 @@ pub fn report_error<'tcx, 'mir>(
273273
} else {
274274
#[rustfmt::skip]
275275
let title = match e.kind() {
276+
UndefinedBehavior(UndefinedBehaviorInfo::ValidationError(e)) if matches!(e.kind, ValidationErrorKind::PointerAsInt { .. } | ValidationErrorKind::PartialPointer) =>
277+
bug!("This validation error should be impossible in Miri: {:?}", e.kind),
276278
UndefinedBehavior(_) =>
277279
"Undefined Behavior",
278280
ResourceExhaustion(_) =>
@@ -377,7 +379,7 @@ pub fn report_error<'tcx, 'mir>(
377379
if let Some((alloc_id, access)) = extra {
378380
eprintln!(
379381
"Uninitialized memory occurred at {alloc_id:?}{range:?}, in this allocation:",
380-
range = access.uninit,
382+
range = access.bad,
381383
);
382384
eprintln!("{:?}", ecx.dump_alloc(alloc_id));
383385
}

tests/fail/validity/uninit_float.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
error: Undefined Behavior: constructing invalid value at .value[0]: encountered uninitialized bytes
1+
error: Undefined Behavior: constructing invalid value at .value[0]: encountered uninitialized memory, but expected a floating point number
22
--> $DIR/uninit_float.rs:LL:CC
33
|
44
LL | let _val: [f32; 1] = unsafe { std::mem::uninitialized() };
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value at .value[0]: encountered uninitialized bytes
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value at .value[0]: encountered uninitialized memory, but expected a floating point number
66
|
77
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
88
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information

tests/fail/validity/uninit_integer.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
error: Undefined Behavior: constructing invalid value at .value[0]: encountered uninitialized bytes
1+
error: Undefined Behavior: constructing invalid value at .value[0]: encountered uninitialized memory, but expected an integer
22
--> $DIR/uninit_integer.rs:LL:CC
33
|
44
LL | let _val = unsafe { std::mem::MaybeUninit::<[usize; 1]>::uninit().assume_init() };
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value at .value[0]: encountered uninitialized bytes
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value at .value[0]: encountered uninitialized memory, but expected an integer
66
|
77
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
88
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information

0 commit comments

Comments
 (0)