Skip to content

Commit 5f27a25

Browse files
committed
Invalid byte alignment expected/provided in message #58617
1 parent eac0908 commit 5f27a25

File tree

3 files changed

+10
-7
lines changed

3 files changed

+10
-7
lines changed

src/librustc_mir/interpret/validity.rs

Lines changed: 4 additions & 2 deletions
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 {} 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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use std::mem;
55

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

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

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,36 @@ error[E0080]: it is undefined behavior to use this value
22
--> $DIR/ub-ref.rs:6: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 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:10: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:13: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:16: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:19: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)