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

Lines changed: 7 additions & 1 deletion
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

Lines changed: 6 additions & 0 deletions
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

Lines changed: 14 additions & 0 deletions
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

Lines changed: 0 additions & 6 deletions
This file was deleted.

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

Lines changed: 0 additions & 5 deletions
This file was deleted.
Lines changed: 9 additions & 0 deletions
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
Lines changed: 5 additions & 0 deletions
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

Lines changed: 2 additions & 0 deletions
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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 0 additions & 12 deletions
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

0 commit comments

Comments
 (0)