rust-lang / rust-lang/rust

Post-mono cycle ICE with dyn and RPIT

Open
#155,538 12 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-coercions A-coinduction A-dyn-trait A-impl-trait C-bug I-ICE P-medium regression-from-stable-to-stable S-blocked T-types
Dominant language
Rust
Stars
119k
Forks
16.2k
PR merge metrics
PR metrics pending

Description

Code

This is a variant of #150508. Unlike that issue, this issue ICEs in both the old solver and new solver.

trait Apply {
    type Output<T: Trait>: Trait;
}
struct Identity;
impl Apply for Identity {
    type Output<T: Trait> = T;
}

struct Thing<A: Apply>(A);

trait Trait {}

impl<A: Apply> Trait for Thing<A> where <A as Apply>::Output<Self>: Trait {}

/*
effectively:
impl Trait for Thing<Identity> where Thing<Identity>: Trait {}
*/

fn weird<A: Apply>(x: A) -> impl Trait {
    Thing(x)
}

fn main() {
    let _ = Box::new(weird(Identity)) as Box<dyn Trait>;
}

In the above code, each individual trait bound holds pre-monomorphization. However, after monomorphization, the required trait bound is Thing<Identity>: Trait. Trying to prove this bound runs into an inductive cycle.

cc @lcnr

Meta

Reproducible on the playground with version 1.97.0-nightly (2026-04-18 0febdbab2720dc0360cd)

Error output
error: internal compiler error: /rustc-dev/e22c616e4e87914135c1db261a03e0437255335e/compiler/rustc_mir_transform/src/validate.rs:82:25: broken MIR in Item(DefId(0:21 ~ playground[c925]::main)) (after phase change to runtime-optimized) at bb2[0]:
                                Unsize coercion, but `std::boxed::Box<Thing<Identity>>` isn't coercible to `std::boxed::Box<dyn Trait>`
  --> src/main.rs:25:13
   |
25 |     let _ = Box::new(weird(Identity)) as Box<dyn Trait>;
   |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^


thread 'rustc' (26) panicked at /rustc-dev/e22c616e4e87914135c1db261a03e0437255335e/compiler/rustc_mir_transform/src/validate.rs:82:25:
Box<dyn Any>
Backtrace

stack backtrace:
   0: std::panicking::begin_panic::<rustc_errors::ExplicitBug>
   1: <rustc_errors::diagnostic::BugAbort as rustc_errors::diagnostic::EmissionGuarantee>::emit_producing_guarantee
   2: <rustc_errors::DiagCtxtHandle>::span_bug::<rustc_span::span_encoding::Span, alloc::string::String>
   3: rustc_middle::util::bug::opt_span_bug_fmt::<rustc_span::span_encoding::Span>::{closure#0}
   4: rustc_middle::ty::context::tls::with_opt::<rustc_middle::util::bug::opt_span_bug_fmt<rustc_span::span_encoding::Span>::{closure#0}, !>::{closure#0}
   5: rustc_middle::ty::context::tls::with_context_opt::<rustc_middle::ty::context::tls::with_opt<rustc_middle::util::bug::opt_span_bug_fmt<rustc_span::span_encoding::Span>::{closure#0}, !>::{closure#0}, !>
   6: rustc_middle::util::bug::span_bug_fmt::<rustc_span::span_encoding::Span>
   7: <rustc_mir_transform::validate::CfgChecker>::fail::<alloc::string::String>
   8: <rustc_mir_transform::validate::Validator as rustc_mir_transform::pass_manager::MirPass>::run_pass
   9: rustc_mir_transform::run_optimization_passes
  10: rustc_mir_transform::optimized_mir
      [... omitted 1 frame ...]
  11: <rustc_middle::ty::context::TyCtxt>::instance_mir
  12: rustc_monomorphize::collector::items_of_instance
      [... omitted 1 frame ...]
  13: rustc_monomorphize::collector::collect_items_rec
  14: rustc_monomorphize::collector::collect_crate_mono_items::{closure#1}::{closure#0}
  15: rustc_monomorphize::partitioning::collect_and_partition_mono_items
      [... omitted 1 frame ...]
  16: rustc_codegen_ssa::base::codegen_crate::<rustc_codegen_llvm::LlvmCodegenBackend>
  17: <rustc_codegen_llvm::LlvmCodegenBackend as rustc_codegen_ssa::traits::backend::CodegenBackend>::codegen_crate
  18: <rustc_interface::queries::Linker>::codegen_and_build_linker
  19: rustc_interface::interface::run_compiler::<(), rustc_driver_impl::run_compiler::{closure#0}>::{closure#1}
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

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: please make sure that you have updated to the latest nightly

note: please attach the file at `/playground/rustc-ice-2026-04-20T04_48_17-24.txt` to your bug report

note: rustc 1.97.0-nightly (e22c616e4 2026-04-19) running on x86_64-unknown-linux-gnu

note: compiler flags: --crate-type bin -C embed-bitcode=no -C codegen-units=1 -C debuginfo=2

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

query stack during panic:
#0 [optimized_mir] optimizing MIR for `main`
#1 [items_of_instance] collecting items used by `main`
#2 [collect_and_partition_mono_items] collect_and_partition_mono_items
end of query stack

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the supplied example on the reported nightly, then inspect rustc_mir_transform/src/validate.rs around the reported validator failure and the monomorphization path named in the query stack. Trace how the post-monomorphization trait cycle reaches MIR validation; done means the example no longer causes an internal compiler error and is handled consistently by the compiler.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Active
Clarity
Needs clarification
Newbie friendliness
28/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.