Skip to content

Commit d038fb2

Browse files
authored
Rollup merge of #58658 - pmccarter:align_msg, r=matthewjasper
Add expected/provided byte alignments to validation error message Fixes #58617
2 parents 2db0e48 + d0c110f commit d038fb2

File tree

3 files changed

+12
-8
lines changed

3 files changed

+12
-8
lines changed

src/librustc_mir/interpret/validity.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -357,8 +357,10 @@ impl<'rt, 'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>>
357357
match err.kind {
358358
EvalErrorKind::InvalidNullPointerUsage =>
359359
return validation_failure!("NULL reference", self.path),
360-
EvalErrorKind::AlignmentCheckFailed { .. } =>
361-
return validation_failure!("unaligned reference", self.path),
360+
EvalErrorKind::AlignmentCheckFailed { required, has } =>
361+
return validation_failure!(format!("unaligned reference \
362+
(required {} byte alignment but found {})",
363+
required.bytes(), has.bytes()), self.path),
362364
_ =>
363365
return validation_failure!(
364366
"dangling (out-of-bounds) reference (might be NULL at \

src/test/ui/consts/const-eval/ub-ref.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
// ignore-tidy-linelength
12
#![feature(const_transmute)]
23
#![allow(const_err)] // make sure we cannot allow away the errors tested here
34

45
use std::mem;
56

67
const UNALIGNED: &u16 = unsafe { mem::transmute(&[0u8; 4]) };
78
//~^ ERROR it is undefined behavior to use this value
9+
//~^^ type validation failed: encountered unaligned reference (required 2 byte alignment but found 1)
810

911
const NULL: &u16 = unsafe { mem::transmute(0usize) };
1012
//~^ ERROR it is undefined behavior to use this value

src/test/ui/consts/const-eval/ub-ref.stderr

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,37 @@
11
error[E0080]: it is undefined behavior to use this value
2-
--> $DIR/ub-ref.rs:6:1
2+
--> $DIR/ub-ref.rs:7:1
33
|
44
LL | const UNALIGNED: &u16 = unsafe { mem::transmute(&[0u8; 4]) };
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered unaligned reference
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered unaligned reference (required 2 byte alignment but found 1)
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 rust compiler repository if you believe it should not be considered undefined behavior
88

99
error[E0080]: it is undefined behavior to use this value
10-
--> $DIR/ub-ref.rs:9:1
10+
--> $DIR/ub-ref.rs:11:1
1111
|
1212
LL | const NULL: &u16 = unsafe { mem::transmute(0usize) };
1313
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered 0, but expected something greater or equal to 1
1414
|
1515
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rust compiler repository if you believe it should not be considered undefined behavior
1616

1717
error[E0080]: it is undefined behavior to use this value
18-
--> $DIR/ub-ref.rs:12:1
18+
--> $DIR/ub-ref.rs:14:1
1919
|
2020
LL | const REF_AS_USIZE: usize = unsafe { mem::transmute(&0) };
2121
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered a pointer, but expected initialized plain (non-pointer) bytes
2222
|
2323
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rust compiler repository if you believe it should not be considered undefined behavior
2424

2525
error[E0080]: it is undefined behavior to use this value
26-
--> $DIR/ub-ref.rs:15:1
26+
--> $DIR/ub-ref.rs:17:1
2727
|
2828
LL | const REF_AS_USIZE_SLICE: &[usize] = &[unsafe { mem::transmute(&0) }];
2929
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered a pointer at .<deref>, but expected plain (non-pointer) bytes
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 rust compiler repository if you believe it should not be considered undefined behavior
3232

3333
error[E0080]: it is undefined behavior to use this value
34-
--> $DIR/ub-ref.rs:18:1
34+
--> $DIR/ub-ref.rs:20:1
3535
|
3636
LL | const USIZE_AS_REF: &'static u8 = unsafe { mem::transmute(1337usize) };
3737
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered integer pointer in non-ZST reference

0 commit comments

Comments
 (0)