Skip to content

Commit 9834674

Browse files
authored
Rollup merge of #96272 - tmiasko:validate-uninhabited, r=RalfJung
Update `validate_uninhabited_zsts.rs` test after MIR building changes to ensure that it still tests validation, instead of failing earlier on during evaluation. r? `@RalfJung`
2 parents 41ef767 + 9ff5b7e commit 9834674

File tree

3 files changed

+55
-34
lines changed

3 files changed

+55
-34
lines changed

src/test/ui/consts/const-eval/validate_uninhabited_zsts.32bit.stderr

+21-14
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,17 @@ LL | unsafe { std::mem::transmute(()) }
77
| transmuting to uninhabited type
88
| inside `foo` at $DIR/validate_uninhabited_zsts.rs:4:14
99
...
10-
LL | const FOO: [Empty; 3] = [foo(); 3];
11-
| ----- inside `FOO` at $DIR/validate_uninhabited_zsts.rs:13:26
10+
LL | const FOO: [empty::Empty; 3] = [foo(); 3];
11+
| ----- inside `FOO` at $DIR/validate_uninhabited_zsts.rs:20:33
1212

13-
error[E0080]: evaluation of constant value failed
14-
--> $DIR/validate_uninhabited_zsts.rs:16:35
13+
error[E0080]: it is undefined behavior to use this value
14+
--> $DIR/validate_uninhabited_zsts.rs:23:1
15+
|
16+
LL | const BAR: [empty::Empty; 3] = [unsafe { std::mem::transmute(()) }; 3];
17+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed at [0].0: encountered a value of uninhabited type empty::Void
1518
|
16-
LL | const BAR: [Empty; 3] = [unsafe { std::mem::transmute(()) }; 3];
17-
| ^^^^^^^^^^^^^^^^^^^^^^^ transmuting to uninhabited type
19+
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior.
20+
= note: the raw bytes of the constant (size: 0, align: 1) {}
1821

1922
warning: the type `!` does not permit zero-initialization
2023
--> $DIR/validate_uninhabited_zsts.rs:4:14
@@ -28,16 +31,20 @@ LL | unsafe { std::mem::transmute(()) }
2831
= note: `#[warn(invalid_value)]` on by default
2932
= note: the `!` type has no valid value
3033

31-
warning: the type `Empty` does not permit zero-initialization
32-
--> $DIR/validate_uninhabited_zsts.rs:16:35
34+
warning: the type `empty::Empty` does not permit zero-initialization
35+
--> $DIR/validate_uninhabited_zsts.rs:23:42
36+
|
37+
LL | const BAR: [empty::Empty; 3] = [unsafe { std::mem::transmute(()) }; 3];
38+
| ^^^^^^^^^^^^^^^^^^^^^^^
39+
| |
40+
| this code causes undefined behavior when executed
41+
| help: use `MaybeUninit<T>` instead, and only call `assume_init` after initialization is done
3342
|
34-
LL | const BAR: [Empty; 3] = [unsafe { std::mem::transmute(()) }; 3];
35-
| ^^^^^^^^^^^^^^^^^^^^^^^
36-
| |
37-
| this code causes undefined behavior when executed
38-
| help: use `MaybeUninit<T>` instead, and only call `assume_init` after initialization is done
43+
note: enums with no variants have no valid value (in this struct field)
44+
--> $DIR/validate_uninhabited_zsts.rs:16:22
3945
|
40-
= note: enums with no variants have no valid value
46+
LL | pub struct Empty(Void);
47+
| ^^^^
4148

4249
error: aborting due to 2 previous errors; 2 warnings emitted
4350

src/test/ui/consts/const-eval/validate_uninhabited_zsts.64bit.stderr

+21-14
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,17 @@ LL | unsafe { std::mem::transmute(()) }
77
| transmuting to uninhabited type
88
| inside `foo` at $DIR/validate_uninhabited_zsts.rs:4:14
99
...
10-
LL | const FOO: [Empty; 3] = [foo(); 3];
11-
| ----- inside `FOO` at $DIR/validate_uninhabited_zsts.rs:13:26
10+
LL | const FOO: [empty::Empty; 3] = [foo(); 3];
11+
| ----- inside `FOO` at $DIR/validate_uninhabited_zsts.rs:20:33
1212

13-
error[E0080]: evaluation of constant value failed
14-
--> $DIR/validate_uninhabited_zsts.rs:16:35
13+
error[E0080]: it is undefined behavior to use this value
14+
--> $DIR/validate_uninhabited_zsts.rs:23:1
15+
|
16+
LL | const BAR: [empty::Empty; 3] = [unsafe { std::mem::transmute(()) }; 3];
17+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed at [0].0: encountered a value of uninhabited type empty::Void
1518
|
16-
LL | const BAR: [Empty; 3] = [unsafe { std::mem::transmute(()) }; 3];
17-
| ^^^^^^^^^^^^^^^^^^^^^^^ transmuting to uninhabited type
19+
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior.
20+
= note: the raw bytes of the constant (size: 0, align: 1) {}
1821

1922
warning: the type `!` does not permit zero-initialization
2023
--> $DIR/validate_uninhabited_zsts.rs:4:14
@@ -28,16 +31,20 @@ LL | unsafe { std::mem::transmute(()) }
2831
= note: `#[warn(invalid_value)]` on by default
2932
= note: the `!` type has no valid value
3033

31-
warning: the type `Empty` does not permit zero-initialization
32-
--> $DIR/validate_uninhabited_zsts.rs:16:35
34+
warning: the type `empty::Empty` does not permit zero-initialization
35+
--> $DIR/validate_uninhabited_zsts.rs:23:42
36+
|
37+
LL | const BAR: [empty::Empty; 3] = [unsafe { std::mem::transmute(()) }; 3];
38+
| ^^^^^^^^^^^^^^^^^^^^^^^
39+
| |
40+
| this code causes undefined behavior when executed
41+
| help: use `MaybeUninit<T>` instead, and only call `assume_init` after initialization is done
3342
|
34-
LL | const BAR: [Empty; 3] = [unsafe { std::mem::transmute(()) }; 3];
35-
| ^^^^^^^^^^^^^^^^^^^^^^^
36-
| |
37-
| this code causes undefined behavior when executed
38-
| help: use `MaybeUninit<T>` instead, and only call `assume_init` after initialization is done
43+
note: enums with no variants have no valid value (in this struct field)
44+
--> $DIR/validate_uninhabited_zsts.rs:16:22
3945
|
40-
= note: enums with no variants have no valid value
46+
LL | pub struct Empty(Void);
47+
| ^^^^
4148

4249
error: aborting due to 2 previous errors; 2 warnings emitted
4350

src/test/ui/consts/const-eval/validate_uninhabited_zsts.rs

+13-6
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,23 @@ const fn foo() -> ! {
66
//~| WARN the type `!` does not permit zero-initialization [invalid_value]
77
}
88

9-
#[derive(Clone, Copy)]
10-
enum Empty { }
9+
// Type defined in a submodule, so that it is not "visibly"
10+
// uninhabited (which would change interpreter behavior).
11+
pub mod empty {
12+
#[derive(Clone, Copy)]
13+
enum Void {}
14+
15+
#[derive(Clone, Copy)]
16+
pub struct Empty(Void);
17+
}
1118

1219
#[warn(const_err)]
13-
const FOO: [Empty; 3] = [foo(); 3];
20+
const FOO: [empty::Empty; 3] = [foo(); 3];
1421

1522
#[warn(const_err)]
16-
const BAR: [Empty; 3] = [unsafe { std::mem::transmute(()) }; 3];
17-
//~^ ERROR evaluation of constant value failed
18-
//~| WARN the type `Empty` does not permit zero-initialization
23+
const BAR: [empty::Empty; 3] = [unsafe { std::mem::transmute(()) }; 3];
24+
//~^ ERROR it is undefined behavior to use this value
25+
//~| WARN the type `empty::Empty` does not permit zero-initialization
1926

2027
fn main() {
2128
FOO;

0 commit comments

Comments
 (0)