Skip to content

Commit

Permalink
add test for const-eval error in dead code during monomorphization
Browse files Browse the repository at this point in the history
  • Loading branch information
RalfJung committed Oct 5, 2023
1 parent 5236c8e commit d51b41c
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
19 changes: 19 additions & 0 deletions tests/ui/consts/const-eval/unused-broken-const-late.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// build-fail
//! 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)

Check failure on line 3 in tests/ui/consts/const-eval/unused-broken-const-late.rs

View workflow job for this annotation

GitHub Actions / PR - mingw-check-tidy

line longer than 100 chars

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>(); //~ encountered
}
17 changes: 17 additions & 0 deletions tests/ui/consts/const-eval/unused-broken-const-late.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
error[E0080]: evaluation of `PrintName::<i32>::VOID` failed
--> $DIR/unused-broken-const-late.rs:7:22
|
LL | const VOID: () = panic!();
| ^^^^^^^^ the evaluated program panicked at 'explicit panic', $DIR/unused-broken-const-late.rs:7: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)

note: the above error was encountered while instantiating `fn no_codegen::<i32>`
--> $DIR/unused-broken-const-late.rs:18:5
|
LL | no_codegen::<i32>();
| ^^^^^^^^^^^^^^^^^^^

error: aborting due to previous error

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

0 comments on commit d51b41c

Please sign in to comment.