Skip to content

Commit e7f4399

Browse files
committed
more extensive dead cases for more kinds of dead code
1 parent 39a1d18 commit e7f4399

6 files changed

+111
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error[E0080]: evaluation of `Fail::<i32>::C` failed
2+
--> $DIR/dead-code-in-dead-drop.rs:9:19
3+
|
4+
LL | const C: () = panic!();
5+
| ^^^^^^^^ the evaluated program panicked at 'explicit panic', $DIR/dead-code-in-dead-drop.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 <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,14 @@
1+
error[E0080]: evaluation of `Fail::<i32>::C` failed
2+
--> $DIR/dead-code-in-dead-drop.rs:9:19
3+
|
4+
LL | const C: () = panic!();
5+
| ^^^^^^^^ the evaluated program panicked at 'explicit panic', $DIR/dead-code-in-dead-drop.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 <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,30 @@
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 is mentioned implicitly as destructor in dead code in a
13+
// function that is called. Make sure we still find this error.
14+
impl<T> Drop for Fail<T> {
15+
fn drop(&mut self) {
16+
let _ = Fail::<T>::C;
17+
}
18+
}
19+
20+
#[inline(never)]
21+
fn called<T>(x: T) {
22+
if false {
23+
let v = Fail(x);
24+
drop(v);
25+
}
26+
}
27+
28+
pub fn main() {
29+
called::<i32>(0);
30+
}

tests/ui/consts/monomorphization/dead-code-in-dead-fn.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ impl<T> Fail<T> {
99
const C: () = panic!(); //~ERROR evaluation of `Fail::<i32>::C` failed
1010
}
1111

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.
12+
// This function is not actually called, but it is mentioned in dead code in a function that is
13+
// called. Make sure we still find this error.
1414
#[inline(never)]
1515
fn not_called<T>() {
1616
let _ = Fail::<T>::C;
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-vtable.rs:9:19
3+
|
4+
LL | const C: () = panic!();
5+
| ^^^^^^^^ the evaluated program panicked at 'explicit panic', $DIR/dead-code-in-dead-vtable.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 <std::vec::Vec<i32> as MyTrait>::not_called`
10+
--> $DIR/dead-code-in-dead-vtable.rs:28:40
11+
|
12+
LL | let gen_vtable: &dyn MyTrait = &v;
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: 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+
trait MyTrait {
13+
fn not_called(&self);
14+
}
15+
16+
// This function is not actually called, but it is mentioned in a vtable in a function that is
17+
// called. Make sure we still find this error.
18+
impl<T> MyTrait for Vec<T> {
19+
fn not_called(&self) {
20+
let _ = Fail::<T>::C;
21+
}
22+
}
23+
24+
#[inline(never)]
25+
fn called<T>() {
26+
if false {
27+
let v: Vec<T> = Vec::new();
28+
let gen_vtable: &dyn MyTrait = &v;
29+
}
30+
}
31+
32+
pub fn main() {
33+
called::<i32>();
34+
}

0 commit comments

Comments
 (0)