Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
matthiaskrgr opened this issue Feb 17, 2025 · 6 comments
Open

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

matthiaskrgr opened this issue Feb 17, 2025 · 6 comments
Labels
C-bug Category: This is a bug. F-trait_upcasting `#![feature(trait_upcasting)]` I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ S-bug-has-test Status: This bug is tracked inside the repo by a `known-bug` test. S-has-mcve Status: A Minimal Complete and Verifiable Example has been found for this issue T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@matthiaskrgr
Copy link
Member

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

@matthiaskrgr matthiaskrgr added C-bug Category: This is a bug. I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Feb 17, 2025
@rustbot rustbot added needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. -Zvalidate-mir Unstable option: MIR validation F-trait_upcasting `#![feature(trait_upcasting)]` labels Feb 17, 2025
@matthiaskrgr
Copy link
Member Author

bisects to #135318

@matthiaskrgr
Copy link
Member Author

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()
}

@matthiaskrgr
Copy link
Member Author

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

@cyrgani
Copy link
Contributor

cyrgani commented Feb 18, 2025

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.

@jieyouxu jieyouxu added S-has-mcve Status: A Minimal Complete and Verifiable Example has been found for this issue and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Feb 18, 2025
@compiler-errors
Copy link
Member

compiler-errors commented Feb 18, 2025

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();

@compiler-errors compiler-errors removed the -Zvalidate-mir Unstable option: MIR validation label Feb 18, 2025
@WaffleLapkin
Copy link
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;

@matthiaskrgr matthiaskrgr added the S-bug-has-test Status: This bug is tracked inside the repo by a `known-bug` test. label Mar 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug. F-trait_upcasting `#![feature(trait_upcasting)]` I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ S-bug-has-test Status: This bug is tracked inside the repo by a `known-bug` test. S-has-mcve Status: A Minimal Complete and Verifiable Example has been found for this issue T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

6 participants