Skip to content

Commit fa4cc63

Browse files
committed
Auto merge of #110107 - cjgillot:const-prop-lint-junk, r=oli-obk
Ensure mir_drops_elaborated_and_const_checked when requiring codegen. mir_drops_elaborated_and_const_checked may emit errors while codegen has started, and the compiler would exit leaving object code files around. Found by `@cuviper` in #109731
2 parents 4a03f14 + 7e214bf commit fa4cc63

File tree

12 files changed

+65
-51
lines changed

12 files changed

+65
-51
lines changed

compiler/rustc_interface/src/passes.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -794,8 +794,14 @@ fn analysis(tcx: TyCtxt<'_>, (): ()) -> Result<()> {
794794
}
795795
tcx.ensure().has_ffi_unwind_calls(def_id);
796796

797-
if tcx.hir().body_const_context(def_id).is_some() {
797+
// If we need to codegen, ensure that we emit all errors from
798+
// `mir_drops_elaborated_and_const_checked` now, to avoid discovering
799+
// them later during codegen.
800+
if tcx.sess.opts.output_types.should_codegen()
801+
|| tcx.hir().body_const_context(def_id).is_some()
802+
{
798803
tcx.ensure().mir_drops_elaborated_and_const_checked(def_id);
804+
tcx.ensure().unused_generic_params(ty::InstanceDef::Item(def_id.to_def_id()));
799805
}
800806
}
801807
});

src/tools/miri/tests/fail/const-ub-checks.stderr

+6
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ error[E0080]: evaluation of constant value failed
44
LL | ptr.read();
55
| ^^^^^^^^^^ accessing memory with alignment ALIGN, but alignment ALIGN is required
66

7+
note: erroneous constant used
8+
--> $DIR/const-ub-checks.rs:LL:CC
9+
|
10+
LL | let _x = UNALIGNED_READ;
11+
| ^^^^^^^^^^^^^^
12+
713
error: aborting due to previous error
814

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

src/tools/miri/tests/fail/erroneous_const2.stderr

+14
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,20 @@ error[E0080]: evaluation of constant value failed
44
LL | const FOO: u32 = [X - Y, Y - X][(X < Y) as usize];
55
| ^^^^^ attempt to compute `5_u32 - 6_u32`, which would overflow
66

7+
note: erroneous constant used
8+
--> $DIR/erroneous_const2.rs:LL:CC
9+
|
10+
LL | println!("{}", FOO);
11+
| ^^^
12+
13+
note: erroneous constant used
14+
--> $DIR/erroneous_const2.rs:LL:CC
15+
|
16+
LL | println!("{}", FOO);
17+
| ^^^
18+
|
19+
= note: this note originates in the macro `$crate::format_args_nl` which comes from the expansion of the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info)
20+
721
error: aborting due to previous error
822

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

src/tools/miri/tests/pass/track-alloc-1.rs

-6
This file was deleted.

src/tools/miri/tests/pass/track-alloc-1.stderr

-5
This file was deleted.
+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
include ../tools.mk
2+
3+
# Test that emitting an error because of arithmetic
4+
# overflow lint does not leave .o files around
5+
# because of interrupted codegen.
6+
7+
all:
8+
$(RUSTC) input.rs; test $$? -eq 1
9+
ls *.o; test $$? -ne 0
+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#![deny(arithmetic_overflow)]
2+
3+
fn main() {
4+
let x = 255u8 + 1;
5+
}

tests/ui/consts/const-eval/issue-100878.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// This checks that the const-eval ICE in issue #100878 does not recur.
22
//
33
// build-pass
4+
5+
#[allow(arithmetic_overflow)]
46
pub fn bitshift_data(data: [u8; 1]) -> u8 {
57
data[0] << 8
68
}

tests/ui/issues/issue-33287.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// build-pass
22
#![allow(dead_code)]
33
#![allow(unused_variables)]
4+
#![allow(unconditional_panic)]
45
const A: [u32; 1] = [0];
56

67
fn test() {

tests/ui/polymorphization/generators.stderr

-12
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,6 @@ LL | pub fn unused_type<T>() -> impl Generator<(), Yield = u32, Return = u32> +
1515
LL | || {
1616
| ^^
1717

18-
note: the above error was encountered while instantiating `fn finish::<[generator@$DIR/generators.rs:35:5: 35:7], u32, u32>`
19-
--> $DIR/generators.rs:86:5
20-
|
21-
LL | finish(unused_type::<u32>());
22-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
23-
2418
error: item has unused generic parameters
2519
--> $DIR/generators.rs:60:5
2620
|
@@ -29,11 +23,5 @@ LL | pub fn unused_const<const T: u32>() -> impl Generator<(), Yield = u32, Retu
2923
LL | || {
3024
| ^^
3125

32-
note: the above error was encountered while instantiating `fn finish::<[generator@$DIR/generators.rs:60:5: 60:7], u32, u32>`
33-
--> $DIR/generators.rs:89:5
34-
|
35-
LL | finish(unused_const::<1u32>());
36-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
37-
3826
error: aborting due to 2 previous errors; 1 warning emitted
3927

tests/ui/polymorphization/predicates.stderr

+6-12
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
error: item has unused generic parameters
2+
--> $DIR/predicates.rs:10:4
3+
|
4+
LL | fn bar<I>() {
5+
| ^^^ - generic parameter `I` is unused
6+
17
error: item has unused generic parameters
28
--> $DIR/predicates.rs:15:4
39
|
@@ -35,17 +41,5 @@ error: item has unused generic parameters
3541
LL | fn foobar<F, G>() -> usize
3642
| ^^^^^^ - generic parameter `F` is unused
3743

38-
error: item has unused generic parameters
39-
--> $DIR/predicates.rs:10:4
40-
|
41-
LL | fn bar<I>() {
42-
| ^^^ - generic parameter `I` is unused
43-
44-
note: the above error was encountered while instantiating `fn foo::<std::slice::Iter<'_, u32>, T>`
45-
--> $DIR/predicates.rs:86:5
46-
|
47-
LL | foo(x.iter());
48-
| ^^^^^^^^^^^^^
49-
5044
error: aborting due to 6 previous errors
5145

tests/ui/polymorphization/type_parameters/closures.stderr

+15-15
Original file line numberDiff line numberDiff line change
@@ -43,21 +43,6 @@ LL | impl<F: Default> Foo<F> {
4343
LL | pub fn unused_all<G: Default>() -> u32 {
4444
| ^^^^^^^^^^ - generic parameter `G` is unused
4545

46-
error: item has unused generic parameters
47-
--> $DIR/closures.rs:128:23
48-
|
49-
LL | pub fn used_impl<G: Default>() -> u32 {
50-
| - generic parameter `G` is unused
51-
LL |
52-
LL | let add_one = |x: u32| {
53-
| ^^^^^^^^
54-
55-
error: item has unused generic parameters
56-
--> $DIR/closures.rs:126:12
57-
|
58-
LL | pub fn used_impl<G: Default>() -> u32 {
59-
| ^^^^^^^^^ - generic parameter `G` is unused
60-
6146
error: item has unused generic parameters
6247
--> $DIR/closures.rs:115:23
6348
|
@@ -76,5 +61,20 @@ LL | impl<F: Default> Foo<F> {
7661
LL | pub fn used_fn<G: Default>() -> u32 {
7762
| ^^^^^^^
7863

64+
error: item has unused generic parameters
65+
--> $DIR/closures.rs:128:23
66+
|
67+
LL | pub fn used_impl<G: Default>() -> u32 {
68+
| - generic parameter `G` is unused
69+
LL |
70+
LL | let add_one = |x: u32| {
71+
| ^^^^^^^^
72+
73+
error: item has unused generic parameters
74+
--> $DIR/closures.rs:126:12
75+
|
76+
LL | pub fn used_impl<G: Default>() -> u32 {
77+
| ^^^^^^^^^ - generic parameter `G` is unused
78+
7979
error: aborting due to 9 previous errors
8080

0 commit comments

Comments
 (0)