Skip to content

ICE: Unsize coercion, but &dyn Foo<'_> isn't coercible to &dyn Bar<'_, '_, ()> #131886

Open

Description

@matthiaskrgr

auto-reduced (treereduce-rust):

#![feature(trait_upcasting, type_alias_impl_trait)]

type Tait = impl Sized;

trait Foo<'a>: Bar<'a, 'a, Tait> {}
trait Bar<'a, 'b, T> {}

fn test_correct3<'a>(x: &dyn Foo<'a>, _: Tait) {
    let _ = x as &dyn Bar<'_, '_, ()>;
}

original:

#![feature(trait_upcasting, type_alias_impl_trait)]

//@ check-pass

type Tait = impl Sized;

trait Foo<'a>: Bar<'a, 'a, Tait> {}
trait Bar<'a, 'b, T> {}

fn test_correct(x: &dyn Foo<'static>) {
    let _ = x as &dyn Bar<'static, 'static, Tait>;
}

fn test_correct2<'a>(x: &dyn Foo<'a>) {
    let _ = x as &dyn Bar<'_, '_, Tait>;
}

fn test_correct3<'a>(x: &dyn Foo<'a>, _: Tait) {
    let _ = x as &dyn Bar<'_, '_, ()>;
}

Version information

rustc 1.84.0-nightly (db8043bb1 2024-10-18)
binary: rustc
commit-hash: db8043bb199705e72246ca43d4af1e9dbe7d55be
commit-date: 2024-10-18
host: x86_64-unknown-linux-gnu
release: 1.84.0-nightly
LLVM version: 19.1.1

Command:
/home/matthias/.rustup/toolchains/master/bin/rustc -Zvalidate-mir --crate-type=lib

Program output

thread 'rustc' panicked at compiler/rustc_mir_transform/src/validate.rs:95:25:
broken MIR in Item(DefId(0:11 ~ mvce[0ba5]::test_correct3)) (after pass CheckPackedRef) at bb0[3]:
Unsize coercion, but `&dyn Foo<'_>` isn't coercible to `&dyn Bar<'_, '_, ()>`
stack backtrace:
   0:     0x78597e480c7a - <std::sys::backtrace::BacktraceLock::print::DisplayBacktrace as core::fmt::Display>::fmt::h311c30006464a44d
   1:     0x78597ec03e0a - core::fmt::write::hefa9af5f84579548
   2:     0x78597ffa9291 - std::io::Write::write_fmt::h7b02b91c119616de
   3:     0x78597e480ad2 - std::sys::backtrace::BacktraceLock::print::h1bf9540f585b1c0c
   4:     0x78597e482fb6 - std::panicking::default_hook::{{closure}}::hdddf3b12f17177ae
   5:     0x78597e482e00 - std::panicking::default_hook::h95cfcd1ff904ee1d
   6:     0x78597d50280f - std[7ea95b089189ea8]::panicking::update_hook::<alloc[906de809b2d05e52]::boxed::Box<rustc_driver_impl[d25ecc56b0b66360]::install_ice_hook::{closure#0}>>::{closure#0}
   7:     0x78597e4836c8 - std::panicking::rust_panic_with_hook::h4741b39194e01f78
   8:     0x78597e48349a - std::panicking::begin_panic_handler::{{closure}}::h99fdffb7c066d2c5
   9:     0x78597e481129 - std::sys::backtrace::__rust_end_short_backtrace::h2eb0b5ad24f0eada
  10:     0x78597e48315c - rust_begin_unwind
  11:     0x78597aee8c20 - core::panicking::panic_fmt::h9d32603254f24257
  12:     0x78597b703213 - <rustc_mir_transform[7bdd74c785fb8b70]::validate::CfgChecker>::fail::<alloc[906de809b2d05e52]::string::String>
  13:     0x78597b701615 - <rustc_mir_transform[7bdd74c785fb8b70]::validate::Validator as rustc_mir_transform[7bdd74c785fb8b70]::pass_manager::MirPass>::run_pass
  14:     0x78597cde1a10 - rustc_mir_transform[7bdd74c785fb8b70]::pass_manager::validate_body
  15:     0x78597ec06d29 - rustc_mir_transform[7bdd74c785fb8b70]::mir_built
  16:     0x78597ec06907 - rustc_query_impl[32db4d22f0b27131]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[32db4d22f0b27131]::query_impl::mir_built::dynamic_query::{closure#2}::{closure#0}, rustc_middle[3555d1292876aeec]::query::erase::Erased<[u8; 8usize]>>
  17:     0x78597ef68d7a - rustc_query_system[4eccca7fcaf3aaf0]::query::plumbing::try_execute_query::<rustc_query_impl[32db4d22f0b27131]::DynamicConfig<rustc_query_system[4eccca7fcaf3aaf0]::query::caches::VecCache<rustc_span[1759084578182824]::def_id::LocalDefId, rustc_middle[3555d1292876aeec]::query::erase::Erased<[u8; 8usize]>>, false, false, false>, rustc_query_impl[32db4d22f0b27131]::plumbing::QueryCtxt, false>
  18:     0x78597ef6888d - rustc_query_impl[32db4d22f0b27131]::query_impl::mir_built::get_query_non_incr::__rust_end_short_backtrace
  19:     0x78597ec2c929 - rustc_mir_transform[7bdd74c785fb8b70]::ffi_unwind_calls::has_ffi_unwind_calls
  20:     0x78597ec2c2d5 - rustc_query_impl[32db4d22f0b27131]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[32db4d22f0b27131]::query_impl::has_ffi_unwind_calls::dynamic_query::{closure#2}::{closure#0}, rustc_middle[3555d1292876aeec]::query::erase::Erased<[u8; 1usize]>>
  21:     0x78597efbd167 - rustc_query_system[4eccca7fcaf3aaf0]::query::plumbing::try_execute_query::<rustc_query_impl[32db4d22f0b27131]::DynamicConfig<rustc_query_system[4eccca7fcaf3aaf0]::query::caches::VecCache<rustc_span[1759084578182824]::def_id::LocalDefId, rustc_middle[3555d1292876aeec]::query::erase::Erased<[u8; 1usize]>>, false, false, false>, rustc_query_impl[32db4d22f0b27131]::plumbing::QueryCtxt, false>
  22:     0x78597efbcd41 - rustc_query_impl[32db4d22f0b27131]::query_impl::has_ffi_unwind_calls::get_query_non_incr::__rust_end_short_backtrace
  23:     0x78597c0a075f - rustc_mir_transform[7bdd74c785fb8b70]::mir_promoted
  24:     0x78597f0e2292 - rustc_query_impl[32db4d22f0b27131]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[32db4d22f0b27131]::query_impl::mir_promoted::dynamic_query::{closure#2}::{closure#0}, rustc_middle[3555d1292876aeec]::query::erase::Erased<[u8; 16usize]>>
  25:     0x78597f0e255a - rustc_query_system[4eccca7fcaf3aaf0]::query::plumbing::try_execute_query::<rustc_query_impl[32db4d22f0b27131]::DynamicConfig<rustc_query_system[4eccca7fcaf3aaf0]::query::caches::VecCache<rustc_span[1759084578182824]::def_id::LocalDefId, rustc_middle[3555d1292876aeec]::query::erase::Erased<[u8; 16usize]>>, false, false, false>, rustc_query_impl[32db4d22f0b27131]::plumbing::QueryCtxt, false>
  26:     0x78597fc2d9d0 - rustc_query_impl[32db4d22f0b27131]::query_impl::mir_promoted::get_query_non_incr::__rust_end_short_backtrace
  27:     0x78597fc2dac3 - rustc_query_impl[32db4d22f0b27131]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[32db4d22f0b27131]::query_impl::mir_borrowck::dynamic_query::{closure#2}::{closure#0}, rustc_middle[3555d1292876aeec]::query::erase::Erased<[u8; 8usize]>>
  28:     0x78597ef68d7a - rustc_query_system[4eccca7fcaf3aaf0]::query::plumbing::try_execute_query::<rustc_query_impl[32db4d22f0b27131]::DynamicConfig<rustc_query_system[4eccca7fcaf3aaf0]::query::caches::VecCache<rustc_span[1759084578182824]::def_id::LocalDefId, rustc_middle[3555d1292876aeec]::query::erase::Erased<[u8; 8usize]>>, false, false, false>, rustc_query_impl[32db4d22f0b27131]::plumbing::QueryCtxt, false>
  29:     0x78597ef687d3 - rustc_query_impl[32db4d22f0b27131]::query_impl::mir_borrowck::get_query_non_incr::__rust_end_short_backtrace
  30:     0x78597f5c32f9 - rustc_middle[3555d1292876aeec]::query::plumbing::query_get_at::<rustc_query_system[4eccca7fcaf3aaf0]::query::caches::VecCache<rustc_span[1759084578182824]::def_id::LocalDefId, rustc_middle[3555d1292876aeec]::query::erase::Erased<[u8; 8usize]>>>
  31:     0x78597d6fc2d2 - <rustc_hir_analysis[3f1e84bf7d1c6f50]::collect::type_of::opaque::TaitConstraintLocator>::check
  32:     0x78597d69a8b2 - <rustc_hir_analysis[3f1e84bf7d1c6f50]::collect::type_of::opaque::TaitConstraintLocator as rustc_hir[5cb3b32afa60e663]::intravisit::Visitor>::visit_nested_item
  33:     0x78597fcca3b7 - rustc_hir_analysis[3f1e84bf7d1c6f50]::collect::type_of::type_of_opaque
  34:     0x78597fcc9ce5 - rustc_query_impl[32db4d22f0b27131]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[32db4d22f0b27131]::query_impl::type_of_opaque::dynamic_query::{closure#2}::{closure#0}, rustc_middle[3555d1292876aeec]::query::erase::Erased<[u8; 8usize]>>
  35:     0x78597ec2972a - rustc_query_system[4eccca7fcaf3aaf0]::query::plumbing::try_execute_query::<rustc_query_impl[32db4d22f0b27131]::DynamicConfig<rustc_query_system[4eccca7fcaf3aaf0]::query::caches::DefIdCache<rustc_middle[3555d1292876aeec]::query::erase::Erased<[u8; 8usize]>>, false, false, false>, rustc_query_impl[32db4d22f0b27131]::plumbing::QueryCtxt, false>
  36:     0x78597fe49bb6 - rustc_query_impl[32db4d22f0b27131]::query_impl::type_of_opaque::get_query_non_incr::__rust_end_short_backtrace
  37:     0x78597f3be502 - rustc_middle[3555d1292876aeec]::query::plumbing::query_get_at::<rustc_query_system[4eccca7fcaf3aaf0]::query::caches::DefIdCache<rustc_middle[3555d1292876aeec]::query::erase::Erased<[u8; 8usize]>>>
  38:     0x78597bda2135 - rustc_hir_analysis[3f1e84bf7d1c6f50]::collect::type_of::type_of
  39:     0x78597ec2aa30 - rustc_query_impl[32db4d22f0b27131]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[32db4d22f0b27131]::query_impl::type_of::dynamic_query::{closure#2}::{closure#0}, rustc_middle[3555d1292876aeec]::query::erase::Erased<[u8; 8usize]>>
  40:     0x78597ec2972a - rustc_query_system[4eccca7fcaf3aaf0]::query::plumbing::try_execute_query::<rustc_query_impl[32db4d22f0b27131]::DynamicConfig<rustc_query_system[4eccca7fcaf3aaf0]::query::caches::DefIdCache<rustc_middle[3555d1292876aeec]::query::erase::Erased<[u8; 8usize]>>, false, false, false>, rustc_query_impl[32db4d22f0b27131]::plumbing::QueryCtxt, false>
  41:     0x78597ec292e3 - rustc_query_impl[32db4d22f0b27131]::query_impl::type_of::get_query_non_incr::__rust_end_short_backtrace
  42:     0x78597f3be502 - rustc_middle[3555d1292876aeec]::query::plumbing::query_get_at::<rustc_query_system[4eccca7fcaf3aaf0]::query::caches::DefIdCache<rustc_middle[3555d1292876aeec]::query::erase::Erased<[u8; 8usize]>>>
  43:     0x78597fcd57d9 - rustc_hir_analysis[3f1e84bf7d1c6f50]::check::check::check_item_type
  44:     0x78597bba12ed - rustc_hir_analysis[3f1e84bf7d1c6f50]::check::wfcheck::check_well_formed
  45:     0x78597efbdac7 - rustc_query_impl[32db4d22f0b27131]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[32db4d22f0b27131]::query_impl::check_well_formed::dynamic_query::{closure#2}::{closure#0}, rustc_middle[3555d1292876aeec]::query::erase::Erased<[u8; 1usize]>>
  46:     0x78597efbd21f - rustc_query_system[4eccca7fcaf3aaf0]::query::plumbing::try_execute_query::<rustc_query_impl[32db4d22f0b27131]::DynamicConfig<rustc_query_system[4eccca7fcaf3aaf0]::query::caches::VecCache<rustc_span[1759084578182824]::def_id::LocalDefId, rustc_middle[3555d1292876aeec]::query::erase::Erased<[u8; 1usize]>>, false, false, false>, rustc_query_impl[32db4d22f0b27131]::plumbing::QueryCtxt, false>
  47:     0x78597efbce90 - rustc_query_impl[32db4d22f0b27131]::query_impl::check_well_formed::get_query_non_incr::__rust_end_short_backtrace
  48:     0x78597efbdb47 - rustc_middle[3555d1292876aeec]::query::plumbing::query_ensure_error_guaranteed::<rustc_query_system[4eccca7fcaf3aaf0]::query::caches::VecCache<rustc_span[1759084578182824]::def_id::LocalDefId, rustc_middle[3555d1292876aeec]::query::erase::Erased<[u8; 1usize]>>, ()>
  49:     0x78597efbe11b - rustc_hir_analysis[3f1e84bf7d1c6f50]::check::wfcheck::check_mod_type_wf
  50:     0x78597efbdb6f - rustc_query_impl[32db4d22f0b27131]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[32db4d22f0b27131]::query_impl::check_mod_type_wf::dynamic_query::{closure#2}::{closure#0}, rustc_middle[3555d1292876aeec]::query::erase::Erased<[u8; 1usize]>>
  51:     0x78597f74533b - rustc_query_system[4eccca7fcaf3aaf0]::query::plumbing::try_execute_query::<rustc_query_impl[32db4d22f0b27131]::DynamicConfig<rustc_query_system[4eccca7fcaf3aaf0]::query::caches::DefaultCache<rustc_span[1759084578182824]::def_id::LocalModDefId, rustc_middle[3555d1292876aeec]::query::erase::Erased<[u8; 1usize]>>, false, false, false>, rustc_query_impl[32db4d22f0b27131]::plumbing::QueryCtxt, false>
  52:     0x78597f7450ed - rustc_query_impl[32db4d22f0b27131]::query_impl::check_mod_type_wf::get_query_non_incr::__rust_end_short_backtrace
  53:     0x78597ef648bb - rustc_hir_analysis[3f1e84bf7d1c6f50]::check_crate
  54:     0x78597f519f57 - rustc_interface[ca54b24fb50a6540]::passes::run_required_analyses
  55:     0x78597face5de - rustc_interface[ca54b24fb50a6540]::passes::analysis
  56:     0x78597face5b1 - rustc_query_impl[32db4d22f0b27131]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[32db4d22f0b27131]::query_impl::analysis::dynamic_query::{closure#2}::{closure#0}, rustc_middle[3555d1292876aeec]::query::erase::Erased<[u8; 1usize]>>
  57:     0x78597fb7a56e - rustc_query_system[4eccca7fcaf3aaf0]::query::plumbing::try_execute_query::<rustc_query_impl[32db4d22f0b27131]::DynamicConfig<rustc_query_system[4eccca7fcaf3aaf0]::query::caches::SingleCache<rustc_middle[3555d1292876aeec]::query::erase::Erased<[u8; 1usize]>>, false, false, false>, rustc_query_impl[32db4d22f0b27131]::plumbing::QueryCtxt, false>
  58:     0x78597fb7a24f - rustc_query_impl[32db4d22f0b27131]::query_impl::analysis::get_query_non_incr::__rust_end_short_backtrace
  59:     0x78597fa0e91e - rustc_interface[ca54b24fb50a6540]::interface::run_compiler::<core[8e2925c99194b988]::result::Result<(), rustc_span[1759084578182824]::ErrorGuaranteed>, rustc_driver_impl[d25ecc56b0b66360]::run_compiler::{closure#0}>::{closure#1}
  60:     0x78597faab714 - std[7ea95b089189ea8]::sys::backtrace::__rust_begin_short_backtrace::<rustc_interface[ca54b24fb50a6540]::util::run_in_thread_with_globals<rustc_interface[ca54b24fb50a6540]::util::run_in_thread_pool_with_globals<rustc_interface[ca54b24fb50a6540]::interface::run_compiler<core[8e2925c99194b988]::result::Result<(), rustc_span[1759084578182824]::ErrorGuaranteed>, rustc_driver_impl[d25ecc56b0b66360]::run_compiler::{closure#0}>::{closure#1}, core[8e2925c99194b988]::result::Result<(), rustc_span[1759084578182824]::ErrorGuaranteed>>::{closure#0}, core[8e2925c99194b988]::result::Result<(), rustc_span[1759084578182824]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[8e2925c99194b988]::result::Result<(), rustc_span[1759084578182824]::ErrorGuaranteed>>
  61:     0x78597faabb28 - <<std[7ea95b089189ea8]::thread::Builder>::spawn_unchecked_<rustc_interface[ca54b24fb50a6540]::util::run_in_thread_with_globals<rustc_interface[ca54b24fb50a6540]::util::run_in_thread_pool_with_globals<rustc_interface[ca54b24fb50a6540]::interface::run_compiler<core[8e2925c99194b988]::result::Result<(), rustc_span[1759084578182824]::ErrorGuaranteed>, rustc_driver_impl[d25ecc56b0b66360]::run_compiler::{closure#0}>::{closure#1}, core[8e2925c99194b988]::result::Result<(), rustc_span[1759084578182824]::ErrorGuaranteed>>::{closure#0}, core[8e2925c99194b988]::result::Result<(), rustc_span[1759084578182824]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[8e2925c99194b988]::result::Result<(), rustc_span[1759084578182824]::ErrorGuaranteed>>::{closure#1} as core[8e2925c99194b988]::ops::function::FnOnce<()>>::call_once::{shim:vtable#0}
  62:     0x78597faac5eb - std::sys::pal::unix::thread::Thread::new::thread_start::hfa6ba0429166ab6d
  63:     0x78598132939d - <unknown>
  64:     0x7859813ae49c - <unknown>
  65:                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: please make sure that you have updated to the latest nightly

note: rustc 1.84.0-nightly (db8043bb1 2024-10-18) running on x86_64-unknown-linux-gnu

note: compiler flags: -Z validate-mir --crate-type lib -Z dump-mir-dir=dir

query stack during panic:
#0 [mir_built] building MIR for `test_correct3`
#1 [has_ffi_unwind_calls] checking if `test_correct3` contains FFI-unwind calls
end of query stack

@rustbot label +F-trait_upcasting +F-type_alias_impl_trait

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 Oct 18, 2024
  2. added
    needs-triageThis issue may need triage. Remove when done. See docs forge.rust-lang.org/release/issue-triaging
    on Oct 18, 2024
  3. added
    -Zvalidate-mirUnstable option: MIR validation
    and removed
    needs-triageThis issue may need triage. Remove when done. See docs forge.rust-lang.org/release/issue-triaging
    on Oct 18, 2024
  4. added
    S-bug-has-testStatus: This bug is tracked inside the repo by a `known-bug` test.
    on Oct 20, 2024
  5. WaffleLapkin commented on Dec 4, 2024

    @WaffleLapkin
    Member

    The lifetimes are unnecessary, this also fails when using -Zvalidate-mir:

    #![feature(trait_upcasting, type_alias_impl_trait)]
    
    type Tait = impl Sized;
    
    trait Foo: Bar<Tait> {}
    trait Bar<T> {}
    
    fn test_correct3(x: &dyn Foo, _: Tait) {
        _ = x as &dyn Bar<()>;
    }

    Specifically the problem seems to be with using the hidden type in upcasting, when an opaque is expected.

  6. crlf0710 commented on Dec 10, 2024

    @crlf0710
    Member

    Tried all three code snippets on playground:

    Build using the Nightly version: 1.85.0-nightly
    
    (2024-12-09 a224f3807e58afc93535)
    

    None of the snippets ICEs.

    Might worth adding some (or all) of them to test suites.

  7. WaffleLapkin commented on Dec 10, 2024

    @WaffleLapkin
    Member

    @crlf0710 they only ICE with -Zvalidate-mir, which you can't enable on playground as far as I know

  8. cjgillot commented on Oct 15, 2025

    @cjgillot
    Contributor

    -Zvalidate-mir ICEing on erroneous code. Marking as fixed by #147092

  9. added
    fixed-by-next-solverFixed by the next-generation trait solver, `-Znext-solver`.
    on Oct 5, 2026
  10. lcnr commented on Oct 5, 2026

    @lcnr
    Contributor

    not actually fixed by that

  11. reopened this on Oct 5, 2026
  12. added 3 commits that reference this issue on Oct 7, 2026
  13. added a commit that references this issue on Oct 8, 2026
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

    -Zvalidate-mirUnstable option: MIR validationC-bugCategory: This is a bug.F-trait_upcasting`#![feature(trait_upcasting)]`F-type_alias_impl_trait`#[feature(type_alias_impl_trait)]`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.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.fixed-by-next-solverFixed by the next-generation trait solver, `-Znext-solver`.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions