From 5e1b0cbe5d0c86601b70bd8cc883227421c40cac Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Thu, 5 Oct 2023 10:02:07 +0200 Subject: [PATCH] add test for const-eval error in dead code during monomorphization --- .../const-eval/unused-broken-const-late.rs | 20 +++++++++++++++++++ .../unused-broken-const-late.stderr | 11 ++++++++++ 2 files changed, 31 insertions(+) create mode 100644 tests/ui/consts/const-eval/unused-broken-const-late.rs create mode 100644 tests/ui/consts/const-eval/unused-broken-const-late.stderr diff --git a/tests/ui/consts/const-eval/unused-broken-const-late.rs b/tests/ui/consts/const-eval/unused-broken-const-late.rs new file mode 100644 index 0000000000000..a6528ec5fd6a3 --- /dev/null +++ b/tests/ui/consts/const-eval/unused-broken-const-late.rs @@ -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); +impl PrintName { + const VOID: () = panic!(); //~ERROR evaluation of `PrintName::::VOID` failed +} + +fn no_codegen() { + // 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::::VOID; + } +} +pub fn main() { + no_codegen::(); +} diff --git a/tests/ui/consts/const-eval/unused-broken-const-late.stderr b/tests/ui/consts/const-eval/unused-broken-const-late.stderr new file mode 100644 index 0000000000000..cdb70a69dfd8a --- /dev/null +++ b/tests/ui/consts/const-eval/unused-broken-const-late.stderr @@ -0,0 +1,11 @@ +error[E0080]: evaluation of `PrintName::::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`.