Repository: rust
Issue: ICE: Broken MIR while testing type compatibility
Labels: I-ICE,T-compiler,C-bug

I found a compiler crash that looks similar to #130921 and #131886.

### Code

[playground](https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=9346aece676bf91fdea1fcdc927496e2)

```Rust
pub trait BaseTrait {
    type BaseType;
}

// Works if trait bound `BaseTrait` becomes `BaseTrait<BaseType = ()>`
pub trait IntermediateTrait2<'a>: BaseTrait {}

// Works if trait bound `BaseTrait<BaeType = ()>` becomes `BaseTrait`
pub trait IntermediateTrait1: BaseTrait<BaseType = ()> {}

pub trait FinalTrait: IntermediateTrait1 + for<'a> IntermediateTrait2<'a> {}

struct IntermediateTraitsImpl;

impl BaseTrait for IntermediateTraitsImpl {
    type BaseType = ();
}
impl IntermediateTrait1 for IntermediateTraitsImpl {}
impl<'a> IntermediateTrait2<'a> for IntermediateTraitsImpl {}

// Works if `Box<dyn FinalTrait<BaseType = ()>>` becomes `Box<dyn FinalTrait>`
pub struct Foo {
    final_trait_impl: Box<dyn FinalTrait<BaseType = ()>>,
}

impl Foo {
    pub fn backends(&self) -> &dyn FinalTrait<BaseType = ()> {
        self.final_trait_impl.as_ref()
    }
}
```

### Meta
`rustc --version --verbose`:
```
rustc 1.83.0 (90b35a623 2024-11-26)
binary: rustc
commit-hash: 90b35a6239c3d8bdabc530a6a0816f7ff89a0aaf
commit-date: 2024-11-26
host: x86_64-unknown-linux-gnu
release: 1.83.0
LLVM version: 19.1.1
```

The bug also triggers in beta and nightly in a similar manner.

### Error output

```
thread 'rustc' panicked at compiler/rustc_mir_transform/src/validate.rs:95:25:
broken MIR in Item(DefId(0:20 ~ compiler_bug[9e9d]::{impl#3}::backends)) (after phase change to runtime-optimized) at bb0[0]:
encountered `Assign((_2, &((*_1).0: std::boxed::Box<dyn FinalTrait>)))` with incompatible types:
left-hand side has type: &Box<dyn FinalTrait>
right-hand side has type: &Box<dyn FinalTrait>
```

<details><summary><strong>Program output</strong></summary>
<p>

```
thread 'rustc' panicked at compiler/rustc_mir_transform/src/validate.rs:95:25:
broken MIR in Item(DefId(0:20 ~ compiler_bug[9e9d]::{impl#3}::backends)) (after phase change to runtime-optimized) at bb0[0]:
encountered `Assign((_2, &((*_1).0: std::boxed::Box<dyn FinalTrait>)))` with incompatible types:
left-hand side has type: &Box<dyn FinalTrait>
right-hand side has type: &Box<dyn FinalTrait>
stack backtrace:
   0:     0x75d55504012a - <std::sys::backtrace::BacktraceLock::print::DisplayBacktrace as core::fmt::Display>::fmt::h5b6bd5631a6d1f6b
   1:     0x75d5558218f8 - core::fmt::write::h7550c97b06c86515
   2:     0x75d556a58b91 - std::io::Write::write_fmt::h7b09c64fe0be9c84
   3:     0x75d55503ff82 - std::sys::backtrace::BacktraceLock::print::h2395ccd2c84ba3aa
   4:     0x75d555042456 - std::panicking::default_hook::{{closure}}::he19d4c7230e07961
   5:     0x75d5550422a0 - std::panicking::default_hook::hf614597d3c67bbdb
   6:     0x75d554104556 - std[c6eb78587944e35c]::panicking::update_hook::<alloc[148a978a4a62f5d]::boxed::Box<rustc_driver_impl[4c2d2ad79fb810ac]::install_ice_hook::{closure#0}>>::{closure#0}
   7:     0x75d555042b68 - std::panicking::rust_panic_with_hook::h8942133a8b252070
   8:     0x75d55504293a - std::panicking::begin_panic_handler::{{closure}}::hb5f5963570096b29
   9:     0x75d5550405d9 - std::sys::backtrace::__rust_end_short_backtrace::h6208cedc1922feda
  10:     0x75d5550425fc - rust_begin_unwind
  11:     0x75d552abb160 - core::panicking::panic_fmt::h0c3082644d1bf418
  12:     0x75d552e223e3 - <rustc_mir_transform[b36c87ceb4bb9a8e]::validate::CfgChecker>::fail::<alloc[148a978a4a62f5d]::string::String>
  13:     0x75d552e21752 - <rustc_mir_transform[b36c87ceb4bb9a8e]::validate::Validator as rustc_mir_transform[b36c87ceb4bb9a8e]::pass_manager::MirPass>::run_pass
  14:     0x75d55580b674 - rustc_mir_transform[b36c87ceb4bb9a8e]::pass_manager::run_passes_inner
  15:     0x75d555d0a9fa - rustc_mir_transform[b36c87ceb4bb9a8e]::optimized_mir
  16:     0x75d555d08369 - rustc_query_impl[db795c774d495014]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[db795c774d495014]::query_impl::optimized_mir::dynamic_query::{closure#2}::{closure#0}, rustc_middle[a886f61dbc61428a]::query::erase::Erased<[u8; 8usize]>>
  17:     0x75d555c5449a - rustc_query_system[b2bb6e43dd6b7fda]::query::plumbing::try_execute_query::<rustc_query_impl[db795c774d495014]::DynamicConfig<rustc_query_system[b2bb6e43dd6b7fda]::query::caches::DefIdCache<rustc_middle[a886f61dbc61428a]::query::erase::Erased<[u8; 8usize]>>, false, false, false>, rustc_query_impl[db795c774d495014]::plumbing::QueryCtxt, true>
  18:     0x75d555c52aa3 - rustc_query_impl[db795c774d495014]::query_impl::optimized_mir::get_query_incr::__rust_end_short_backtrace
  19:     0x75d5521210b5 - <rustc_middle[a886f61dbc61428a]::ty::context::TyCtxt>::instance_mir
  20:     0x75d5530f8d3a - rustc_monomorphize[4c5fa529dcdcd4f2]::collector::collect_items_rec::{closure#0}
  21:     0x75d5562be2b2 - rustc_monomorphize[4c5fa529dcdcd4f2]::collector::collect_items_rec
  22:     0x75d5562c4b3a - rustc_monomorphize[4c5fa529dcdcd4f2]::partitioning::collect_and_partition_mono_items
  23:     0x75d5565f5b16 - rustc_query_impl[db795c774d495014]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[db795c774d495014]::query_impl::collect_and_partition_mono_items::dynamic_query::{closure#2}::{closure#0}, rustc_middle[a886f61dbc61428a]::query::erase::Erased<[u8; 24usize]>>
  24:     0x75d5565f5adb - <rustc_query_impl[db795c774d495014]::query_impl::collect_and_partition_mono_items::dynamic_query::{closure#2} as core[c06ff78fa456ca03]::ops::function::FnOnce<(rustc_middle[a886f61dbc61428a]::ty::context::TyCtxt, ())>>::call_once
  25:     0x75d556802ea1 - rustc_query_system[b2bb6e43dd6b7fda]::query::plumbing::try_execute_query::<rustc_query_impl[db795c774d495014]::DynamicConfig<rustc_query_system[b2bb6e43dd6b7fda]::query::caches::SingleCache<rustc_middle[a886f61dbc61428a]::query::erase::Erased<[u8; 24usize]>>, false, false, false>, rustc_query_impl[db795c774d495014]::plumbing::QueryCtxt, true>
  26:     0x75d55680275a - rustc_query_impl[db795c774d495014]::query_impl::collect_and_partition_mono_items::get_query_incr::__rust_end_short_backtrace
  27:     0x75d5562d90cb - rustc_codegen_ssa[76f2c5b87770fd75]::back::symbol_export::exported_symbols_provider_local
  28:     0x75d555a4b839 - rustc_query_impl[db795c774d495014]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[db795c774d495014]::query_impl::exported_symbols::dynamic_query::{closure#2}::{closure#0}, rustc_middle[a886f61dbc61428a]::query::erase::Erased<[u8; 16usize]>>
  29:     0x75d555a4b80f - <rustc_query_impl[db795c774d495014]::query_impl::exported_symbols::dynamic_query::{closure#2} as core[c06ff78fa456ca03]::ops::function::FnOnce<(rustc_middle[a886f61dbc61428a]::ty::context::TyCtxt, rustc_span[3e5cf3424d44936d]::def_id::CrateNum)>>::call_once
  30:     0x75d55652995b - rustc_query_system[b2bb6e43dd6b7fda]::query::plumbing::try_execute_query::<rustc_query_impl[db795c774d495014]::DynamicConfig<rustc_query_system[b2bb6e43dd6b7fda]::query::caches::VecCache<rustc_span[3e5cf3424d44936d]::def_id::CrateNum, rustc_middle[a886f61dbc61428a]::query::erase::Erased<[u8; 16usize]>>, false, false, false>, rustc_query_impl[db795c774d495014]::plumbing::QueryCtxt, true>
  31:     0x75d556528f96 - rustc_query_impl[db795c774d495014]::query_impl::exported_symbols::get_query_incr::__rust_end_short_backtrace
  32:     0x75d5568da277 - <rustc_metadata[a330ceb16976a880]::rmeta::encoder::EncodeContext>::encode_crate_root
  33:     0x75d5568cc59a - rustc_metadata[a330ceb16976a880]::rmeta::encoder::encode_metadata
  34:     0x75d5568bdb33 - rustc_metadata[a330ceb16976a880]::fs::encode_and_write_metadata
  35:     0x75d5568bc9d6 - <rustc_interface[88a02114bbdb2383]::queries::Linker>::codegen_and_build_linker
  36:     0x75d5565621d4 - rustc_interface[88a02114bbdb2383]::interface::run_compiler::<core[c06ff78fa456ca03]::result::Result<(), rustc_span[3e5cf3424d44936d]::ErrorGuaranteed>, rustc_driver_impl[4c2d2ad79fb810ac]::run_compiler::{closure#0}>::{closure#1}
  37:     0x75d5565533d9 - std[c6eb78587944e35c]::sys::backtrace::__rust_begin_short_backtrace::<rustc_interface[88a02114bbdb2383]::util::run_in_thread_with_globals<rustc_interface[88a02114bbdb2383]::interface::run_compiler<core[c06ff78fa456ca03]::result::Result<(), rustc_span[3e5cf3424d44936d]::ErrorGuaranteed>, rustc_driver_impl[4c2d2ad79fb810ac]::run_compiler::{closure#0}>::{closure#1}, core[c06ff78fa456ca03]::result::Result<(), rustc_span[3e5cf3424d44936d]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[c06ff78fa456ca03]::result::Result<(), rustc_span[3e5cf3424d44936d]::ErrorGuaranteed>>
  38:     0x75d556622fac - <<std[c6eb78587944e35c]::thread::Builder>::spawn_unchecked_<rustc_interface[88a02114bbdb2383]::util::run_in_thread_with_globals<rustc_interface[88a02114bbdb2383]::interface::run_compiler<core[c06ff78fa456ca03]::result::Result<(), rustc_span[3e5cf3424d44936d]::ErrorGuaranteed>, rustc_driver_impl[4c2d2ad79fb810ac]::run_compiler::{closure#0}>::{closure#1}, core[c06ff78fa456ca03]::result::Result<(), rustc_span[3e5cf3424d44936d]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[c06ff78fa456ca03]::result::Result<(), rustc_span[3e5cf3424d44936d]::ErrorGuaranteed>>::{closure#1} as core[c06ff78fa456ca03]::ops::function::FnOnce<()>>::call_once::{shim:vtable#0}
  39:     0x75d556623a6b - std::sys::pal::unix::thread::Thread::new::thread_start::hcc78f3943333fa94
  40:     0x75d550aa339d - <unknown>
  41:     0x75d550b2849c - <unknown>
  42:                0x0 - <unknown>

error: the compiler unexpectedly panicked. this is a bug.

note: we would appreciate a bug report: https://github.com/rust-lang/rust/issues/new?labels=C-bug%2C+I-ICE%2C+T-compiler&template=ice.md

note: rustc 1.83.0 (90b35a623 2024-11-26) running on x86_64-unknown-linux-gnu

note: compiler flags: --crate-type lib -C embed-bitcode=no -C debuginfo=2 -C incremental=[REDACTED]

note: some of the compiler flags provided by cargo are hidden

query stack during panic:
#0 [optimized_mir] optimizing MIR for `<impl at src/lib.rs:26:1: 26:9>::backends`
#1 [collect_and_partition_mono_items] collect_and_partition_mono_items
end of query stack
```

</p>
</details>