Skip to content

ICE: Failed to resolve instance for <() as Supertrait<()>>::method #137190

Description

@matthiaskrgr

auto-reduced (treereduce-rust):

//@compile-flags: -Zmir-opt-level=5 -Zvalidate-mir
trait Supertrait<T> {
    fn method(&self) {}
}

trait Trait<P>: Supertrait<()> {}

impl<P> Trait<P> for () {}

fn upcast<P>(x: &dyn Trait<P>) -> &dyn Supertrait<()> {
    x
}

fn main() {
    upcast::<()>(&()).method();
}

original:

#![feature(trait_upcasting)]

trait Supertrait<T> {
    fn method(&self) {}
}
impl<T> Supertrait<T> for () {}

trait Identity {
    type Selff;
}
impl NonZeroF64 {
    pub fn new(_: f64) -> Option<Self> {
        None
    }
}
trait Trait<P>: Supertrait<()> + Supertrait<<P as Identity>::Selff> {}

impl<P> Trait<P> for () {}

fn upcast<P>(x: &dyn Trait<P>) -> &dyn Supertrait<()> {
    x
}

fn main() {
    upcast::<()>(&()).method();
}

Version information

rustc 1.87.0-nightly (ce36a966c 2025-02-17)
binary: rustc
commit-hash: ce36a966c79e109dabeef7a47fe68e5294c6d71e
commit-date: 2025-02-17
host: x86_64-unknown-linux-gnu
release: 1.87.0-nightly
LLVM version: 20.1.0

Possibly related line of code:

shrunk,
was_written,
path,
type_length,
});
} else {
span_bug!(
span_or_local_def_span(),
"failed to resolve instance for {}",
tcx.def_path_str_with_args(def_id, args)
)
}
}

Command:
/home/matthias/.rustup/toolchains/master/bin/rustc -Zmir-opt-level=5 -Zvalidate-mir

Program output

error[E0277]: the trait bound `(): Supertrait<()>` is not satisfied
 --> /tmp/icemaker_global_tempdir.qxoy71AFuMEA/rustc_testrunner_tmpdir_reporting.SjuyEgDtsyCA/mvce.rs:7:22
  |
7 | impl<P> Trait<P> for () {}
  |                      ^^ the trait `Supertrait<()>` is not implemented for `()`
  |
help: this trait has no implementations, consider adding one
 --> /tmp/icemaker_global_tempdir.qxoy71AFuMEA/rustc_testrunner_tmpdir_reporting.SjuyEgDtsyCA/mvce.rs:1:1
  |
1 | trait Supertrait<T> {
  | ^^^^^^^^^^^^^^^^^^^
note: required by a bound in `Trait`
 --> /tmp/icemaker_global_tempdir.qxoy71AFuMEA/rustc_testrunner_tmpdir_reporting.SjuyEgDtsyCA/mvce.rs:5:17
  |
5 | trait Trait<P>: Supertrait<()> {}
  |                 ^^^^^^^^^^^^^^ required by this bound in `Trait`

error: internal compiler error: compiler/rustc_middle/src/ty/instance.rs:603:21: failed to resolve instance for <() as Supertrait<()>>::method
 --> /tmp/icemaker_global_tempdir.qxoy71AFuMEA/rustc_testrunner_tmpdir_reporting.SjuyEgDtsyCA/mvce.rs:2:5
  |
2 |     fn method(&self) {}
  |     ^^^^^^^^^^^^^^^^


thread 'rustc' panicked at compiler/rustc_middle/src/ty/instance.rs:603:21:
Box<dyn Any>
stack backtrace:
   0:     0x77994b199e40 - <std::sys::backtrace::BacktraceLock::print::DisplayBacktrace as core::fmt::Display>::fmt::h3841cd57ff2ecd73
   1:     0x77994ba148ac - core::fmt::write::h1e3520bf56740af3
   2:     0x77994ca318d1 - std::io::Write::write_fmt::hba9e4167b3001b26
   3:     0x77994b199ca2 - std::sys::backtrace::BacktraceLock::print::h4cef63a231b9b903
   4:     0x77994b19c4a6 - std::panicking::default_hook::{{closure}}::h42a071c1742d1486
   5:     0x77994b19c084 - std::panicking::default_hook::hb623603f8ca8d5a9
   6:     0x77994a2e571b - std[3b75b760e1909bee]::panicking::update_hook::<alloc[fee51eceb6acdb2a]::boxed::Box<rustc_driver_impl[345723d7f8fea549]::install_ice_hook::{closure#1}>>::{closure#0}
   7:     0x77994b19cd53 - std::panicking::rust_panic_with_hook::h4a78a56a08a0281e
   8:     0x77994a3218a1 - std[3b75b760e1909bee]::panicking::begin_panic::<rustc_errors[ebdc752efb94ef64]::ExplicitBug>::{closure#0}
   9:     0x77994a315c86 - std[3b75b760e1909bee]::sys::backtrace::__rust_end_short_backtrace::<std[3b75b760e1909bee]::panicking::begin_panic<rustc_errors[ebdc752efb94ef64]::ExplicitBug>::{closure#0}, !>
  10:     0x77994a315a67 - std[3b75b760e1909bee]::panicking::begin_panic::<rustc_errors[ebdc752efb94ef64]::ExplicitBug>
  11:     0x77994a32b241 - <rustc_errors[ebdc752efb94ef64]::diagnostic::BugAbort as rustc_errors[ebdc752efb94ef64]::diagnostic::EmissionGuarantee>::emit_producing_guarantee
  12:     0x77994a87874c - <rustc_errors[ebdc752efb94ef64]::DiagCtxtHandle>::span_bug::<rustc_span[75e5c439b5acee8]::span_encoding::Span, alloc[fee51eceb6acdb2a]::string::String>
  13:     0x77994a8f8477 - rustc_middle[8f0bb18145f914db]::util::bug::opt_span_bug_fmt::<rustc_span[75e5c439b5acee8]::span_encoding::Span>::{closure#0}
  14:     0x77994a8e16da - rustc_middle[8f0bb18145f914db]::ty::context::tls::with_opt::<rustc_middle[8f0bb18145f914db]::util::bug::opt_span_bug_fmt<rustc_span[75e5c439b5acee8]::span_encoding::Span>::{closure#0}, !>::{closure#0}
  15:     0x77994a8e156b - rustc_middle[8f0bb18145f914db]::ty::context::tls::with_context_opt::<rustc_middle[8f0bb18145f914db]::ty::context::tls::with_opt<rustc_middle[8f0bb18145f914db]::util::bug::opt_span_bug_fmt<rustc_span[75e5c439b5acee8]::span_encoding::Span>::{closure#0}, !>::{closure#0}, !>
  16:     0x779948abe907 - rustc_middle[8f0bb18145f914db]::util::bug::span_bug_fmt::<rustc_span[75e5c439b5acee8]::span_encoding::Span>
  17:     0x77994c108492 - <rustc_middle[8f0bb18145f914db]::ty::instance::Instance>::expect_resolve
  18:     0x77994c414155 - <rustc_middle[8f0bb18145f914db]::ty::instance::Instance>::expect_resolve_for_vtable
  19:     0x77994c4cbfba - rustc_trait_selection[4ce5037939a8bd42]::traits::vtable::vtable_entries::{closure#0}
  20:     0x77994c4b9db4 - rustc_trait_selection[4ce5037939a8bd42]::traits::vtable::vtable_entries
  21:     0x77994c4b9aa0 - rustc_query_impl[1dc7e25d3505b9db]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[1dc7e25d3505b9db]::query_impl::vtable_entries::dynamic_query::{closure#2}::{closure#0}, rustc_middle[8f0bb18145f914db]::query::erase::Erased<[u8; 16usize]>>
  22:     0x77994c4b9a5f - <rustc_query_impl[1dc7e25d3505b9db]::query_impl::vtable_entries::dynamic_query::{closure#2} as core[22e5dcc507c1ce60]::ops::function::FnOnce<(rustc_middle[8f0bb18145f914db]::ty::context::TyCtxt, rustc_type_ir[c825c5250d010cf3]::predicate::TraitRef<rustc_middle[8f0bb18145f914db]::ty::context::TyCtxt>)>>::call_once
  23:     0x77994c4b892e - rustc_query_system[923c01520de53f9e]::query::plumbing::try_execute_query::<rustc_query_impl[1dc7e25d3505b9db]::DynamicConfig<rustc_query_system[923c01520de53f9e]::query::caches::DefaultCache<rustc_type_ir[c825c5250d010cf3]::predicate::TraitRef<rustc_middle[8f0bb18145f914db]::ty::context::TyCtxt>, rustc_middle[8f0bb18145f914db]::query::erase::Erased<[u8; 16usize]>>, false, false, false>, rustc_query_impl[1dc7e25d3505b9db]::plumbing::QueryCtxt, false>
  24:     0x77994c4b862f - rustc_query_impl[1dc7e25d3505b9db]::query_impl::vtable_entries::get_query_non_incr::__rust_end_short_backtrace
  25:     0x77994aa2dafa - <rustc_const_eval[24ba1ca4ddcda3d4]::interpret::eval_context::InterpCx<rustc_const_eval[24ba1ca4ddcda3d4]::const_eval::dummy_machine::DummyMachine>>::vtable_entries
  26:     0x77994d3f9917 - <rustc_const_eval[24ba1ca4ddcda3d4]::interpret::eval_context::InterpCx<rustc_const_eval[24ba1ca4ddcda3d4]::const_eval::dummy_machine::DummyMachine>>::unsize_into_ptr.cold
  27:     0x77994c53e614 - <rustc_mir_transform[f79e9594970959e5]::gvn::VnState>::insert
  28:     0x77994c53a4ba - <rustc_mir_transform[f79e9594970959e5]::gvn::VnState>::simplify_rvalue::{closure#0}
  29:     0x7799491e97e6 - <rustc_mir_transform[f79e9594970959e5]::gvn::GVN as rustc_mir_transform[f79e9594970959e5]::pass_manager::MirPass>::run_pass
  30:     0x77994ba05533 - rustc_mir_transform[f79e9594970959e5]::pass_manager::run_passes_inner
  31:     0x77994c1a197b - rustc_mir_transform[f79e9594970959e5]::optimized_mir
  32:     0x77994c1a11a1 - rustc_query_impl[1dc7e25d3505b9db]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[1dc7e25d3505b9db]::query_impl::optimized_mir::dynamic_query::{closure#2}::{closure#0}, rustc_middle[8f0bb18145f914db]::query::erase::Erased<[u8; 8usize]>>
  33:     0x77994bc2040a - rustc_query_system[923c01520de53f9e]::query::plumbing::try_execute_query::<rustc_query_impl[1dc7e25d3505b9db]::DynamicConfig<rustc_query_system[923c01520de53f9e]::query::caches::DefIdCache<rustc_middle[8f0bb18145f914db]::query::erase::Erased<[u8; 8usize]>>, false, false, false>, rustc_query_impl[1dc7e25d3505b9db]::plumbing::QueryCtxt, false>
  34:     0x77994bc1f833 - rustc_query_impl[1dc7e25d3505b9db]::query_impl::optimized_mir::get_query_non_incr::__rust_end_short_backtrace
  35:     0x77994bf58e4a - <rustc_middle[8f0bb18145f914db]::ty::context::TyCtxt>::instance_mir
  36:     0x77994bd60867 - rustc_interface[d53afbea1eb411d7]::passes::run_required_analyses
  37:     0x77994ca2d95e - rustc_interface[d53afbea1eb411d7]::passes::analysis
  38:     0x77994ca2d92f - rustc_query_impl[1dc7e25d3505b9db]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[1dc7e25d3505b9db]::query_impl::analysis::dynamic_query::{closure#2}::{closure#0}, rustc_middle[8f0bb18145f914db]::query::erase::Erased<[u8; 0usize]>>
  39:     0x77994ca17495 - rustc_query_system[923c01520de53f9e]::query::plumbing::try_execute_query::<rustc_query_impl[1dc7e25d3505b9db]::DynamicConfig<rustc_query_system[923c01520de53f9e]::query::caches::SingleCache<rustc_middle[8f0bb18145f914db]::query::erase::Erased<[u8; 0usize]>>, false, false, false>, rustc_query_impl[1dc7e25d3505b9db]::plumbing::QueryCtxt, false>
  40:     0x77994ca171ce - rustc_query_impl[1dc7e25d3505b9db]::query_impl::analysis::get_query_non_incr::__rust_end_short_backtrace
  41:     0x77994cadaf3c - rustc_interface[d53afbea1eb411d7]::passes::create_and_enter_global_ctxt::<core[22e5dcc507c1ce60]::option::Option<rustc_interface[d53afbea1eb411d7]::queries::Linker>, rustc_driver_impl[345723d7f8fea549]::run_compiler::{closure#0}::{closure#2}>::{closure#2}::{closure#0}
  42:     0x77994ca557e2 - rustc_interface[d53afbea1eb411d7]::interface::run_compiler::<(), rustc_driver_impl[345723d7f8fea549]::run_compiler::{closure#0}>::{closure#1}
  43:     0x77994c9d5898 - std[3b75b760e1909bee]::sys::backtrace::__rust_begin_short_backtrace::<rustc_interface[d53afbea1eb411d7]::util::run_in_thread_with_globals<rustc_interface[d53afbea1eb411d7]::util::run_in_thread_pool_with_globals<rustc_interface[d53afbea1eb411d7]::interface::run_compiler<(), rustc_driver_impl[345723d7f8fea549]::run_compiler::{closure#0}>::{closure#1}, ()>::{closure#0}, ()>::{closure#0}::{closure#0}, ()>
  44:     0x77994c9d5574 - <<std[3b75b760e1909bee]::thread::Builder>::spawn_unchecked_<rustc_interface[d53afbea1eb411d7]::util::run_in_thread_with_globals<rustc_interface[d53afbea1eb411d7]::util::run_in_thread_pool_with_globals<rustc_interface[d53afbea1eb411d7]::interface::run_compiler<(), rustc_driver_impl[345723d7f8fea549]::run_compiler::{closure#0}>::{closure#1}, ()>::{closure#0}, ()>::{closure#0}::{closure#0}, ()>::{closure#1} as core[22e5dcc507c1ce60]::ops::function::FnOnce<()>>::call_once::{shim:vtable#0}
  45:     0x77994c9d4d2b - std::sys::pal::unix::thread::Thread::new::thread_start::h9b930113dc9df217
  46:     0x779946aa339d - <unknown>
  47:     0x779946b2849c - <unknown>
  48:                0x0 - <unknown>

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: rustc 1.87.0-nightly (ce36a966c 2025-02-17) running on x86_64-unknown-linux-gnu

note: compiler flags: -Z mir-opt-level=5 -Z validate-mir -Z dump-mir-dir=dir

query stack during panic:
#0 [vtable_entries] finding all vtable entries for trait `Trait`
#1 [optimized_mir] optimizing MIR for `main`
#2 [analysis] running analysis passes on this crate
end of query stack
error: aborting due to 2 previous errors

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

@rustbot label +F-trait_upcasting +-Zvalidate-mir

Activity

  1. added
    C-bugCategory: This is a bug.
    I-ICEIssue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    on Feb 17, 2025
  2. added
    needs-triageThis issue may need triage. Remove when done. See docs forge.rust-lang.org/release/issue-triaging
    -Zvalidate-mirUnstable option: MIR validation
    on Feb 17, 2025
  3. matthiaskrgr commented on Feb 17, 2025

    @matthiaskrgr
    MemberAuthor

    bisects to #135318

  4. matthiaskrgr commented on Feb 17, 2025

    @matthiaskrgr
    MemberAuthor

    yeet!

    trait A {
        fn b(&self);
    }
    trait C: A {}
    impl C for () {}
    fn d<e>(f: &dyn C) -> &dyn A {
        f
    }
    fn g() {
        d::<()>(&()).b()
    }
  5. matthiaskrgr commented on Feb 17, 2025

    @matthiaskrgr
    MemberAuthor

    ah, this might be the none-errorguaranteed counterpart to #136381

  6. cyrgani commented on Feb 18, 2025

    @cyrgani
    Contributor

    a bit simpler:

    trait A {
        fn b(&self);
    }
    trait C: A {}
    impl C for () {}
    fn main() {
        (&() as &dyn C as &dyn A).b();
    }

    then it just needs rustc -Zmir-opt-level=2 -Zvalidate-mir main.rs.

  7. added
    S-has-mcveStatus: A Minimal Complete and Verifiable Example has been found for this issue
    and removed
    needs-triageThis issue may need triage. Remove when done. See docs forge.rust-lang.org/release/issue-triaging
    on Feb 18, 2025
  8. compiler-errors commented on Feb 18, 2025

    @compiler-errors
    Contributor

    This ICEs without any flags:

    trait Supertrait<T> {
        fn method(&self) {}
    }
    
    trait Trait<P>: Supertrait<()> {}
    
    impl<P> Trait<P> for () {}
    
    const fn upcast<P>(x: &dyn Trait<P>) -> &dyn Supertrait<()> {
        x
    }
    
    const fn foo() -> &'static dyn Supertrait<()> {
        upcast::<()>(&())
    }
    
    const _: &'static dyn Supertrait<()> = foo();
  9. WaffleLapkin commented on Feb 24, 2025

    @WaffleLapkin
    Member

    The generics and foo/upcast functions are not actually needed. All you need to trigger this ICE is an upcast in const to a supertrait which is not implemented:

    trait Supertrait {
        fn method(&self) {}
    }
    
    trait Trait: Supertrait {}
    
    impl Trait for () {}
    
    const _: &dyn Supertrait = &() as &dyn Trait as &dyn Supertrait;
  10. 12 remaining items

  11. added a commit that references this issue on Feb 9, 2026
    11b3cce
  12. added a commit that references this issue on Feb 9, 2026
    33f6045
  13. added a commit that references this issue on Feb 16, 2026
    4c88001
  14. workingjubilee commented on Feb 17, 2026

    @workingjubilee
    Member

    Unsound fix reverted by #152738

  15. added a commit that references this issue on Jun 4, 2026
  16. added a commit that references this issue on Jun 21, 2026
  17. added a commit that references this issue on Aug 7, 2026
    fcba4c2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-coercionsArea: implicit and explicit `expr as Type` coercionsA-trait-systemArea: Trait systemC-bugCategory: This is a bug.I-ICEIssue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️S-bug-has-testStatus: This bug is tracked inside the repo by a `known-bug` test.S-has-mcveStatus: A Minimal Complete and Verifiable Example has been found for this issueT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions