Skip to content

Commit fd3851a

Browse files
committed
add test for unused erroneous const in CTFE
1 parent 1fa7203 commit fd3851a

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//! Make sure we error on erroneous consts even if they are unused.
2+
#![warn(const_err, unconditional_panic)]
3+
4+
struct PrintName<T>(T);
5+
impl<T> PrintName<T> {
6+
const VOID: () = [()][2]; //~WARN any use of this value will cause an error
7+
//~^ WARN this operation will panic at runtime
8+
}
9+
10+
const fn no_codegen<T>() {
11+
if false { //~ERROR evaluation of constant value failed
12+
let _ = PrintName::<T>::VOID;
13+
}
14+
}
15+
16+
pub static FOO: () = no_codegen::<i32>(); //~ERROR could not evaluate static initializer
17+
18+
fn main() {
19+
FOO
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
warning: this operation will panic at runtime
2+
--> $DIR/erroneous-const.rs:6:22
3+
|
4+
LL | const VOID: () = [()][2];
5+
| ^^^^^^^ index out of bounds: the len is 1 but the index is 2
6+
|
7+
note: the lint level is defined here
8+
--> $DIR/erroneous-const.rs:2:20
9+
|
10+
LL | #![warn(const_err, unconditional_panic)]
11+
| ^^^^^^^^^^^^^^^^^^^
12+
13+
warning: any use of this value will cause an error
14+
--> $DIR/erroneous-const.rs:6:22
15+
|
16+
LL | const VOID: () = [()][2];
17+
| -----------------^^^^^^^-
18+
| |
19+
| index out of bounds: the len is 1 but the index is 2
20+
|
21+
note: the lint level is defined here
22+
--> $DIR/erroneous-const.rs:2:9
23+
|
24+
LL | #![warn(const_err, unconditional_panic)]
25+
| ^^^^^^^^^
26+
27+
error[E0080]: evaluation of constant value failed
28+
--> $DIR/erroneous-const.rs:11:5
29+
|
30+
LL | / if false {
31+
LL | | let _ = PrintName::<T>::VOID;
32+
LL | | }
33+
| |_____^ referenced constant has errors
34+
35+
error[E0080]: could not evaluate static initializer
36+
--> $DIR/erroneous-const.rs:16:22
37+
|
38+
LL | pub static FOO: () = no_codegen::<i32>();
39+
| ^^^^^^^^^^^^^^^^^^^ referenced constant has errors
40+
41+
error: aborting due to 2 previous errors; 2 warnings emitted
42+
43+
For more information about this error, try `rustc --explain E0080`.

0 commit comments

Comments
 (0)