Skip to content

Commit

Permalink
const-eval: organize and extend tests for required-consts
Browse files Browse the repository at this point in the history
  • Loading branch information
RalfJung committed Mar 13, 2024
1 parent 7de1a1f commit 219ce16
Show file tree
Hide file tree
Showing 22 changed files with 362 additions and 72 deletions.
15 changes: 0 additions & 15 deletions tests/ui/consts/const-eval/erroneous-const.stderr

This file was deleted.

19 changes: 0 additions & 19 deletions tests/ui/consts/const-eval/erroneous-const2.rs

This file was deleted.

15 changes: 0 additions & 15 deletions tests/ui/consts/const-eval/erroneous-const2.stderr

This file was deleted.

11 changes: 0 additions & 11 deletions tests/ui/consts/const-eval/unused-broken-const-late.stderr

This file was deleted.

17 changes: 17 additions & 0 deletions tests/ui/consts/required-consts/collect-in-called-fn.no-opt.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
error[E0080]: evaluation of `Fail::<i32>::C` failed
--> $DIR/collect-in-called-fn.rs:9:19
|
LL | const C: () = panic!();
| ^^^^^^^^ the evaluated program panicked at 'explicit panic', $DIR/collect-in-called-fn.rs:9:19
|
= 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)

note: the above error was encountered while instantiating `fn called::<i32>`
--> $DIR/collect-in-called-fn.rs:23:5
|
LL | called::<i32>();
| ^^^^^^^^^^^^^^^

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0080`.
17 changes: 17 additions & 0 deletions tests/ui/consts/required-consts/collect-in-called-fn.opt.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
error[E0080]: evaluation of `Fail::<i32>::C` failed
--> $DIR/collect-in-called-fn.rs:9:19
|
LL | const C: () = panic!();
| ^^^^^^^^ the evaluated program panicked at 'explicit panic', $DIR/collect-in-called-fn.rs:9:19
|
= 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)

note: the above error was encountered while instantiating `fn called::<i32>`
--> $DIR/collect-in-called-fn.rs:23:5
|
LL | called::<i32>();
| ^^^^^^^^^^^^^^^

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0080`.
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
//@revisions: opt no-opt
//@ build-fail
//@ compile-flags: -O
//@[opt] compile-flags: -O
//! Make sure we detect erroneous constants post-monomorphization even when they are unused. This is
//! crucial, people rely on it for soundness. (https://github.com/rust-lang/rust/issues/112090)

struct PrintName<T>(T);
impl<T> PrintName<T> {
const VOID: () = panic!(); //~ERROR evaluation of `PrintName::<i32>::VOID` failed
struct Fail<T>(T);
impl<T> Fail<T> {
const C: () = panic!(); //~ERROR evaluation of `Fail::<i32>::C` failed
}

fn no_codegen<T>() {
#[inline(never)]
fn called<T>() {
// Any function that is called is guaranteed to have all consts that syntactically
// appear in its body evaluated, even if they only appear in dead code.
// This relies on mono-item collection checking `required_consts` in collected functions.
if false {
let _ = PrintName::<T>::VOID;
let _ = Fail::<T>::C;
}
}

pub fn main() {
no_codegen::<i32>();
called::<i32>();
}
14 changes: 14 additions & 0 deletions tests/ui/consts/required-consts/collect-in-dead-drop.no-opt.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
error[E0080]: evaluation of `Fail::<i32>::C` failed
--> $DIR/collect-in-dead-drop.rs:10:19
|
LL | const C: () = panic!();
| ^^^^^^^^ the evaluated program panicked at 'explicit panic', $DIR/collect-in-dead-drop.rs:10:19
|
= 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)

note: the above error was encountered while instantiating `fn <Fail<i32> as std::ops::Drop>::drop`
--> $SRC_DIR/core/src/ptr/mod.rs:LL:COL

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0080`.
31 changes: 31 additions & 0 deletions tests/ui/consts/required-consts/collect-in-dead-drop.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//@revisions: no-opt
//FIXME: `opt` revision currently does not stop with an error due to <https://github.com/rust-lang/rust/issues/107503>

Check failure on line 2 in tests/ui/consts/required-consts/collect-in-dead-drop.rs

View workflow job for this annotation

GitHub Actions / PR - mingw-check-tidy

line longer than 100 chars
//@ build-fail
//@[opt] compile-flags: -O
//! Make sure we detect erroneous constants post-monomorphization even when they are unused. This is
//! crucial, people rely on it for soundness. (https://github.com/rust-lang/rust/issues/112090)

struct Fail<T>(T);
impl<T> Fail<T> {
const C: () = panic!(); //~ERROR evaluation of `Fail::<i32>::C` failed
}

// This function is not actually called, but is mentioned implicitly as destructor in dead code in a
// function that is called. Make sure we still find this error.
impl<T> Drop for Fail<T> {
fn drop(&mut self) {
let _ = Fail::<T>::C;
}
}

#[inline(never)]
fn called<T>(x: T) {
if false {
let v = Fail(x);
drop(v);
}
}

pub fn main() {
called::<i32>(0);
}
17 changes: 17 additions & 0 deletions tests/ui/consts/required-consts/collect-in-dead-fn.no-opt.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
error[E0080]: evaluation of `Fail::<i32>::C` failed
--> $DIR/collect-in-dead-fn.rs:10:19
|
LL | const C: () = panic!();
| ^^^^^^^^ the evaluated program panicked at 'explicit panic', $DIR/collect-in-dead-fn.rs:10:19
|
= 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)

note: the above error was encountered while instantiating `fn not_called::<i32>`
--> $DIR/collect-in-dead-fn.rs:27:9
|
LL | not_called::<T>();
| ^^^^^^^^^^^^^^^^^

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0080`.
33 changes: 33 additions & 0 deletions tests/ui/consts/required-consts/collect-in-dead-fn.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//@revisions: no-opt
//FIXME: `opt` revision currently does not stop with an error due to <https://github.com/rust-lang/rust/issues/107503>

Check failure on line 2 in tests/ui/consts/required-consts/collect-in-dead-fn.rs

View workflow job for this annotation

GitHub Actions / PR - mingw-check-tidy

line longer than 100 chars
//@ build-fail
//@[opt] compile-flags: -O
//! Make sure we detect erroneous constants post-monomorphization even when they are unused. This is
//! crucial, people rely on it for soundness. (https://github.com/rust-lang/rust/issues/112090)

struct Fail<T>(T);
impl<T> Fail<T> {
const C: () = panic!(); //~ERROR evaluation of `Fail::<i32>::C` failed
}

// This function is not actually called, but it is mentioned in dead code in a function that is
// called. Make sure we still find this error.
// This relies on mono-item collection checking `required_consts` in functions that syntactically
// are called in collected functions (even inside dead code).
#[inline(never)]
fn not_called<T>() {
if false {
let _ = Fail::<T>::C;
}
}

#[inline(never)]
fn called<T>() {
if false {
not_called::<T>();
}
}

pub fn main() {
called::<i32>();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
error[E0080]: evaluation of `Fail::<i32>::C` failed
--> $DIR/collect-in-dead-vtable.rs:10:19
|
LL | const C: () = panic!();
| ^^^^^^^^ the evaluated program panicked at 'explicit panic', $DIR/collect-in-dead-vtable.rs:10:19
|
= 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)

note: the above error was encountered while instantiating `fn <std::vec::Vec<i32> as MyTrait>::not_called`
--> $DIR/collect-in-dead-vtable.rs:33:40
|
LL | let gen_vtable: &dyn MyTrait = &v; // vtable "appears" here
| ^^

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0080`.
39 changes: 39 additions & 0 deletions tests/ui/consts/required-consts/collect-in-dead-vtable.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
//@revisions: no-opt
//FIXME: `opt` revision currently does not stop with an error due to <https://github.com/rust-lang/rust/issues/107503>

Check failure on line 2 in tests/ui/consts/required-consts/collect-in-dead-vtable.rs

View workflow job for this annotation

GitHub Actions / PR - mingw-check-tidy

line longer than 100 chars
//@ build-fail
//@[opt] compile-flags: -O
//! Make sure we detect erroneous constants post-monomorphization even when they are unused. This is
//! crucial, people rely on it for soundness. (https://github.com/rust-lang/rust/issues/112090)

struct Fail<T>(T);
impl<T> Fail<T> {
const C: () = panic!(); //~ERROR evaluation of `Fail::<i32>::C` failed
}

trait MyTrait {
fn not_called(&self);
}

// This function is not actually called, but it is mentioned in a vtable in a function that is
// called. Make sure we still find this error.
// This relies on mono-item collection checking `required_consts` in functions that are referenced
// in vtables that syntactically appear in collected functions (even inside dead code).
impl<T> MyTrait for Vec<T> {
fn not_called(&self) {
if false {
let _ = Fail::<T>::C;
}
}
}

#[inline(never)]
fn called<T>() {
if false {
let v: Vec<T> = Vec::new();
let gen_vtable: &dyn MyTrait = &v; // vtable "appears" here
}
}

pub fn main() {
called::<i32>();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
error[E0080]: evaluation of `Fail::<i32>::C` failed
--> $DIR/interpret-in-const-called-fn.rs:7:19
|
LL | const C: () = panic!();
| ^^^^^^^^ the evaluated program panicked at 'explicit panic', $DIR/interpret-in-const-called-fn.rs:7:19
|
= 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)

note: erroneous constant encountered
--> $DIR/interpret-in-const-called-fn.rs:16:9
|
LL | Fail::<T>::C;
| ^^^^^^^^^^^^

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0080`.
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
error[E0080]: evaluation of `Fail::<i32>::C` failed
--> $DIR/interpret-in-const-called-fn.rs:7:19
|
LL | const C: () = panic!();
| ^^^^^^^^ the evaluated program panicked at 'explicit panic', $DIR/interpret-in-const-called-fn.rs:7:19
|
= 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)

note: erroneous constant encountered
--> $DIR/interpret-in-const-called-fn.rs:16:9
|
LL | Fail::<T>::C;
| ^^^^^^^^^^^^

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0080`.
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
//@revisions: opt no-opt
//@[opt] compile-flags: -O
//! Make sure we error on erroneous consts even if they are unused.
#![allow(unconditional_panic)]

struct PrintName<T>(T);
impl<T> PrintName<T> {
const VOID: () = [()][2]; //~ERROR evaluation of `PrintName::<i32>::VOID` failed
struct Fail<T>(T);
impl<T> Fail<T> {
const C: () = panic!(); //~ERROR evaluation of `Fail::<i32>::C` failed
}

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

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
error[E0080]: evaluation of constant value failed
--> $SRC_DIR/core/src/hint.rs:LL:COL
|
= note: entering unreachable code
|
note: inside `unreachable_unchecked`
--> $SRC_DIR/core/src/hint.rs:LL:COL
note: inside `ub`
--> $DIR/interpret-in-promoted.rs:6:5
|
LL | std::hint::unreachable_unchecked();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
note: inside `FOO`
--> $DIR/interpret-in-promoted.rs:12:28
|
LL | let _x: &'static () = &ub();
| ^^^^

note: erroneous constant encountered
--> $DIR/interpret-in-promoted.rs:12:27
|
LL | let _x: &'static () = &ub();
| ^^^^^

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0080`.
Loading

0 comments on commit 219ce16

Please sign in to comment.