Skip to content

Commit 219ce16

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

22 files changed

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