Skip to content

Commit ec57c60

Browse files
committed
Auto merge of #86194 - RalfJung:const-ub-hard-error, r=oli-obk
make UB during CTFE a hard error This is a next step for #71800. `const_err` has been a future-incompatibility lint for 4 months now since #80394 (and err-by-default for many years before that), so I think we could try making it a proper hard error at least in some situations. I didn't yet adjust the tests, since I first want to gauge the fall-out via crater. Cc `@rust-lang/wg-const-eval`
2 parents ce1d561 + 7475661 commit ec57c60

25 files changed

+336
-859
lines changed

compiler/rustc_middle/src/mir/interpret/error.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -525,6 +525,7 @@ impl InterpError<'_> {
525525
use InterpError::*;
526526
match *self {
527527
MachineStop(ref err) => err.is_hard_err(),
528+
InterpError::UndefinedBehavior(_) => true,
528529
_ => false,
529530
}
530531
}

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@ use std::mem;
55
// Make sure we error with the right kind of error on a too large slice.
66
const TEST: () = { unsafe {
77
let slice: *const [u8] = mem::transmute((1usize, usize::MAX));
8-
let _val = &*slice; //~ ERROR: any use of this value will cause an error
8+
let _val = &*slice; //~ ERROR: evaluation of constant value failed
99
//~| slice is bigger than largest supported object
10-
//~| WARN this was previously accepted by the compiler but is being phased out
1110
} };
1211

1312
fn main() {}
Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,9 @@
1-
error: any use of this value will cause an error
1+
error[E0080]: evaluation of constant value failed
22
--> $DIR/dangling.rs:8:16
33
|
4-
LL | / const TEST: () = { unsafe {
5-
LL | | let slice: *const [u8] = mem::transmute((1usize, usize::MAX));
6-
LL | | let _val = &*slice;
7-
| | ^^^^^^^ invalid metadata in wide pointer: slice is bigger than largest supported object
8-
LL | |
9-
LL | |
10-
LL | | } };
11-
| |____-
12-
|
13-
= note: `#[deny(const_err)]` on by default
14-
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
15-
= note: for more information, see issue #71800 <https://github.com/rust-lang/rust/issues/71800>
4+
LL | let _val = &*slice;
5+
| ^^^^^^^ invalid metadata in wide pointer: slice is bigger than largest supported object
166

177
error: aborting due to previous error
188

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

src/test/ui/consts/const-eval/heap/alloc_intrinsic_errors.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ const FOO: i32 = foo();
88
const fn foo() -> i32 {
99
unsafe {
1010
let _ = intrinsics::const_allocate(4, 3) as * mut i32;
11-
//~^ error: any use of this value will cause an error [const_err]
12-
//~| WARN this was previously accepted by the compiler but is being phased out
11+
//~^ error: evaluation of constant value failed
1312
}
1413
1
1514

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,15 @@
1-
error: any use of this value will cause an error
1+
error[E0080]: evaluation of constant value failed
22
--> $DIR/alloc_intrinsic_errors.rs:10:17
33
|
44
LL | const FOO: i32 = foo();
5-
| -----------------------
5+
| ----- inside `FOO` at $DIR/alloc_intrinsic_errors.rs:7:18
66
...
77
LL | let _ = intrinsics::const_allocate(4, 3) as * mut i32;
88
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
99
| |
1010
| align has to be a power of 2, `3` is not a power of 2
1111
| inside `foo` at $DIR/alloc_intrinsic_errors.rs:10:17
12-
| inside `FOO` at $DIR/alloc_intrinsic_errors.rs:7:18
13-
|
14-
= note: `#[deny(const_err)]` on by default
15-
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
16-
= note: for more information, see issue #71800 <https://github.com/rust-lang/rust/issues/71800>
1712

1813
error: aborting due to previous error
1914

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

src/test/ui/consts/const-eval/issue-49296.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ const fn wat(x: u64) -> &'static u64 {
1717
unsafe { transmute(&x) }
1818
}
1919
const X: u64 = *wat(42);
20-
//~^ ERROR any use of this value will cause an error
21-
//~| WARN this was previously accepted by the compiler but is being phased out
20+
//~^ ERROR evaluation of constant value failed
2221

2322
fn main() {
2423
println!("{}", X);
Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,9 @@
1-
error: any use of this value will cause an error
1+
error[E0080]: evaluation of constant value failed
22
--> $DIR/issue-49296.rs:19:16
33
|
44
LL | const X: u64 = *wat(42);
5-
| ---------------^^^^^^^^-
6-
| |
7-
| pointer to alloc1 was dereferenced after this allocation got freed
8-
|
9-
= note: `#[deny(const_err)]` on by default
10-
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
11-
= note: for more information, see issue #71800 <https://github.com/rust-lang/rust/issues/71800>
5+
| ^^^^^^^^ pointer to alloc1 was dereferenced after this allocation got freed
126

137
error: aborting due to previous error
148

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

src/test/ui/consts/const-eval/ub-incorrect-vtable.32bit.stderr

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,17 @@
1-
error: any use of this value will cause an error
1+
error[E0080]: evaluation of constant value failed
22
--> $DIR/ub-incorrect-vtable.rs:19:14
33
|
4-
LL | / const INVALID_VTABLE_ALIGNMENT: &dyn Trait =
5-
LL | | unsafe { std::mem::transmute((&92u8, &[0usize, 1usize, 1000usize])) };
6-
| |______________^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^__-
7-
| |
8-
| invalid vtable: alignment `1000` is not a power of 2
9-
|
10-
= note: `#[deny(const_err)]` on by default
11-
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
12-
= note: for more information, see issue #71800 <https://github.com/rust-lang/rust/issues/71800>
4+
LL | unsafe { std::mem::transmute((&92u8, &[0usize, 1usize, 1000usize])) };
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ invalid vtable: alignment `1000` is not a power of 2
136

14-
error: any use of this value will cause an error
15-
--> $DIR/ub-incorrect-vtable.rs:25:14
16-
|
17-
LL | / const INVALID_VTABLE_SIZE: &dyn Trait =
18-
LL | | unsafe { std::mem::transmute((&92u8, &[1usize, usize::MAX, 1usize])) };
19-
| |______________^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^__-
20-
| |
21-
| invalid vtable: size is bigger than largest supported object
7+
error[E0080]: evaluation of constant value failed
8+
--> $DIR/ub-incorrect-vtable.rs:24:14
229
|
23-
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
24-
= note: for more information, see issue #71800 <https://github.com/rust-lang/rust/issues/71800>
10+
LL | unsafe { std::mem::transmute((&92u8, &[1usize, usize::MAX, 1usize])) };
11+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ invalid vtable: size is bigger than largest supported object
2512

2613
error[E0080]: it is undefined behavior to use this value
27-
--> $DIR/ub-incorrect-vtable.rs:36:1
14+
--> $DIR/ub-incorrect-vtable.rs:34:1
2815
|
2916
LL | / const INVALID_VTABLE_ALIGNMENT_UB: W<&dyn Trait> =
3017
LL | | unsafe { std::mem::transmute((&92u8, &(drop_me as fn(*mut usize), 1usize, 1000usize))) };
@@ -36,7 +23,7 @@ LL | | unsafe { std::mem::transmute((&92u8, &(drop_me as fn(*mut usize), 1us
3623
}
3724

3825
error[E0080]: it is undefined behavior to use this value
39-
--> $DIR/ub-incorrect-vtable.rs:41:1
26+
--> $DIR/ub-incorrect-vtable.rs:39:1
4027
|
4128
LL | / const INVALID_VTABLE_SIZE_UB: W<&dyn Trait> =
4229
LL | | unsafe { std::mem::transmute((&92u8, &(drop_me as fn(*mut usize), usize::MAX, 1usize))) };

src/test/ui/consts/const-eval/ub-incorrect-vtable.64bit.stderr

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,17 @@
1-
error: any use of this value will cause an error
1+
error[E0080]: evaluation of constant value failed
22
--> $DIR/ub-incorrect-vtable.rs:19:14
33
|
4-
LL | / const INVALID_VTABLE_ALIGNMENT: &dyn Trait =
5-
LL | | unsafe { std::mem::transmute((&92u8, &[0usize, 1usize, 1000usize])) };
6-
| |______________^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^__-
7-
| |
8-
| invalid vtable: alignment `1000` is not a power of 2
9-
|
10-
= note: `#[deny(const_err)]` on by default
11-
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
12-
= note: for more information, see issue #71800 <https://github.com/rust-lang/rust/issues/71800>
4+
LL | unsafe { std::mem::transmute((&92u8, &[0usize, 1usize, 1000usize])) };
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ invalid vtable: alignment `1000` is not a power of 2
136

14-
error: any use of this value will cause an error
15-
--> $DIR/ub-incorrect-vtable.rs:25:14
16-
|
17-
LL | / const INVALID_VTABLE_SIZE: &dyn Trait =
18-
LL | | unsafe { std::mem::transmute((&92u8, &[1usize, usize::MAX, 1usize])) };
19-
| |______________^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^__-
20-
| |
21-
| invalid vtable: size is bigger than largest supported object
7+
error[E0080]: evaluation of constant value failed
8+
--> $DIR/ub-incorrect-vtable.rs:24:14
229
|
23-
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
24-
= note: for more information, see issue #71800 <https://github.com/rust-lang/rust/issues/71800>
10+
LL | unsafe { std::mem::transmute((&92u8, &[1usize, usize::MAX, 1usize])) };
11+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ invalid vtable: size is bigger than largest supported object
2512

2613
error[E0080]: it is undefined behavior to use this value
27-
--> $DIR/ub-incorrect-vtable.rs:36:1
14+
--> $DIR/ub-incorrect-vtable.rs:34:1
2815
|
2916
LL | / const INVALID_VTABLE_ALIGNMENT_UB: W<&dyn Trait> =
3017
LL | | unsafe { std::mem::transmute((&92u8, &(drop_me as fn(*mut usize), 1usize, 1000usize))) };
@@ -36,7 +23,7 @@ LL | | unsafe { std::mem::transmute((&92u8, &(drop_me as fn(*mut usize), 1us
3623
}
3724

3825
error[E0080]: it is undefined behavior to use this value
39-
--> $DIR/ub-incorrect-vtable.rs:41:1
26+
--> $DIR/ub-incorrect-vtable.rs:39:1
4027
|
4128
LL | / const INVALID_VTABLE_SIZE_UB: W<&dyn Trait> =
4229
LL | | unsafe { std::mem::transmute((&92u8, &(drop_me as fn(*mut usize), usize::MAX, 1usize))) };

src/test/ui/consts/const-eval/ub-incorrect-vtable.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,12 @@ trait Trait {}
1717

1818
const INVALID_VTABLE_ALIGNMENT: &dyn Trait =
1919
unsafe { std::mem::transmute((&92u8, &[0usize, 1usize, 1000usize])) };
20-
//~^ ERROR any use of this value will cause an error
21-
//~| WARNING this was previously accepted by the compiler
20+
//~^ ERROR evaluation of constant value failed
2221
//~| invalid vtable: alignment `1000` is not a power of 2
2322

2423
const INVALID_VTABLE_SIZE: &dyn Trait =
2524
unsafe { std::mem::transmute((&92u8, &[1usize, usize::MAX, 1usize])) };
26-
//~^ ERROR any use of this value will cause an error
27-
//~| WARNING this was previously accepted by the compiler
25+
//~^ ERROR evaluation of constant value failed
2826
//~| invalid vtable: size is bigger than largest supported object
2927

3028
#[repr(transparent)]

0 commit comments

Comments
 (0)