Skip to content

Commit 77c2834

Browse files
committed
Also test for undef in enum discriminant
The error message is sub-par, but fixing that requries moving ScalarMaybeUndef to librustc which would conflict badly with another PR that is in flight.
1 parent fa01e04 commit 77c2834

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,15 @@ enum Enum2 {
3333
union TransmuteEnum2 {
3434
a: usize,
3535
b: Enum2,
36+
c: (),
3637
}
3738
const BAD_ENUM2 : Enum2 = unsafe { TransmuteEnum2 { a: 0 }.b };
3839
//~^ ERROR is undefined behavior
3940

41+
// Undef enum discriminant. In an arry to avoid `Scalar` layout.
42+
const BAD_ENUM3 : [Enum2; 2] = [unsafe { TransmuteEnum2 { c: () }.b }; 2];
43+
//~^ ERROR is undefined behavior
44+
4045
// Invalid enum field content (mostly to test printing of apths for enum tuple
4146
// variants and tuples).
4247
union TransmuteChar {

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,29 @@ LL | const BAD_ENUM: Enum = unsafe { TransmuteEnum { a: &1 }.b };
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-enum.rs:37:1
10+
--> $DIR/ub-enum.rs:38:1
1111
|
1212
LL | const BAD_ENUM2 : Enum2 = unsafe { TransmuteEnum2 { a: 0 }.b };
1313
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered 0, but expected a valid enum discriminant
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-enum.rs:47:1
18+
--> $DIR/ub-enum.rs:42:1
19+
|
20+
LL | const BAD_ENUM3 : [Enum2; 2] = [unsafe { TransmuteEnum2 { c: () }.b }; 2];
21+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ attempted to read undefined bytes
22+
|
23+
= 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
24+
25+
error[E0080]: it is undefined behavior to use this value
26+
--> $DIR/ub-enum.rs:52:1
1927
|
2028
LL | const BAD_ENUM_CHAR : Option<(char, char)> = Some(('x', unsafe { TransmuteChar { a: !0 }.b }));
2129
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered 4294967295 at .Some.0.1, but expected something in the range 0..=1114111
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`.

0 commit comments

Comments
 (0)