Skip to content

Commit 8ec63d3

Browse files
committed
collect and extend our tests for monomorphization-related const errors
1 parent dca08fa commit 8ec63d3

14 files changed

+174
-49
lines changed

tests/ui/consts/const-eval/erroneous-const.stderr

-15
This file was deleted.

tests/ui/consts/const-eval/erroneous-const2.stderr

-15
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
error[E0080]: evaluation of `Fail::<i32>::C` failed
2+
--> $DIR/dead-code-in-called-fn.rs:9:19
3+
|
4+
LL | const C: () = panic!();
5+
| ^^^^^^^^ the evaluated program panicked at 'explicit panic', $DIR/dead-code-in-called-fn.rs:9:19
6+
|
7+
= 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)
8+
9+
note: the above error was encountered while instantiating `fn called::<i32>`
10+
--> $DIR/dead-code-in-called-fn.rs:22:5
11+
|
12+
LL | called::<i32>();
13+
| ^^^^^^^^^^^^^^^
14+
15+
error: aborting due to 1 previous error
16+
17+
For more information about this error, try `rustc --explain E0080`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
error[E0080]: evaluation of `Fail::<i32>::C` failed
2+
--> $DIR/dead-code-in-called-fn.rs:9:19
3+
|
4+
LL | const C: () = panic!();
5+
| ^^^^^^^^ the evaluated program panicked at 'explicit panic', $DIR/dead-code-in-called-fn.rs:9:19
6+
|
7+
= 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)
8+
9+
note: the above error was encountered while instantiating `fn called::<i32>`
10+
--> $DIR/dead-code-in-called-fn.rs:22:5
11+
|
12+
LL | called::<i32>();
13+
| ^^^^^^^^^^^^^^^
14+
15+
error: aborting due to 1 previous error
16+
17+
For more information about this error, try `rustc --explain E0080`.
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,23 @@
1+
//@revisions: opt no-opt
12
//@ build-fail
2-
//@ compile-flags: -O
3+
//@[opt] compile-flags: -O
34
//! Make sure we detect erroneous constants post-monomorphization even when they are unused. This is
45
//! crucial, people rely on it for soundness. (https://github.com/rust-lang/rust/issues/112090)
56
6-
struct PrintName<T>(T);
7-
impl<T> PrintName<T> {
8-
const VOID: () = panic!(); //~ERROR evaluation of `PrintName::<i32>::VOID` failed
7+
struct Fail<T>(T);
8+
impl<T> Fail<T> {
9+
const C: () = panic!(); //~ERROR evaluation of `Fail::<i32>::C` failed
910
}
1011

11-
fn no_codegen<T>() {
12+
#[inline(never)]
13+
fn called<T>() {
1214
// Any function that is called is guaranteed to have all consts that syntactically
1315
// appear in its body evaluated, even if they only appear in dead code.
1416
if false {
15-
let _ = PrintName::<T>::VOID;
17+
let _ = Fail::<T>::C;
1618
}
1719
}
20+
1821
pub fn main() {
19-
no_codegen::<i32>();
22+
called::<i32>();
2023
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
error[E0080]: evaluation of `Fail::<i32>::C` failed
2+
--> $DIR/dead-code-in-const-called-fn.rs:8:19
3+
|
4+
LL | const C: () = panic!();
5+
| ^^^^^^^^ the evaluated program panicked at 'explicit panic', $DIR/dead-code-in-const-called-fn.rs:8:19
6+
|
7+
= 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)
8+
9+
note: erroneous constant encountered
10+
--> $DIR/dead-code-in-const-called-fn.rs:16:9
11+
|
12+
LL | Fail::<T>::C;
13+
| ^^^^^^^^^^^^
14+
15+
error: aborting due to 1 previous error
16+
17+
For more information about this error, try `rustc --explain E0080`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
error[E0080]: evaluation of `Fail::<i32>::C` failed
2+
--> $DIR/dead-code-in-const-called-fn.rs:8:19
3+
|
4+
LL | const C: () = panic!();
5+
| ^^^^^^^^ the evaluated program panicked at 'explicit panic', $DIR/dead-code-in-const-called-fn.rs:8:19
6+
|
7+
= 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)
8+
9+
note: erroneous constant encountered
10+
--> $DIR/dead-code-in-const-called-fn.rs:16:9
11+
|
12+
LL | Fail::<T>::C;
13+
| ^^^^^^^^^^^^
14+
15+
error: aborting due to 1 previous error
16+
17+
For more information about this error, try `rustc --explain E0080`.

tests/ui/consts/const-eval/erroneous-const.rs tests/ui/consts/monomorphization/dead-code-in-const-called-fn.rs

+7-4
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
1+
//@revisions: opt no-opt
2+
//@[opt] compile-flags: -O
13
//! Make sure we error on erroneous consts even if they are unused.
24
#![allow(unconditional_panic)]
35

4-
struct PrintName<T>(T);
5-
impl<T> PrintName<T> {
6-
const VOID: () = [()][2]; //~ERROR evaluation of `PrintName::<i32>::VOID` failed
6+
struct Fail<T>(T);
7+
impl<T> Fail<T> {
8+
const C: () = panic!(); //~ERROR evaluation of `Fail::<i32>::C` failed
79
}
810

11+
#[inline(never)]
912
const fn no_codegen<T>() {
1013
if false {
1114
// This bad constant is only used in dead code in a no-codegen function... and yet we still
1215
// must make sure that the build fails.
13-
PrintName::<T>::VOID; //~ constant
16+
Fail::<T>::C; //~ constant
1417
}
1518
}
1619

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
error[E0080]: evaluation of `Fail::<i32>::C` failed
2+
--> $DIR/dead-code-in-dead-fn.rs:9:19
3+
|
4+
LL | const C: () = panic!();
5+
| ^^^^^^^^ the evaluated program panicked at 'explicit panic', $DIR/dead-code-in-dead-fn.rs:9:19
6+
|
7+
= 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)
8+
9+
note: the above error was encountered while instantiating `fn not_called::<i32>`
10+
--> $DIR/dead-code-in-dead-fn.rs:22:9
11+
|
12+
LL | not_called::<T>();
13+
| ^^^^^^^^^^^^^^^^^
14+
15+
error: aborting due to 1 previous error
16+
17+
For more information about this error, try `rustc --explain E0080`.

tests/ui/consts/const-eval/unused-broken-const-late.stderr tests/ui/consts/monomorphization/dead-code-in-dead-fn.opt.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
error[E0080]: evaluation of `PrintName::<i32>::VOID` failed
2-
--> $DIR/unused-broken-const-late.rs:8:22
1+
error[E0080]: evaluation of `Fail::<i32>::C` failed
2+
--> $DIR/dead-code-in-dead-fn.rs:9:19
33
|
4-
LL | const VOID: () = panic!();
5-
| ^^^^^^^^ the evaluated program panicked at 'explicit panic', $DIR/unused-broken-const-late.rs:8:22
4+
LL | const C: () = panic!();
5+
| ^^^^^^^^ the evaluated program panicked at 'explicit panic', $DIR/dead-code-in-dead-fn.rs:9:19
66
|
77
= 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)
88

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//@revisions: opt no-opt
2+
//@ build-fail
3+
//@[opt] compile-flags: -O
4+
//! Make sure we detect erroneous constants post-monomorphization even when they are unused. This is
5+
//! crucial, people rely on it for soundness. (https://github.com/rust-lang/rust/issues/112090)
6+
7+
struct Fail<T>(T);
8+
impl<T> Fail<T> {
9+
const C: () = panic!(); //~ERROR evaluation of `Fail::<i32>::C` failed
10+
}
11+
12+
// This function is not actually called, but it is mentioned in a function that is called.
13+
// Make sure we still find this error.
14+
#[inline(never)]
15+
fn not_called<T>() {
16+
let _ = Fail::<T>::C;
17+
}
18+
19+
#[inline(never)]
20+
fn called<T>() {
21+
if false {
22+
not_called::<T>();
23+
}
24+
}
25+
26+
pub fn main() {
27+
called::<i32>();
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
error[E0080]: evaluation of `Fail::<i32>::C` failed
2+
--> $DIR/dead-code-in-static.rs:8:19
3+
|
4+
LL | const C: () = panic!();
5+
| ^^^^^^^^ the evaluated program panicked at 'explicit panic', $DIR/dead-code-in-static.rs:8:19
6+
|
7+
= 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)
8+
9+
note: erroneous constant encountered
10+
--> $DIR/dead-code-in-static.rs:15:9
11+
|
12+
LL | Fail::<i32>::C;
13+
| ^^^^^^^^^^^^^^
14+
15+
error: aborting due to 1 previous error
16+
17+
For more information about this error, try `rustc --explain E0080`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
error[E0080]: evaluation of `Fail::<i32>::C` failed
2+
--> $DIR/dead-code-in-static.rs:8:19
3+
|
4+
LL | const C: () = panic!();
5+
| ^^^^^^^^ the evaluated program panicked at 'explicit panic', $DIR/dead-code-in-static.rs:8:19
6+
|
7+
= 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)
8+
9+
note: erroneous constant encountered
10+
--> $DIR/dead-code-in-static.rs:15:9
11+
|
12+
LL | Fail::<i32>::C;
13+
| ^^^^^^^^^^^^^^
14+
15+
error: aborting due to 1 previous error
16+
17+
For more information about this error, try `rustc --explain E0080`.

tests/ui/consts/const-eval/erroneous-const2.rs tests/ui/consts/monomorphization/dead-code-in-static.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
1+
//@revisions: opt no-opt
2+
//@[opt] compile-flags: -O
13
//! Make sure we error on erroneous consts even if they are unused.
24
#![allow(unconditional_panic)]
35

4-
struct PrintName<T>(T);
5-
impl<T> PrintName<T> {
6-
const VOID: () = [()][2]; //~ERROR evaluation of `PrintName::<i32>::VOID` failed
6+
struct Fail<T>(T);
7+
impl<T> Fail<T> {
8+
const C: () = panic!(); //~ERROR evaluation of `Fail::<i32>::C` failed
79
}
810

911
pub static FOO: () = {
1012
if false {
1113
// This bad constant is only used in dead code in a static initializer... and yet we still
1214
// must make sure that the build fails.
13-
PrintName::<i32>::VOID; //~ constant
15+
Fail::<i32>::C; //~ constant
1416
}
1517
};
1618

0 commit comments

Comments
 (0)