Rollup merge of #116444 - RalfJung:broken-unused-const, r=oli-obk

add test for const-eval error in dead code during monomorphization
This commit is contained in:
Guillaume Gomez 2023-10-10 18:44:45 +02:00 committed by GitHub
commit bbaf6bd136
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 0 deletions

View File

@ -0,0 +1,20 @@
// build-fail
// compile-flags: -O
//! Make sure we detect erroneous constants post-monomorphization even when they are unused. This is
//! crucial, people rely on it for soundness. (https://github.com/rust-lang/rust/issues/112090)
struct PrintName<T>(T);
impl<T> PrintName<T> {
const VOID: () = panic!(); //~ERROR evaluation of `PrintName::<i32>::VOID` failed
}
fn no_codegen<T>() {
// Any function that is called is guaranteed to have all consts that syntactically
// appear in its body evaluated, even if they only appear in dead code.
if false {
let _ = PrintName::<T>::VOID;
}
}
pub fn main() {
no_codegen::<i32>();
}

View File

@ -0,0 +1,11 @@
error[E0080]: evaluation of `PrintName::<i32>::VOID` failed
--> $DIR/unused-broken-const-late.rs:8:22
|
LL | const VOID: () = panic!();
| ^^^^^^^^ the evaluated program panicked at 'explicit panic', $DIR/unused-broken-const-late.rs:8:22
|
= note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info)
error: aborting due to previous error
For more information about this error, try `rustc --explain E0080`.