Skip to content

Commit 2d1acd9

Browse files
committed
const-eval: organize and extend tests for required-consts
1 parent 7de1a1f commit 2d1acd9

22 files changed

+365
-72
lines changed

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

-15
This file was deleted.

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

-19
This file was deleted.

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

-15
This file was deleted.

tests/ui/consts/const-eval/unused-broken-const-late.stderr

-11
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/collect-in-called-fn.rs:9:19
3+
|
4+
LL | const C: () = panic!();
5+
| ^^^^^^^^ the evaluated program panicked at 'explicit panic', $DIR/collect-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/collect-in-called-fn.rs:23: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/collect-in-called-fn.rs:9:19
3+
|
4+
LL | const C: () = panic!();
5+
| ^^^^^^^^ the evaluated program panicked at 'explicit panic', $DIR/collect-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/collect-in-called-fn.rs:23: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,24 @@
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.
16+
// This relies on mono-item collection checking `required_consts` in collected functions.
1417
if false {
15-
let _ = PrintName::<T>::VOID;
18+
let _ = Fail::<T>::C;
1619
}
1720
}
21+
1822
pub fn main() {
19-
no_codegen::<i32>();
23+
called::<i32>();
2024
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error[E0080]: evaluation of `Fail::<i32>::C` failed
2+
--> $DIR/collect-in-dead-drop.rs:11:19
3+
|
4+
LL | const C: () = panic!();
5+
| ^^^^^^^^ the evaluated program panicked at 'explicit panic', $DIR/collect-in-dead-drop.rs:11: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 <Fail<i32> as std::ops::Drop>::drop`
10+
--> $SRC_DIR/core/src/ptr/mod.rs:LL:COL
11+
12+
error: aborting due to 1 previous error
13+
14+
For more information about this error, try `rustc --explain E0080`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
//@revisions: no-opt
2+
//FIXME: `opt` revision currently does not stop with an error due to
3+
//<https://github.com/rust-lang/rust/issues/107503>.
4+
//@ build-fail
5+
//@[opt] compile-flags: -O
6+
//! Make sure we detect erroneous constants post-monomorphization even when they are unused. This is
7+
//! crucial, people rely on it for soundness. (https://github.com/rust-lang/rust/issues/112090)
8+
9+
struct Fail<T>(T);
10+
impl<T> Fail<T> {
11+
const C: () = panic!(); //~ERROR evaluation of `Fail::<i32>::C` failed
12+
}
13+
14+
// This function is not actually called, but is mentioned implicitly as destructor in dead code in a
15+
// function that is called. Make sure we still find this error.
16+
impl<T> Drop for Fail<T> {
17+
fn drop(&mut self) {
18+
let _ = Fail::<T>::C;
19+
}
20+
}
21+
22+
#[inline(never)]
23+
fn called<T>(x: T) {
24+
if false {
25+
let v = Fail(x);
26+
drop(v);
27+
}
28+
}
29+
30+
pub fn main() {
31+
called::<i32>(0);
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
error[E0080]: evaluation of `Fail::<i32>::C` failed
2+
--> $DIR/collect-in-dead-fn.rs:11:19
3+
|
4+
LL | const C: () = panic!();
5+
| ^^^^^^^^ the evaluated program panicked at 'explicit panic', $DIR/collect-in-dead-fn.rs:11: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/collect-in-dead-fn.rs:28: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`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//@revisions: no-opt
2+
//FIXME: `opt` revision currently does not stop with an error due to
3+
//<https://github.com/rust-lang/rust/issues/107503>.
4+
//@ build-fail
5+
//@[opt] compile-flags: -O
6+
//! Make sure we detect erroneous constants post-monomorphization even when they are unused. This is
7+
//! crucial, people rely on it for soundness. (https://github.com/rust-lang/rust/issues/112090)
8+
9+
struct Fail<T>(T);
10+
impl<T> Fail<T> {
11+
const C: () = panic!(); //~ERROR evaluation of `Fail::<i32>::C` failed
12+
}
13+
14+
// This function is not actually called, but it is mentioned in dead code in a function that is
15+
// called. Make sure we still find this error.
16+
// This relies on mono-item collection checking `required_consts` in functions that syntactically
17+
// are called in collected functions (even inside dead code).
18+
#[inline(never)]
19+
fn not_called<T>() {
20+
if false {
21+
let _ = Fail::<T>::C;
22+
}
23+
}
24+
25+
#[inline(never)]
26+
fn called<T>() {
27+
if false {
28+
not_called::<T>();
29+
}
30+
}
31+
32+
pub fn main() {
33+
called::<i32>();
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
error[E0080]: evaluation of `Fail::<i32>::C` failed
2+
--> $DIR/collect-in-dead-vtable.rs:11:19
3+
|
4+
LL | const C: () = panic!();
5+
| ^^^^^^^^ the evaluated program panicked at 'explicit panic', $DIR/collect-in-dead-vtable.rs:11: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 <std::vec::Vec<i32> as MyTrait>::not_called`
10+
--> $DIR/collect-in-dead-vtable.rs:34:40
11+
|
12+
LL | let gen_vtable: &dyn MyTrait = &v; // vtable "appears" here
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,40 @@
1+
//@revisions: no-opt
2+
//FIXME: `opt` revision currently does not stop with an error due to
3+
//<https://github.com/rust-lang/rust/issues/107503>.
4+
//@ build-fail
5+
//@[opt] compile-flags: -O
6+
//! Make sure we detect erroneous constants post-monomorphization even when they are unused. This is
7+
//! crucial, people rely on it for soundness. (https://github.com/rust-lang/rust/issues/112090)
8+
9+
struct Fail<T>(T);
10+
impl<T> Fail<T> {
11+
const C: () = panic!(); //~ERROR evaluation of `Fail::<i32>::C` failed
12+
}
13+
14+
trait MyTrait {
15+
fn not_called(&self);
16+
}
17+
18+
// This function is not actually called, but it is mentioned in a vtable in a function that is
19+
// called. Make sure we still find this error.
20+
// This relies on mono-item collection checking `required_consts` in functions that are referenced
21+
// in vtables that syntactically appear in collected functions (even inside dead code).
22+
impl<T> MyTrait for Vec<T> {
23+
fn not_called(&self) {
24+
if false {
25+
let _ = Fail::<T>::C;
26+
}
27+
}
28+
}
29+
30+
#[inline(never)]
31+
fn called<T>() {
32+
if false {
33+
let v: Vec<T> = Vec::new();
34+
let gen_vtable: &dyn MyTrait = &v; // vtable "appears" here
35+
}
36+
}
37+
38+
pub fn main() {
39+
called::<i32>();
40+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
error[E0080]: evaluation of `Fail::<i32>::C` failed
2+
--> $DIR/interpret-in-const-called-fn.rs:7:19
3+
|
4+
LL | const C: () = panic!();
5+
| ^^^^^^^^ the evaluated program panicked at 'explicit panic', $DIR/interpret-in-const-called-fn.rs:7: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/interpret-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/interpret-in-const-called-fn.rs:7:19
3+
|
4+
LL | const C: () = panic!();
5+
| ^^^^^^^^ the evaluated program panicked at 'explicit panic', $DIR/interpret-in-const-called-fn.rs:7: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/interpret-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 renamed to tests/ui/consts/required-consts/interpret-in-const-called-fn.rs

+8-5
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.
2-
#![allow(unconditional_panic)]
34
4-
struct PrintName<T>(T);
5-
impl<T> PrintName<T> {
6-
const VOID: () = [()][2]; //~ERROR evaluation of `PrintName::<i32>::VOID` failed
5+
struct Fail<T>(T);
6+
impl<T> Fail<T> {
7+
const C: () = panic!(); //~ERROR evaluation of `Fail::<i32>::C` failed
78
}
89

10+
#[inline(never)]
911
const fn no_codegen<T>() {
1012
if false {
1113
// This bad constant is only used in dead code in a no-codegen function... and yet we still
1214
// must make sure that the build fails.
13-
PrintName::<T>::VOID; //~ constant
15+
// This relies on const-eval evaluating all `required_consts` of `const fn`.
16+
Fail::<T>::C; //~ constant
1417
}
1518
}
1619

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
error[E0080]: evaluation of constant value failed
2+
--> $SRC_DIR/core/src/hint.rs:LL:COL
3+
|
4+
= note: entering unreachable code
5+
|
6+
note: inside `unreachable_unchecked`
7+
--> $SRC_DIR/core/src/hint.rs:LL:COL
8+
note: inside `ub`
9+
--> $DIR/interpret-in-promoted.rs:6:5
10+
|
11+
LL | std::hint::unreachable_unchecked();
12+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
13+
note: inside `FOO`
14+
--> $DIR/interpret-in-promoted.rs:12:28
15+
|
16+
LL | let _x: &'static () = &ub();
17+
| ^^^^
18+
19+
note: erroneous constant encountered
20+
--> $DIR/interpret-in-promoted.rs:12:27
21+
|
22+
LL | let _x: &'static () = &ub();
23+
| ^^^^^
24+
25+
error: aborting due to 1 previous error
26+
27+
For more information about this error, try `rustc --explain E0080`.

0 commit comments

Comments
 (0)