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 <i32 as Baz>::c: Err(ErrorGuaranteed(())) #136381

Open
matthiaskrgr opened this issue Feb 1, 2025 · 3 comments
Labels
A-mir-opt-GVN Area: MIR opt Global Value Numbering (GVN) 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.

Comments

@matthiaskrgr
Copy link
Member

auto-reduced (treereduce-rust):

//@compile-flags: -Zmir-opt-level=5 -Zvalidate-mir
trait Foo: PartialEq<i32> + std::fmt::Debug + Send + Sync {}

trait Bar: Foo {
    fn w(&self) -> i32 {
        21
    }
}

trait Baz: Bar {}

impl Baz for i32 {}

fn main() {
    let baz: &dyn Baz = &1;

    let foo: &dyn Foo = baz;
}
original code

original:

//@ run-pass

#![feature(trait_upcasting)]

trait Foo: PartialEq<i32> + std::fmt::Debug + Send + Sync {
    fn a(&self) -> i32 {
        10
    }

    fn z(&self) -> i32 {
        11
    }

    fn y(&self) -> i32 {
        12
    }
}

trait Bar: Foo {
    fn b(&self) -> i32 {
        20
    }

    fn w(&self) -> i32 {
        21
    }
}

trait Baz: Bar {
    fn c(&self) -> i32 {
        30
    }
}

impl Foo for i32 {
    fn a(&self) -> i32 {
        100
    }
}

impl Bar for i32 {
    fn b(&self) -> i32 {
        200
    }
}

impl Baz for i32 {
    fn c(&<() as Identity>::Selff) -> i32 {
        300
    }
}

fn main() {
    let baz: &dyn Baz = &1;
    let _: &dyn std::fmt::Debug = baz;
    assert_eq!(*baz, 1);
    assert_eq!(baz.a(), 100);
    assert_eq!(baz.b(), 200);
    assert_eq!(baz.c(), 300);
    assert_eq!(baz.z(), 11);
    assert_eq!(baz.y(), 12);
    assert_eq!(baz.w(), 21);

    let bar: &dyn Bar = baz;
    let _: &dyn std::fmt::Debug = bar;
    assert_eq!(*bar, 1);
    assert_eq!(bar.a(), 100);
    assert_eq!(bar.b(), 200);
    assert_eq!(bar.z(), 11);
    assert_eq!(bar.y(), 12);
    assert_eq!(bar.w(), 21);

    let foo: &dyn Foo = baz;
    let _: &dyn std::fmt::Debug = foo;
    assert_eq!(*foo, 1);
    assert_eq!(foo.a(), 100);
    assert_eq!(foo.z(), 11);
    assert_eq!(foo.y(), 12);

    let foo: &dyn Foo = bar;
    let _: &dyn std::fmt::Debug = foo;
    assert_eq!(*foo, 1);
    assert_eq!(foo.a(), 100);
    assert_eq!(foo.z(), 11);
    assert_eq!(foo.y(), 12);
}

Version information

rustc 1.86.0-nightly (9e48dfe5e 2025-02-01)
binary: rustc
commit-hash: 9e48dfe5e0d5ec1b064b5d25504948ede0ec9b53
commit-date: 2025-02-01
host: x86_64-unknown-linux-gnu
release: 1.86.0-nightly
LLVM version: 19.1.7

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 `i32: Bar` is not satisfied
  --> /tmp/icemaker_global_tempdir.l3aOyLMWnNj0/rustc_testrunner_tmpdir_reporting.66VcXZBzeNme/mvce.rs:11:14
   |
11 | impl Baz for i32 {}
   |              ^^^ the trait `Bar` is not implemented for `i32`
   |
help: this trait has no implementations, consider adding one
  --> /tmp/icemaker_global_tempdir.l3aOyLMWnNj0/rustc_testrunner_tmpdir_reporting.66VcXZBzeNme/mvce.rs:3:1
   |
3  | trait Bar: Foo {
   | ^^^^^^^^^^^^^^
note: required by a bound in `Baz`
  --> /tmp/icemaker_global_tempdir.l3aOyLMWnNj0/rustc_testrunner_tmpdir_reporting.66VcXZBzeNme/mvce.rs:9:12
   |
9  | trait Baz: Bar {}
   |            ^^^ required by this bound in `Baz`

error[E0658]: cannot cast `dyn Baz` to `dyn Foo`, trait upcasting coercion is experimental
  --> /tmp/icemaker_global_tempdir.l3aOyLMWnNj0/rustc_testrunner_tmpdir_reporting.66VcXZBzeNme/mvce.rs:16:25
   |
16 |     let foo: &dyn Foo = baz;
   |                         ^^^
   |
   = note: see issue #65991 <https://github.com/rust-lang/rust/issues/65991> for more information
   = help: add `#![feature(trait_upcasting)]` to the crate attributes to enable
   = note: this compiler was built on 2025-02-01; consider upgrading it if it is out of date
   = note: required when coercing `&dyn Baz` into `&dyn Foo`

warning: unused variable: `foo`
  --> /tmp/icemaker_global_tempdir.l3aOyLMWnNj0/rustc_testrunner_tmpdir_reporting.66VcXZBzeNme/mvce.rs:16:9
   |
16 |     let foo: &dyn Foo = baz;
   |         ^^^ help: if this is intentional, prefix it with an underscore: `_foo`
   |
   = note: `#[warn(unused_variables)]` on by default

error: internal compiler error: compiler/rustc_middle/src/ty/instance.rs:603:21: failed to resolve instance for <i32 as Bar>::w
 --> /tmp/icemaker_global_tempdir.l3aOyLMWnNj0/rustc_testrunner_tmpdir_reporting.66VcXZBzeNme/mvce.rs:4:5
  |
4 |     fn w(&self) -> i32 {
  |     ^^^^^^^^^^^^^^^^^^


thread 'rustc' panicked at compiler/rustc_middle/src/ty/instance.rs:603:21:
Box<dyn Any>
stack backtrace:
   0:     0x7dc2d5a29bb0 - <std::sys::backtrace::BacktraceLock::print::DisplayBacktrace as core::fmt::Display>::fmt::h3b2667ac0fa900ee
   1:     0x7dc2d6213926 - core::fmt::write::h1e20c49f4105e8cb
   2:     0x7dc2d7552191 - std::io::Write::write_fmt::hcd547efb96ce3ce8
   3:     0x7dc2d5a29a12 - std::sys::backtrace::BacktraceLock::print::h01ae5c2f80bbe6a2
   4:     0x7dc2d5a2be92 - std::panicking::default_hook::{{closure}}::h38590813ec9a77f4
   5:     0x7dc2d5a2bd1a - std::panicking::default_hook::he0005f0d1786b50d
   6:     0x7dc2d4b85069 - std[a0d1a3d0cea39bfd]::panicking::update_hook::<alloc[7f35e0ab9d09dc5c]::boxed::Box<rustc_driver_impl[2c04243663399a5d]::install_ice_hook::{closure#1}>>::{closure#0}
   7:     0x7dc2d5a2c9d3 - std::panicking::rust_panic_with_hook::h2d8aa917e3df8004
   8:     0x7dc2d4bbfdc1 - std[a0d1a3d0cea39bfd]::panicking::begin_panic::<rustc_errors[ea6c319454a86de8]::ExplicitBug>::{closure#0}
   9:     0x7dc2d4bb4ca6 - std[a0d1a3d0cea39bfd]::sys::backtrace::__rust_end_short_backtrace::<std[a0d1a3d0cea39bfd]::panicking::begin_panic<rustc_errors[ea6c319454a86de8]::ExplicitBug>::{closure#0}, !>
  10:     0x7dc2d4bb4c8d - std[a0d1a3d0cea39bfd]::panicking::begin_panic::<rustc_errors[ea6c319454a86de8]::ExplicitBug>
  11:     0x7dc2d4bc9d11 - <rustc_errors[ea6c319454a86de8]::diagnostic::BugAbort as rustc_errors[ea6c319454a86de8]::diagnostic::EmissionGuarantee>::emit_producing_guarantee
  12:     0x7dc2d512df4c - <rustc_errors[ea6c319454a86de8]::DiagCtxtHandle>::span_bug::<rustc_span[b8e136aab0dfb1f0]::span_encoding::Span, alloc[7f35e0ab9d09dc5c]::string::String>
  13:     0x7dc2d51af667 - rustc_middle[1fcb647e943a03f5]::util::bug::opt_span_bug_fmt::<rustc_span[b8e136aab0dfb1f0]::span_encoding::Span>::{closure#0}
  14:     0x7dc2d51985da - rustc_middle[1fcb647e943a03f5]::ty::context::tls::with_opt::<rustc_middle[1fcb647e943a03f5]::util::bug::opt_span_bug_fmt<rustc_span[b8e136aab0dfb1f0]::span_encoding::Span>::{closure#0}, !>::{closure#0}
  15:     0x7dc2d519846b - rustc_middle[1fcb647e943a03f5]::ty::context::tls::with_context_opt::<rustc_middle[1fcb647e943a03f5]::ty::context::tls::with_opt<rustc_middle[1fcb647e943a03f5]::util::bug::opt_span_bug_fmt<rustc_span[b8e136aab0dfb1f0]::span_encoding::Span>::{closure#0}, !>::{closure#0}, !>
  16:     0x7dc2d3b11667 - rustc_middle[1fcb647e943a03f5]::util::bug::span_bug_fmt::<rustc_span[b8e136aab0dfb1f0]::span_encoding::Span>
  17:     0x7dc2d6937ad1 - <rustc_middle[1fcb647e943a03f5]::ty::instance::Instance>::expect_resolve
  18:     0x7dc2d6e87095 - <rustc_middle[1fcb647e943a03f5]::ty::instance::Instance>::expect_resolve_for_vtable
  19:     0x7dc2d69ea8fe - rustc_trait_selection[5ad581e41908d580]::traits::vtable::vtable_entries::{closure#0}
  20:     0x7dc2d716317d - rustc_trait_selection[5ad581e41908d580]::traits::vtable::vtable_entries
  21:     0x7dc2d7162ea0 - rustc_query_impl[ecbf26680a1b845f]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[ecbf26680a1b845f]::query_impl::vtable_entries::dynamic_query::{closure#2}::{closure#0}, rustc_middle[1fcb647e943a03f5]::query::erase::Erased<[u8; 16usize]>>
  22:     0x7dc2d7162e5f - <rustc_query_impl[ecbf26680a1b845f]::query_impl::vtable_entries::dynamic_query::{closure#2} as core[efe334e59b562b4]::ops::function::FnOnce<(rustc_middle[1fcb647e943a03f5]::ty::context::TyCtxt, rustc_type_ir[ec807296d52f2592]::predicate::TraitRef<rustc_middle[1fcb647e943a03f5]::ty::context::TyCtxt>)>>::call_once
  23:     0x7dc2d7161c27 - rustc_query_system[4b092dbe7cee5ac2]::query::plumbing::try_execute_query::<rustc_query_impl[ecbf26680a1b845f]::DynamicConfig<rustc_query_system[4b092dbe7cee5ac2]::query::caches::DefaultCache<rustc_type_ir[ec807296d52f2592]::predicate::TraitRef<rustc_middle[1fcb647e943a03f5]::ty::context::TyCtxt>, rustc_middle[1fcb647e943a03f5]::query::erase::Erased<[u8; 16usize]>>, false, false, false>, rustc_query_impl[ecbf26680a1b845f]::plumbing::QueryCtxt, false>
  24:     0x7dc2d716192f - rustc_query_impl[ecbf26680a1b845f]::query_impl::vtable_entries::get_query_non_incr::__rust_end_short_backtrace
  25:     0x7dc2d52e1d93 - <rustc_const_eval[84a83e4b3b990e9e]::interpret::eval_context::InterpCx<rustc_const_eval[84a83e4b3b990e9e]::const_eval::dummy_machine::DummyMachine>>::vtable_entries
  26:     0x7dc2d7c62e51 - <rustc_const_eval[84a83e4b3b990e9e]::interpret::eval_context::InterpCx<rustc_const_eval[84a83e4b3b990e9e]::const_eval::dummy_machine::DummyMachine>>::unsize_into_ptr.cold
  27:     0x7dc2d6d44f97 - <rustc_mir_transform[519422bbe054fe1f]::gvn::VnState>::insert
  28:     0x7dc2d6d3e9ab - <rustc_mir_transform[519422bbe054fe1f]::gvn::VnState>::simplify_rvalue::{closure#0}
  29:     0x7dc2d3a3fb4d - <rustc_mir_transform[519422bbe054fe1f]::gvn::GVN as rustc_mir_transform[519422bbe054fe1f]::pass_manager::MirPass>::run_pass
  30:     0x7dc2d62044a2 - rustc_mir_transform[519422bbe054fe1f]::pass_manager::run_passes_inner
  31:     0x7dc2d675f336 - rustc_mir_transform[519422bbe054fe1f]::optimized_mir
  32:     0x7dc2d675eb27 - rustc_query_impl[ecbf26680a1b845f]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[ecbf26680a1b845f]::query_impl::optimized_mir::dynamic_query::{closure#2}::{closure#0}, rustc_middle[1fcb647e943a03f5]::query::erase::Erased<[u8; 8usize]>>
  33:     0x7dc2d648a3df - rustc_query_system[4b092dbe7cee5ac2]::query::plumbing::try_execute_query::<rustc_query_impl[ecbf26680a1b845f]::DynamicConfig<rustc_query_system[4b092dbe7cee5ac2]::query::caches::DefIdCache<rustc_middle[1fcb647e943a03f5]::query::erase::Erased<[u8; 8usize]>>, false, false, false>, rustc_query_impl[ecbf26680a1b845f]::plumbing::QueryCtxt, false>
  34:     0x7dc2d64897df - rustc_query_impl[ecbf26680a1b845f]::query_impl::optimized_mir::get_query_non_incr::__rust_end_short_backtrace
  35:     0x7dc2d30cd644 - <rustc_middle[1fcb647e943a03f5]::ty::context::TyCtxt>::instance_mir
  36:     0x7dc2d65a6ac1 - rustc_interface[2a4b200aeca1f4ad]::passes::run_required_analyses
  37:     0x7dc2d6d1021e - rustc_interface[2a4b200aeca1f4ad]::passes::analysis
  38:     0x7dc2d6d101ef - rustc_query_impl[ecbf26680a1b845f]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[ecbf26680a1b845f]::query_impl::analysis::dynamic_query::{closure#2}::{closure#0}, rustc_middle[1fcb647e943a03f5]::query::erase::Erased<[u8; 0usize]>>
  39:     0x7dc2d71d9895 - rustc_query_system[4b092dbe7cee5ac2]::query::plumbing::try_execute_query::<rustc_query_impl[ecbf26680a1b845f]::DynamicConfig<rustc_query_system[4b092dbe7cee5ac2]::query::caches::SingleCache<rustc_middle[1fcb647e943a03f5]::query::erase::Erased<[u8; 0usize]>>, false, false, false>, rustc_query_impl[ecbf26680a1b845f]::plumbing::QueryCtxt, false>
  40:     0x7dc2d71d95ce - rustc_query_impl[ecbf26680a1b845f]::query_impl::analysis::get_query_non_incr::__rust_end_short_backtrace
  41:     0x7dc2d71a8d2b - rustc_interface[2a4b200aeca1f4ad]::passes::create_and_enter_global_ctxt::<core[efe334e59b562b4]::option::Option<rustc_interface[2a4b200aeca1f4ad]::queries::Linker>, rustc_driver_impl[2c04243663399a5d]::run_compiler::{closure#0}::{closure#2}>::{closure#2}::{closure#0}
  42:     0x7dc2d718a733 - rustc_interface[2a4b200aeca1f4ad]::interface::run_compiler::<(), rustc_driver_impl[2c04243663399a5d]::run_compiler::{closure#0}>::{closure#1}
  43:     0x7dc2d70d0935 - std[a0d1a3d0cea39bfd]::sys::backtrace::__rust_begin_short_backtrace::<rustc_interface[2a4b200aeca1f4ad]::util::run_in_thread_with_globals<rustc_interface[2a4b200aeca1f4ad]::util::run_in_thread_pool_with_globals<rustc_interface[2a4b200aeca1f4ad]::interface::run_compiler<(), rustc_driver_impl[2c04243663399a5d]::run_compiler::{closure#0}>::{closure#1}, ()>::{closure#0}, ()>::{closure#0}::{closure#0}, ()>
  44:     0x7dc2d70d0619 - <<std[a0d1a3d0cea39bfd]::thread::Builder>::spawn_unchecked_<rustc_interface[2a4b200aeca1f4ad]::util::run_in_thread_with_globals<rustc_interface[2a4b200aeca1f4ad]::util::run_in_thread_pool_with_globals<rustc_interface[2a4b200aeca1f4ad]::interface::run_compiler<(), rustc_driver_impl[2c04243663399a5d]::run_compiler::{closure#0}>::{closure#1}, ()>::{closure#0}, ()>::{closure#0}::{closure#0}, ()>::{closure#1} as core[efe334e59b562b4]::ops::function::FnOnce<()>>::call_once::{shim:vtable#0}
  45:     0x7dc2d70cfdaf - std::sys::pal::unix::thread::Thread::new::thread_start::h1c3d68b5dbee7695
  46:     0x7dc2d12a339d - <unknown>
  47:     0x7dc2d132849c - <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.86.0-nightly (9e48dfe5e 2025-02-01) 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 `Baz`
#1 [optimized_mir] optimizing MIR for `main`
#2 [analysis] running analysis passes on this crate
end of query stack
error: aborting due to 3 previous errors; 1 warning emitted

Some errors have detailed explanations: E0277, E0658.
For more information about an 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 1, 2025
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Feb 1, 2025
@matthiaskrgr
Copy link
Member Author

bisects to #135318

@matthiaskrgr matthiaskrgr added the A-mir-opt-GVN Area: MIR opt Global Value Numbering (GVN) label Feb 1, 2025
@matthiaskrgr
Copy link
Member Author

-Zmir-enable-passes=+GVN -Zvalidate-mir are the relevant flags

@matthiaskrgr
Copy link
Member Author

//@ compile-flags: -Zmir-enable-passes=+GVN -Zvalidate-mir
#![feature(trait_upcasting)]
trait Foo: PartialEq<i32> {}

trait Bar: Foo {
    fn w(&self) -> i32 {
        todo!();
    }
}

trait Baz: Bar {}

impl Baz for i32 {}
impl Foo for i32 {}
//impl Bar for i32 {}  // uncommenting this fixes the ICE



fn main() {
    let baz: &dyn Baz = &1;
    let foo: &dyn Foo = baz;
}

@saethlin saethlin removed the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Feb 1, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-mir-opt-GVN Area: MIR opt Global Value Numbering (GVN) 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.
Projects
None yet
Development

No branches or pull requests

3 participants