Skip to content

Commit 61ae06f

Browse files
committed
unify handling of thin and fat pointers by moving primitive type handling out of aggregate handling
Also, make enum variant handling a bit nicer
1 parent 0436a82 commit 61ae06f

File tree

6 files changed

+225
-202
lines changed

6 files changed

+225
-202
lines changed

src/librustc_mir/interpret/validity.rs

+206-194
Large diffs are not rendered by default.

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,23 @@ error[E0080]: this constant likely exhibits undefined behavior
22
--> $DIR/ub-nonnull.rs:17:1
33
|
44
LL | const NULL_PTR: NonNull<u8> = unsafe { mem::transmute(0usize) };
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered 0, but expected something in the range 1..=18446744073709551615
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered 0, but expected something greater or equal to 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]: this constant likely exhibits undefined behavior
1010
--> $DIR/ub-nonnull.rs:20:1
1111
|
1212
LL | const NULL_U8: NonZeroU8 = unsafe { mem::transmute(0u8) };
13-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered 0, but expected something in the range 1..=255
13+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 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]: this constant likely exhibits undefined behavior
1818
--> $DIR/ub-nonnull.rs:22:1
1919
|
2020
LL | const NULL_USIZE: NonZeroUsize = unsafe { mem::transmute(0usize) };
21-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered 0, but expected something in the range 1..=18446744073709551615
21+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered 0, but expected something greater or equal to 1
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

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

+3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ use std::mem;
1515
const UNALIGNED: &u16 = unsafe { mem::transmute(&[0u8; 4]) };
1616
//~^ ERROR this constant likely exhibits undefined behavior
1717

18+
const NULL: &u16 = unsafe { mem::transmute(0usize) };
19+
//~^ ERROR this constant likely exhibits undefined behavior
20+
1821
const REF_AS_USIZE: usize = unsafe { mem::transmute(&0) };
1922
//~^ ERROR this constant likely exhibits undefined behavior
2023

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

+10-2
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,27 @@ LL | const UNALIGNED: &u16 = unsafe { mem::transmute(&[0u8; 4]) };
99
error[E0080]: this constant likely exhibits undefined behavior
1010
--> $DIR/ub-ref.rs:18:1
1111
|
12+
LL | const NULL: &u16 = unsafe { mem::transmute(0usize) };
13+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered 0, but expected something greater or equal to 1
14+
|
15+
= 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
16+
17+
error[E0080]: this constant likely exhibits undefined behavior
18+
--> $DIR/ub-ref.rs:21:1
19+
|
1220
LL | const REF_AS_USIZE: usize = unsafe { mem::transmute(&0) };
1321
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered a pointer, but expected initialized plain bits
1422
|
1523
= 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
1624

1725
error[E0080]: this constant likely exhibits undefined behavior
18-
--> $DIR/ub-ref.rs:21:1
26+
--> $DIR/ub-ref.rs:24:1
1927
|
2028
LL | const USIZE_AS_REF: &'static u8 = unsafe { mem::transmute(1337usize) };
2129
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered integer pointer in non-ZST reference
2230
|
2331
= 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
2432

25-
error: aborting due to 3 previous errors
33+
error: aborting due to 4 previous errors
2634

2735
For more information about this error, try `rustc --explain E0080`.

src/test/ui/consts/const-eval/union-ice.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ LL | / const FIELD_PATH: Struct = Struct { //~ ERROR this constant likely exhibi
1313
LL | | a: 42,
1414
LL | | b: unsafe { UNION.field3 },
1515
LL | | };
16-
| |__^ type validation failed: encountered uninitialized bytes at .b, but expected something in the range 0..=18446744073709551615
16+
| |__^ type validation failed: encountered uninitialized bytes at .b, but expected initialized plain bits
1717
|
1818
= 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
1919

src/test/ui/consts/const-eval/union-ub-fat-ptr.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0080]: this constant likely exhibits undefined behavior
22
--> $DIR/union-ub-fat-ptr.rs:87:1
33
|
44
LL | const B: &str = unsafe { SliceTransmute { repr: SliceRepr { ptr: &42, len: 999 } }.str};
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered dangling reference (not entirely in bounds)
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered dangling (not entirely in bounds) reference
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

@@ -26,7 +26,7 @@ error[E0080]: this constant likely exhibits undefined behavior
2626
--> $DIR/union-ub-fat-ptr.rs:99:1
2727
|
2828
LL | const B2: &[u8] = unsafe { SliceTransmute { repr: SliceRepr { ptr: &42, len: 999 } }.slice};
29-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered dangling reference (not entirely in bounds)
29+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered dangling (not entirely in bounds) reference
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

0 commit comments

Comments
 (0)