Skip to content

Commit 96b288f

Browse files
authored
Rollup merge of rust-lang#67164 - matthewjasper:never-remove-const, r=oli-obk
Ensure that panicking in constants eventually errors based on rust-lang#67134 closes rust-lang#66975 r? @oli-obk
2 parents 4c3e95e + d2ed209 commit 96b288f

8 files changed

+150
-1
lines changed

src/librustc_mir/build/expr/into.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,14 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
6565
_ => false,
6666
};
6767

68-
unpack!(block = this.as_local_rvalue(block, source));
68+
// (#66975) Source could be a const of type `!`, so has to
69+
// exist in the generated MIR.
70+
unpack!(block = this.as_temp(
71+
block,
72+
this.local_scope(),
73+
source,
74+
Mutability::Mut,
75+
));
6976

7077
// This is an optimization. If the expression was a call then we already have an
7178
// unreachable block. Don't bother to terminate it and create a new one.
+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Regression test for #66975 - ensure that we don't keep unevaluated
2+
// `!`-typed constants until codegen.
3+
4+
// Force generation of optimized mir for functions that do not reach codegen.
5+
// compile-flags: --emit mir,link
6+
7+
#![feature(const_panic)]
8+
9+
struct PrintName<T>(T);
10+
11+
impl<T> PrintName<T> {
12+
const VOID: ! = panic!();
13+
}
14+
15+
fn no_codegen<T>() {
16+
let _ = PrintName::<T>::VOID;
17+
}
18+
19+
fn main() {}
20+
21+
// END RUST SOURCE
22+
// START rustc.no_codegen.PreCodegen.after.mir
23+
// bb0: {
24+
// StorageLive(_1);
25+
// _1 = const PrintName::<T>::VOID;
26+
// unreachable;
27+
// }
28+
// END rustc.no_codegen.PreCodegen.after.mir
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Regression test for #66975
2+
#![warn(const_err)]
3+
4+
struct PrintName<T>(T);
5+
6+
impl<T> PrintName<T> {
7+
const VOID: ! = { let x = 0 * std::mem::size_of::<T>(); [][x] };
8+
//~^ WARN any use of this value will cause an error
9+
}
10+
11+
fn f<T>() {
12+
let _ = PrintName::<T>::VOID;
13+
//~^ ERROR erroneous constant encountered
14+
}
15+
16+
pub fn main() {
17+
f::<()>();
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
warning: any use of this value will cause an error
2+
--> $DIR/index-out-of-bounds-never-type.rs:7:61
3+
|
4+
LL | const VOID: ! = { let x = 0 * std::mem::size_of::<T>(); [][x] };
5+
| --------------------------------------------------------^^^^^---
6+
| |
7+
| index out of bounds: the len is 0 but the index is 0
8+
|
9+
note: lint level defined here
10+
--> $DIR/index-out-of-bounds-never-type.rs:2:9
11+
|
12+
LL | #![warn(const_err)]
13+
| ^^^^^^^^^
14+
15+
error: erroneous constant encountered
16+
--> $DIR/index-out-of-bounds-never-type.rs:12:13
17+
|
18+
LL | let _ = PrintName::<T>::VOID;
19+
| ^^^^^^^^^^^^^^^^^^^^
20+
21+
error: aborting due to previous error
22+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Regression test for #66975
2+
#![warn(const_err)]
3+
#![feature(const_panic)]
4+
5+
struct PrintName;
6+
7+
impl PrintName {
8+
const VOID: ! = panic!();
9+
//~^ WARN any use of this value will cause an error
10+
}
11+
12+
fn main() {
13+
let _ = PrintName::VOID;
14+
//~^ ERROR erroneous constant used
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
warning: any use of this value will cause an error
2+
--> $DIR/panic-assoc-never-type.rs:8:21
3+
|
4+
LL | const VOID: ! = panic!();
5+
| ----------------^^^^^^^^-
6+
| |
7+
| the evaluated program panicked at 'explicit panic', $DIR/panic-assoc-never-type.rs:8:21
8+
|
9+
note: lint level defined here
10+
--> $DIR/panic-assoc-never-type.rs:2:9
11+
|
12+
LL | #![warn(const_err)]
13+
| ^^^^^^^^^
14+
= note: this warning originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)
15+
16+
error[E0080]: erroneous constant used
17+
--> $DIR/panic-assoc-never-type.rs:13:13
18+
|
19+
LL | let _ = PrintName::VOID;
20+
| ^^^^^^^^^^^^^^^ referenced constant has errors
21+
22+
error: aborting due to previous error
23+
24+
For more information about this error, try `rustc --explain E0080`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Regression test for #66975
2+
#![warn(const_err)]
3+
#![feature(const_panic)]
4+
5+
const VOID: ! = panic!();
6+
//~^ WARN any use of this value will cause an error
7+
8+
fn main() {
9+
let _ = VOID;
10+
//~^ ERROR erroneous constant used
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
warning: any use of this value will cause an error
2+
--> $DIR/panic-never-type.rs:5:17
3+
|
4+
LL | const VOID: ! = panic!();
5+
| ----------------^^^^^^^^-
6+
| |
7+
| the evaluated program panicked at 'explicit panic', $DIR/panic-never-type.rs:5:17
8+
|
9+
note: lint level defined here
10+
--> $DIR/panic-never-type.rs:2:9
11+
|
12+
LL | #![warn(const_err)]
13+
| ^^^^^^^^^
14+
= note: this warning originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)
15+
16+
error[E0080]: erroneous constant used
17+
--> $DIR/panic-never-type.rs:9:13
18+
|
19+
LL | let _ = VOID;
20+
| ^^^^ referenced constant has errors
21+
22+
error: aborting due to previous error
23+
24+
For more information about this error, try `rustc --explain E0080`.

0 commit comments

Comments
 (0)