Rollup merge of #147092 - cjgillot:late-validate-mir, r=compiler-errors

Do not compute optimized MIR if code does not type-check.

Since https://github.com/rust-lang/rust/pull/128612, we compute optimized MIR when `-Zvalidate-mir` is present.

This is done as part of required analyses, even if type-checking fails. This causes ICEs, as most of the mir-opt pipeline expects well-formed code.

Fixes rust-lang/rust#129095
Fixes rust-lang/rust#134174
Fixes rust-lang/rust#134654
Fixes rust-lang/rust#135570
Fixes rust-lang/rust#136381
Fixes rust-lang/rust#137468
Fixes rust-lang/rust#144491
Fixes rust-lang/rust#147011

This does not fix issue rust-lang/rust#137190, as it ICEs without `-Zvalidate-mir`.

r? ``@compiler-errors``
This commit is contained in:
Stuart Cook 2025-09-29 11:56:42 +10:00 committed by GitHub
commit 01b172ef33
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 14 additions and 116 deletions

View file

@ -1122,18 +1122,6 @@ fn run_required_analyses(tcx: TyCtxt<'_>) {
sess.time("layout_testing", || layout_test::test_layout(tcx));
sess.time("abi_testing", || abi_test::test_abi(tcx));
// If `-Zvalidate-mir` is set, we also want to compute the final MIR for each item
// (either its `mir_for_ctfe` or `optimized_mir`) since that helps uncover any bugs
// in MIR optimizations that may only be reachable through codegen, or other codepaths
// that requires the optimized/ctfe MIR, coroutine bodies, or evaluating consts.
if tcx.sess.opts.unstable_opts.validate_mir {
sess.time("ensuring_final_MIR_is_computable", || {
tcx.par_hir_body_owners(|def_id| {
tcx.instance_mir(ty::InstanceKind::Item(def_id.into()));
});
});
}
}
/// Runs the type-checking, region checking and other miscellaneous analysis
@ -1199,6 +1187,20 @@ fn analysis(tcx: TyCtxt<'_>, (): ()) {
// we will fail to emit overlap diagnostics. Thus we invoke it here unconditionally.
let _ = tcx.all_diagnostic_items(());
});
// If `-Zvalidate-mir` is set, we also want to compute the final MIR for each item
// (either its `mir_for_ctfe` or `optimized_mir`) since that helps uncover any bugs
// in MIR optimizations that may only be reachable through codegen, or other codepaths
// that requires the optimized/ctfe MIR, coroutine bodies, or evaluating consts.
// Nevertheless, wait after type checking is finished, as optimizing code that does not
// type-check is very prone to ICEs.
if tcx.sess.opts.unstable_opts.validate_mir {
sess.time("ensuring_final_MIR_is_computable", || {
tcx.par_hir_body_owners(|def_id| {
tcx.instance_mir(ty::InstanceKind::Item(def_id.into()));
});
});
}
}
/// Runs the codegen backend, after which the AST and analysis can

View file

@ -1,13 +0,0 @@
//@ known-bug: rust-lang/rust#129095
//@ compile-flags: -Zmir-enable-passes=+GVN -Zmir-enable-passes=+Inline -Zvalidate-mir
#![feature(adt_const_params, unsized_const_params)]
#![allow(incomplete_features)]
pub fn function_with_bytes<const BYTES: &'static [u8; 4]>() -> &'static [u8] {
BYTES
}
pub fn main() {
assert_eq!(function_with_bytes::<b"AAAAA">(), &[0x41, 0x41, 0x41, 0x41]);
}

View file

@ -1,17 +0,0 @@
//@ known-bug: #134175
//@compile-flags: -Zvalidate-mir -Zinline-mir=yes
use std::vec::IntoIter;
pub(crate) trait Foo: Iterator<Item = <Self as Foo>::Key> {
type Key;
}
impl Foo for IntoIter<i16> {}
fn sum_foo<F: Foo<Key = i32>>(f: F) -> i32 {
f.fold(0, |a, b| a + b)
}
fn main() {
let x = sum_foo(vec![11, 10, 1].into_iter());
}

View file

@ -1,15 +0,0 @@
//@ known-bug: #134654
//@ compile-flags: -Zmir-enable-passes=+GVN -Zmir-enable-passes=+Inline -Zvalidate-mir
//@ only-x86_64
#![feature(adt_const_params, unsized_const_params)]
#![allow(incomplete_features)]
fn function_with_bytes<const BYTES:
&'static [u8; 0xa9008fb6c9d81e42_0e25730562a601c8_u128]>() -> &'static [u8] {
BYTES
}
fn main() {
function_with_bytes::<b"aa">() == &[];
}

View file

@ -1,15 +0,0 @@
//@ known-bug: #135570
//@compile-flags: -Zvalidate-mir -Zmir-enable-passes=+Inline -Copt-level=0 -Zmir-enable-passes=+GVN
//@ only-x86_64
#![feature(adt_const_params, unsized_const_params)]
#![allow(incomplete_features)]
fn function_with_bytes<const BYTES: &'static [u8; 0xc7b889180b67b07d_bc1a3c88783d35b5_u128]>(
) -> &'static [u8] {
BYTES
}
fn main() {
function_with_bytes::<b"aa">() == &[];
}

View file

@ -1,18 +0,0 @@
//@ known-bug: #136381
//@ compile-flags: -Zvalidate-mir -Zmir-enable-passes=+GVN
#![feature(trait_upcasting)]
trait A {}
trait B: A {
fn c(&self);
}
impl B for i32 {
fn c(self) {
todo!();
}
}
fn main() {
let baz: &dyn B = &1;
let bar: &dyn A = baz;
}

View file

@ -1,10 +0,0 @@
//@ known-bug: #137190
//@ compile-flags: -Zmir-opt-level=2 -Zvalidate-mir
trait A {
fn b(&self);
}
trait C: A {}
impl C for () {}
fn main() {
(&() as &dyn C as &dyn A).b();
}

View file

@ -1,16 +0,0 @@
//@ known-bug: #137468
//@ compile-flags: -Copt-level=0 -Zmir-enable-passes=+GVN -Zvalidate-mir
trait Supertrait<T> {}
trait Identity {
type Selff;
}
trait Trait<P>: Supertrait<()> + Supertrait<<P as Identity>::Selff> {}
impl<P> Trait<P> for () {}
fn main() {
let x: &dyn Trait<()> = &();
let x: &dyn Supertrait<()> = x;
}