From fd3851aadb3f5326480f9e9ced57ade2db86480b Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Mon, 10 Aug 2020 10:58:53 +0200 Subject: [PATCH] add test for unused erroneous const in CTFE --- .../ui/consts/const-eval/erroneous-const.rs | 20 +++++++++ .../consts/const-eval/erroneous-const.stderr | 43 +++++++++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 src/test/ui/consts/const-eval/erroneous-const.rs create mode 100644 src/test/ui/consts/const-eval/erroneous-const.stderr diff --git a/src/test/ui/consts/const-eval/erroneous-const.rs b/src/test/ui/consts/const-eval/erroneous-const.rs new file mode 100644 index 0000000000000..93c4e9372e8fb --- /dev/null +++ b/src/test/ui/consts/const-eval/erroneous-const.rs @@ -0,0 +1,20 @@ +//! Make sure we error on erroneous consts even if they are unused. +#![warn(const_err, unconditional_panic)] + +struct PrintName(T); +impl PrintName { + const VOID: () = [()][2]; //~WARN any use of this value will cause an error + //~^ WARN this operation will panic at runtime +} + +const fn no_codegen() { + if false { //~ERROR evaluation of constant value failed + let _ = PrintName::::VOID; + } +} + +pub static FOO: () = no_codegen::(); //~ERROR could not evaluate static initializer + +fn main() { + FOO +} diff --git a/src/test/ui/consts/const-eval/erroneous-const.stderr b/src/test/ui/consts/const-eval/erroneous-const.stderr new file mode 100644 index 0000000000000..da7e7247d50f9 --- /dev/null +++ b/src/test/ui/consts/const-eval/erroneous-const.stderr @@ -0,0 +1,43 @@ +warning: this operation will panic at runtime + --> $DIR/erroneous-const.rs:6:22 + | +LL | const VOID: () = [()][2]; + | ^^^^^^^ index out of bounds: the len is 1 but the index is 2 + | +note: the lint level is defined here + --> $DIR/erroneous-const.rs:2:20 + | +LL | #![warn(const_err, unconditional_panic)] + | ^^^^^^^^^^^^^^^^^^^ + +warning: any use of this value will cause an error + --> $DIR/erroneous-const.rs:6:22 + | +LL | const VOID: () = [()][2]; + | -----------------^^^^^^^- + | | + | index out of bounds: the len is 1 but the index is 2 + | +note: the lint level is defined here + --> $DIR/erroneous-const.rs:2:9 + | +LL | #![warn(const_err, unconditional_panic)] + | ^^^^^^^^^ + +error[E0080]: evaluation of constant value failed + --> $DIR/erroneous-const.rs:11:5 + | +LL | / if false { +LL | | let _ = PrintName::::VOID; +LL | | } + | |_____^ referenced constant has errors + +error[E0080]: could not evaluate static initializer + --> $DIR/erroneous-const.rs:16:22 + | +LL | pub static FOO: () = no_codegen::(); + | ^^^^^^^^^^^^^^^^^^^ referenced constant has errors + +error: aborting due to 2 previous errors; 2 warnings emitted + +For more information about this error, try `rustc --explain E0080`.