Skip to content

Attempting to store function callback of a particular incorrect type produces ICE #140823

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

Closed
jagunter opened this issue May 8, 2025 · 2 comments · Fixed by #141236
Closed

Attempting to store function callback of a particular incorrect type produces ICE #140823

jagunter opened this issue May 8, 2025 · 2 comments · Fixed by #141236
Labels
C-bug Category: This is a bug. I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ S-has-bisection Status: A bisection has been found for this issue 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

@jagunter
Copy link
Contributor

jagunter commented May 8, 2025

Code

struct Container<T> {
    data: T,
}

fn ice(callback: Box<dyn Fn(Container<&u8>)>) {
    let fails: Box<dyn Fn(&Container<&u8>)> = callback;
}

Notes:

  • The reference mismatch in function callback arguments is required to repro (&Container<...> vs Container<....>)
  • The Container needs to take a generic argument to repro, and the argument value needs to be a reference. Using Container<u8> or Container<Vec<u8>> does not repro.
  • Swapping the types of fails and callback not repro.
  • Also repros when replacing Container with real containers like Vec or VecDeque

Meta

rustc --version --verbose:

rustc 1.86.0 (05f9846f8 2025-03-31)
binary: rustc
commit-hash: 05f9846f893b09a1be1fc8560e33fc3c815cfecb
commit-date: 2025-03-31
host: x86_64-unknown-linux-gnu
release: 1.86.0
LLVM version: 19.1.7

Confirmed bug reproduces on nightly. Traces are from stable.

rustc +nightly --version --verbose
rustc 1.88.0-nightly (e9f8103f9 2025-05-07)
binary: rustc
commit-hash: e9f8103f93f8ce2fa2c15c0c6796ec821f8ae15d
commit-date: 2025-05-07
host: x86_64-unknown-linux-gnu
release: 1.88.0-nightly
LLVM version: 20.1.4

Error output

thread 'rustc' panicked at /rust/deps/ena-0.14.3/src/snapshot_vec.rs:199:10:
index out of bounds: the len is 1 but the index is 1
Backtrace

Without RUST_BACKTRACE=1

Including since there's a stacktrace here that doesn't appear otherwise

   Compiling ice v0.1.0 (/home/user/projects/rust-ice/ice)

thread 'rustc' panicked at /rust/deps/ena-0.14.3/src/snapshot_vec.rs:199:10:
index out of bounds: the len is 1 but the index is 1
stack backtrace:
   0:     0x7f7471bd6e40 - <std::sys::backtrace::BacktraceLock::print::DisplayBacktrace as core::fmt::Display>::fmt::h6d42cc84fc840290
   1:     0x7f747242867c - core::fmt::write::h5af61a909e3ec64d
   2:     0x7f747372ca51 - std::io::Write::write_fmt::h5a7b54aa6e4a315d
   3:     0x7f7471bd6ca2 - std::sys::backtrace::BacktraceLock::print::h555579e7396c26ac
   4:     0x7f7471bd9122 - std::panicking::default_hook::{{closure}}::h9128866118196224
   5:     0x7f7471bd8faa - std::panicking::default_hook::h52e9e7314e0255f6
   6:     0x7f7470d33449 - std[d9e466a2d75004a2]::panicking::update_hook::<alloc[54bfe2542ace865d]::boxed::Box<rustc_driver_impl[680c351c5444d7cd]::install_ice_hook::{closure#1}>>::{closure#0}
   7:     0x7f7471bd9ca3 - std::panicking::rust_panic_with_hook::h541791bcc774ef34
   8:     0x7f7471bd999a - std::panicking::begin_panic_handler::{{closure}}::h6479a2f0137c7d19
   9:     0x7f7471bd7329 - std::sys::backtrace::__rust_end_short_backtrace::ha04e7c0fc61ded91
  10:     0x7f7471bd965d - rust_begin_unwind
  11:     0x7f746e83d920 - core::panicking::panic_fmt::h5764ee7030b7a73d
  12:     0x7f7470459089 - core::panicking::panic_bounds_check::h0328ca7e7f0749c4
  13:     0x7f74738f7ce7 - <ena[1fe2efd429ac361d]::unify::UnificationTable<ena[1fe2efd429ac361d]::unify::backing_vec::InPlace<rustc_infer[86fba76a03fa934a]::infer::unify_key::RegionVidKey, &mut alloc[54bfe2542ace865d]::vec::Vec<ena[1fe2efd429ac361d]::unify::VarValue<rustc_infer[86fba76a03fa934a]::infer::unify_key::RegionVidKey>>, &mut rustc_infer[86fba76a03fa934a]::infer::snapshot::undo_log::InferCtxtUndoLogs>>>::uninlined_get_root_key.cold
  14:     0x7f747258a7d3 - rustc_type_ir[282c50670f22d67a]::relate::structurally_relate_tys::<rustc_middle[a78c8f6c75429410]::ty::context::TyCtxt, rustc_infer[86fba76a03fa934a]::infer::relate::type_relating::TypeRelating>::{closure#0}
  15:     0x7f74725857a1 - <rustc_infer[86fba76a03fa934a]::infer::relate::type_relating::TypeRelating as rustc_type_ir[282c50670f22d67a]::relate::TypeRelation<rustc_middle[a78c8f6c75429410]::ty::context::TyCtxt>>::tys
  16:     0x7f7472589f17 - rustc_type_ir[282c50670f22d67a]::relate::structurally_relate_tys::<rustc_middle[a78c8f6c75429410]::ty::context::TyCtxt, rustc_infer[86fba76a03fa934a]::infer::relate::type_relating::TypeRelating>::{closure#0}
  17:     0x7f74725857a1 - <rustc_infer[86fba76a03fa934a]::infer::relate::type_relating::TypeRelating as rustc_type_ir[282c50670f22d67a]::relate::TypeRelation<rustc_middle[a78c8f6c75429410]::ty::context::TyCtxt>>::tys
  18:     0x7f7472583e96 - <rustc_infer[86fba76a03fa934a]::infer::at::At>::eq_trace::<rustc_middle[a78c8f6c75429410]::ty::Ty>
  19:     0x7f7470f3386c - <rustc_infer[86fba76a03fa934a]::infer::InferCtxt>::probe::<bool, <rustc_infer[86fba76a03fa934a]::infer::InferCtxt as rustc_trait_selection[2bf3b2046bc969f2]::infer::InferCtxtExt>::can_eq<rustc_middle[a78c8f6c75429410]::ty::Ty>::{closure#0}>
  20:     0x7f7471024760 - <rustc_hir_typeck[5fc4f412604a8286]::fn_ctxt::FnCtxt>::emit_coerce_suggestions
  21:     0x7f74731213a5 - <rustc_hir_typeck[5fc4f412604a8286]::fn_ctxt::FnCtxt>::check_decl
  22:     0x7f747311d73f - <rustc_hir_typeck[5fc4f412604a8286]::fn_ctxt::FnCtxt>::check_expr_block
  23:     0x7f747312441e - <rustc_hir_typeck[5fc4f412604a8286]::fn_ctxt::FnCtxt>::check_expr_with_expectation_and_args
  24:     0x7f7472811c46 - rustc_hir_typeck[5fc4f412604a8286]::check::check_fn
  25:     0x7f747281a281 - rustc_hir_typeck[5fc4f412604a8286]::typeck_with_inspect::{closure#0}
  26:     0x7f747281828c - rustc_query_impl[abff21d8349146d]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[abff21d8349146d]::query_impl::typeck::dynamic_query::{closure#2}::{closure#0}, rustc_middle[a78c8f6c75429410]::query::erase::Erased<[u8; 8usize]>>
  27:     0x7f747280c37e - rustc_query_system[9464b8c501ef87f3]::query::plumbing::try_execute_query::<rustc_query_impl[abff21d8349146d]::DynamicConfig<rustc_data_structures[6e992f72641bf6c6]::vec_cache::VecCache<rustc_span[15ded62729b25549]::def_id::LocalDefId, rustc_middle[a78c8f6c75429410]::query::erase::Erased<[u8; 8usize]>, rustc_query_system[9464b8c501ef87f3]::dep_graph::graph::DepNodeIndex>, false, false, false>, rustc_query_impl[abff21d8349146d]::plumbing::QueryCtxt, true>
  28:     0x7f747289748e - rustc_query_impl[abff21d8349146d]::query_impl::typeck::get_query_incr::__rust_end_short_backtrace
  29:     0x7f74728086d5 - <rustc_middle[a78c8f6c75429410]::hir::map::Map>::par_body_owners::<rustc_hir_analysis[f9dd0ea7be29e3be]::check_crate::{closure#3}>::{closure#0}
  30:     0x7f7472807d45 - rustc_hir_analysis[f9dd0ea7be29e3be]::check_crate
  31:     0x7f74728028ab - rustc_interface[84ab11ffe8dacd23]::passes::run_required_analyses
  32:     0x7f7472f18f1e - rustc_interface[84ab11ffe8dacd23]::passes::analysis
  33:     0x7f7472f18eef - rustc_query_impl[abff21d8349146d]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[abff21d8349146d]::query_impl::analysis::dynamic_query::{closure#2}::{closure#0}, rustc_middle[a78c8f6c75429410]::query::erase::Erased<[u8; 0usize]>>
  34:     0x7f747344ef93 - rustc_query_system[9464b8c501ef87f3]::query::plumbing::try_execute_query::<rustc_query_impl[abff21d8349146d]::DynamicConfig<rustc_query_system[9464b8c501ef87f3]::query::caches::SingleCache<rustc_middle[a78c8f6c75429410]::query::erase::Erased<[u8; 0usize]>>, false, false, false>, rustc_query_impl[abff21d8349146d]::plumbing::QueryCtxt, true>
  35:     0x7f747344e915 - rustc_query_impl[abff21d8349146d]::query_impl::analysis::get_query_incr::__rust_end_short_backtrace
  36:     0x7f747339aea9 - rustc_interface[84ab11ffe8dacd23]::passes::create_and_enter_global_ctxt::<core[46aa9df3d3dcdeb1]::option::Option<rustc_interface[84ab11ffe8dacd23]::queries::Linker>, rustc_driver_impl[680c351c5444d7cd]::run_compiler::{closure#0}::{closure#2}>::{closure#2}::{closure#0}
  37:     0x7f74733ab726 - rustc_interface[84ab11ffe8dacd23]::interface::run_compiler::<(), rustc_driver_impl[680c351c5444d7cd]::run_compiler::{closure#0}>::{closure#1}
  38:     0x7f74732e0744 - std[d9e466a2d75004a2]::sys::backtrace::__rust_begin_short_backtrace::<rustc_interface[84ab11ffe8dacd23]::util::run_in_thread_with_globals<rustc_interface[84ab11ffe8dacd23]::util::run_in_thread_pool_with_globals<rustc_interface[84ab11ffe8dacd23]::interface::run_compiler<(), rustc_driver_impl[680c351c5444d7cd]::run_compiler::{closure#0}>::{closure#1}, ()>::{closure#0}, ()>::{closure#0}::{closure#0}, ()>
  39:     0x7f74732e0419 - <<std[d9e466a2d75004a2]::thread::Builder>::spawn_unchecked_<rustc_interface[84ab11ffe8dacd23]::util::run_in_thread_with_globals<rustc_interface[84ab11ffe8dacd23]::util::run_in_thread_pool_with_globals<rustc_interface[84ab11ffe8dacd23]::interface::run_compiler<(), rustc_driver_impl[680c351c5444d7cd]::run_compiler::{closure#0}>::{closure#1}, ()>::{closure#0}, ()>::{closure#0}::{closure#0}, ()>::{closure#1} as core[46aa9df3d3dcdeb1]::ops::function::FnOnce<()>>::call_once::{shim:vtable#0}
  40:     0x7f74732dfbaf - std::sys::pal::unix::thread::Thread::new::thread_start::hcc5ed016d554f327
  41:     0x7f746d475aa4 - <unknown>
  42:     0x7f746d502c3c - <unknown>
  43:                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: rustc 1.86.0 (05f9846f8 2025-03-31) running on x86_64-unknown-linux-gnu

note: compiler flags: --crate-type lib -C embed-bitcode=no -C debuginfo=2 -C incremental=[REDACTED]

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

query stack during panic:
#0 [typeck] type-checking `ice`
#1 [analysis] running analysis passes on this crate
end of query stack
note: no errors encountered even though delayed bugs were created

note: those delayed bugs will now be shown as internal compiler errors

error: internal compiler error: `TypeError` when attempting coercion but no error emitted
 --> src/lib.rs:6:47
  |
6 |     let fails: Box<dyn Fn(&Container<&u8>)> = callback;
  |                                               ^^^^^^^^
  |
note: delayed at compiler/rustc_hir_typeck/src/demand.rs:273:47 - disabled backtrace
 --> src/lib.rs:6:47
  |
6 |     let fails: Box<dyn Fn(&Container<&u8>)> = callback;
  |                                               ^^^^^^^^

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: rustc 1.86.0 (05f9846f8 2025-03-31) running on x86_64-unknown-linux-gnu

note: compiler flags: --crate-type lib -C embed-bitcode=no -C debuginfo=2 -C incremental=[REDACTED]

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

query stack during panic:
end of query stack
error: could not compile `ice` (lib)

Caused by:
  process didn't exit successfully: `/home/user/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/bin/rustc --crate-name ice --edition=2024 src/lib.rs --error-format=json --json=diagnostic-rendered-ansi,artifacts,future-incompat --crate-type lib --emit=dep-info,metadata,link -C embed-bitcode=no -C debuginfo=2 --check-cfg 'cfg(docsrs,test)' --check-cfg 'cfg(feature, values())' -C metadata=cb5a12b6d550384b -C extra-filename=-f11bb989a7d34b0a --out-dir /home/user/projects/rust-ice/ice/target/debug/deps -C incremental=/home/user/projects/rust-ice/ice/target/debug/incremental -L dependency=/home/user/projects/rust-ice/ice/target/debug/deps` (exit status: 101)

With RUST_BACKTRACE=1

   Compiling ice v0.1.0 (/home/user/projects/rust-ice/ice)

thread 'rustc' panicked at /rust/deps/ena-0.14.3/src/snapshot_vec.rs:199:10:
index out of bounds: the len is 1 but the index is 1
stack backtrace:
   0: rust_begin_unwind
   1: core::panicking::panic_fmt
   2: core::panicking::panic_bounds_check
   3: <ena::unify::UnificationTable<ena::unify::backing_vec::InPlace<rustc_infer::infer::unify_key::RegionVidKey, &mut alloc::vec::Vec<ena::unify::VarValue<rustc_infer::infer::unify_key::RegionVidKey>>, &mut rustc_infer::infer::snapshot::undo_log::InferCtxtUndoLogs>>>::uninlined_get_root_key.cold
   4: rustc_type_ir::relate::structurally_relate_tys::<rustc_middle::ty::context::TyCtxt, rustc_infer::infer::relate::type_relating::TypeRelating>::{closure#0}
   5: <rustc_infer::infer::relate::type_relating::TypeRelating as rustc_type_ir::relate::TypeRelation<rustc_middle::ty::context::TyCtxt>>::tys
   6: rustc_type_ir::relate::structurally_relate_tys::<rustc_middle::ty::context::TyCtxt, rustc_infer::infer::relate::type_relating::TypeRelating>::{closure#0}
   7: <rustc_infer::infer::relate::type_relating::TypeRelating as rustc_type_ir::relate::TypeRelation<rustc_middle::ty::context::TyCtxt>>::tys
   8: <rustc_infer::infer::at::At>::eq_trace::<rustc_middle::ty::Ty>
   9: <rustc_infer::infer::InferCtxt>::probe::<bool, <rustc_infer::infer::InferCtxt as rustc_trait_selection::infer::InferCtxtExt>::can_eq<rustc_middle::ty::Ty>::{closure#0}>
  10: <rustc_hir_typeck::fn_ctxt::FnCtxt>::emit_coerce_suggestions
  11: <rustc_hir_typeck::fn_ctxt::FnCtxt>::check_decl
  12: <rustc_hir_typeck::fn_ctxt::FnCtxt>::check_expr_block
  13: <rustc_hir_typeck::fn_ctxt::FnCtxt>::check_expr_with_expectation_and_args
  14: rustc_hir_typeck::check::check_fn
  15: rustc_hir_typeck::typeck_with_inspect::{closure#0}
      [... omitted 1 frame ...]
  16: <rustc_middle::hir::map::Map>::par_body_owners::<rustc_hir_analysis::check_crate::{closure#3}>::{closure#0}
  17: rustc_hir_analysis::check_crate
  18: rustc_interface::passes::run_required_analyses
  19: rustc_interface::passes::analysis
      [... omitted 1 frame ...]
  20: rustc_interface::passes::create_and_enter_global_ctxt::<core::option::Option<rustc_interface::queries::Linker>, rustc_driver_impl::run_compiler::{closure#0}::{closure#2}>::{closure#2}::{closure#0}
  21: 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.

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: rustc 1.86.0 (05f9846f8 2025-03-31) running on x86_64-unknown-linux-gnu

note: compiler flags: --crate-type lib -C embed-bitcode=no -C debuginfo=2 -C incremental=[REDACTED]

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

query stack during panic:
#0 [typeck] type-checking `ice`
#1 [analysis] running analysis passes on this crate
end of query stack
note: no errors encountered even though delayed bugs were created

note: those delayed bugs will now be shown as internal compiler errors

error: internal compiler error: `TypeError` when attempting coercion but no error emitted
 --> src/lib.rs:6:47
  |
6 |     let fails: Box<dyn Fn(&Container<&u8>)> = callback;
  |                                               ^^^^^^^^
  |
note: delayed at compiler/rustc_hir_typeck/src/demand.rs:273:47
         0: <rustc_errors::DiagCtxtInner>::emit_diagnostic
         1: <rustc_errors::DiagCtxtHandle>::emit_diagnostic
         2: <rustc_span::ErrorGuaranteed as rustc_errors::diagnostic::EmissionGuarantee>::emit_producing_guarantee
         3: <rustc_errors::DiagCtxtHandle>::span_delayed_bug::<rustc_span::span_encoding::Span, &str>
         4: <rustc_hir_typeck::fn_ctxt::FnCtxt>::check_decl
         5: <rustc_hir_typeck::fn_ctxt::FnCtxt>::check_expr_block
         6: <rustc_hir_typeck::fn_ctxt::FnCtxt>::check_expr_with_expectation_and_args
         7: rustc_hir_typeck::check::check_fn
         8: rustc_hir_typeck::typeck_with_inspect::{closure#0}
         9: rustc_query_impl::plumbing::__rust_begin_short_backtrace::<rustc_query_impl::query_impl::typeck::dynamic_query::{closure#2}::{closure#0}, rustc_middle::query::erase::Erased<[u8; 8]>>
        10: rustc_query_system::query::plumbing::try_execute_query::<rustc_query_impl::DynamicConfig<rustc_data_structures::vec_cache::VecCache<rustc_span::def_id::LocalDefId, rustc_middle::query::erase::Erased<[u8; 8]>, rustc_query_system::dep_graph::graph::DepNodeIndex>, false, false, false>, rustc_query_impl::plumbing::QueryCtxt, true>
        11: rustc_query_impl::query_impl::typeck::get_query_incr::__rust_end_short_backtrace
        12: <rustc_middle::hir::map::Map>::par_body_owners::<rustc_hir_analysis::check_crate::{closure#3}>::{closure#0}
        13: rustc_hir_analysis::check_crate
        14: rustc_interface::passes::run_required_analyses
        15: rustc_interface::passes::analysis
        16: rustc_query_impl::plumbing::__rust_begin_short_backtrace::<rustc_query_impl::query_impl::analysis::dynamic_query::{closure#2}::{closure#0}, rustc_middle::query::erase::Erased<[u8; 0]>>
        17: rustc_query_system::query::plumbing::try_execute_query::<rustc_query_impl::DynamicConfig<rustc_query_system::query::caches::SingleCache<rustc_middle::query::erase::Erased<[u8; 0]>>, false, false, false>, rustc_query_impl::plumbing::QueryCtxt, true>
        18: rustc_query_impl::query_impl::analysis::get_query_incr::__rust_end_short_backtrace
        19: rustc_interface::passes::create_and_enter_global_ctxt::<core::option::Option<rustc_interface::queries::Linker>, rustc_driver_impl::run_compiler::{closure#0}::{closure#2}>::{closure#2}::{closure#0}
        20: rustc_interface::interface::run_compiler::<(), rustc_driver_impl::run_compiler::{closure#0}>::{closure#1}
        21: std::sys::backtrace::__rust_begin_short_backtrace::<rustc_interface::util::run_in_thread_with_globals<rustc_interface::util::run_in_thread_pool_with_globals<rustc_interface::interface::run_compiler<(), rustc_driver_impl::run_compiler::{closure#0}>::{closure#1}, ()>::{closure#0}, ()>::{closure#0}::{closure#0}, ()>
        22: <<std::thread::Builder>::spawn_unchecked_<rustc_interface::util::run_in_thread_with_globals<rustc_interface::util::run_in_thread_pool_with_globals<rustc_interface::interface::run_compiler<(), rustc_driver_impl::run_compiler::{closure#0}>::{closure#1}, ()>::{closure#0}, ()>::{closure#0}::{closure#0}, ()>::{closure#1} as core::ops::function::FnOnce<()>>::call_once::{shim:vtable#0}
        23: std::sys::pal::unix::thread::Thread::new::thread_start
        24: <unknown>
        25: <unknown>
 --> src/lib.rs:6:47
  |
6 |     let fails: Box<dyn Fn(&Container<&u8>)> = callback;
  |                                               ^^^^^^^^

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: rustc 1.86.0 (05f9846f8 2025-03-31) running on x86_64-unknown-linux-gnu

note: compiler flags: --crate-type lib -C embed-bitcode=no -C debuginfo=2 -C incremental=[REDACTED]

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

query stack during panic:
end of query stack
error: could not compile `ice` (lib)

Caused by:
  process didn't exit successfully: `/home/user/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/bin/rustc --crate-name ice --edition=2024 src/lib.rs --error-format=json --json=diagnostic-rendered-ansi,artifacts,future-incompat --crate-type lib --emit=dep-info,metadata,link -C embed-bitcode=no -C debuginfo=2 --check-cfg 'cfg(docsrs,test)' --check-cfg 'cfg(feature, values())' -C metadata=cb5a12b6d550384b -C extra-filename=-f11bb989a7d34b0a --out-dir /home/user/projects/rust-ice/ice/target/debug/deps -C incremental=/home/user/projects/rust-ice/ice/target/debug/incremental -L dependency=/home/user/projects/rust-ice/ice/target/debug/deps` (exit status: 101)

@jagunter jagunter added 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. C-bug Category: This is a bug. labels May 8, 2025
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label May 8, 2025
@moxian
Copy link
Contributor

moxian commented May 8, 2025

Bisects to #135465 rollup, and probably #134977 therein, given emit_coerce_suggestions in the stacktrace

@rustbot label: +S-has-mcve +S-has-bisection

p.s. I cannot reproduce the ICE with just Box

@rustbot rustbot added S-has-bisection Status: A bisection has been found for this issue S-has-mcve Status: A Minimal Complete and Verifiable Example has been found for this issue T-types Relevant to the types team, which will review and decide on the PR/issue. and removed T-types Relevant to the types team, which will review and decide on the PR/issue. labels May 8, 2025
@jagunter
Copy link
Contributor Author

jagunter commented May 18, 2025

Confirmed #134977 is the culprit. Specifically the following in annotate_mut_binding_to_immutable_binding:

// The difference between the expected and found values is one level of borrowing.
&& self.can_eq(self.param_env, *inner, found)

Looking at the rustc logs this is the original error which is being expanded upon:

     1ms DEBUG rustc_trait_selection::error_reporting::infer exp_found Some(ExpectedFound { expected: std::boxed::Box<dyn [Binder { value: Trait(MyFn<&'a Container<&'a u8>>), bound_vars: [Region(BrNamed(DefId(0:10 ~ rust_ice[c3ad]::Desugared::callback::'a), 'a))] }] + 'static, std::alloc::Global>, found: std::boxed::Box<dyn [Binder { value: Trait(MyFn<Container<&'a u8>>), bound_vars: [Region(BrNamed(DefId(0:12 ~ rust_ice[c3ad]::ice::'a), 'a))] }] + 'static, std::alloc::Global> }) terr Sorts(ExpectedFound { expected: &!1_0.Named(DefId(0:10 ~ rust_ice[c3ad]::Desugared::callback::'a), "'a") Container<&!1_0.Named(DefId(0:10 ~ rust_ice[c3ad]::Desugared::callback::'a), "'a") u8>, found: Container<&'?0 u8> }) cause.code Misc

so here found: Container<&'?0 u8> and *inner is Container<&!1_0.Named(DefId(0:10 ~ rust_ice[c3ad]::Desugared::callback::'a), "'a") u8>. This eventually triggers

make_eqregion: unifying !1_0.Named(DefId(0:10 ~ rust_ice[c3ad]::Desugared::callback::'a), "'a") with '?0

and crashes because '?0 isn't present in the region constraints unification table at this time. It's not present because it's been rolled back in the region constraints:

                   0ms DEBUG rustc_infer::infer::region_constraints created new region variable '?0 in U1 with origin BoundRegion(src/main.rs:12:17: 12:25 (#0), BrNamed(DefId(0:12 ~ rust_ice[c3ad]::ice::'a), 'a), HigherRankedType)
                   0ms DEBUG rustc_middle::ty::region type_flags('?0) = TypeFlags(HAS_RE_INFER | HAS_FREE_LOCAL_REGIONS | HAS_FREE_REGIONS)
                   0ms DEBUG rustc_infer::infer::relate::type_relating new ambient variance, self.ambient_variance=o
                   rustc_infer::infer::relate::type_relating::tys a=&!1_0.Named(DefId(0:10 ~ rust_ice[c3ad]::Desugared::callback::'a), "'a") Container<&!1_0.Named(DefId(0:10 ~ rust_ice[c3ad]::Desugared::callback::'a), "'a") u8>, b=Container<&'?0 u8>
                     0ms DEBUG rustc_type_ir::relate::combine super_combine_tys::<rustc_infer::infer::relate::type_relating::TypeRelating>(&!1_0.Named(DefId(0:10 ~ rust_ice[c3ad]::Desugared::callback::'a), "'a") Container<&!1_0.Named(DefId(0:10 ~ rust_ice[c3ad]::Desugared::callback::'a), "'a") u8>, Container<&'?0 u8>)
                     rustc_type_ir::relate::structurally_relate_tys a=&!1_0.Named(DefId(0:10 ~ rust_ice[c3ad]::Desugared::callback::'a), "'a") Container<&!1_0.Named(DefId(0:10 ~ rust_ice[c3ad]::Desugared::callback::'a), "'a") u8>, b=Container<&'?0 u8>
                       0ms TRACE rustc_type_ir::relate return=Err(Sorts(ExpectedFound { expected: &!1_0.Named(DefId(0:10 ~ rust_ice[c3ad]::Desugared::callback::'a), "'a") Container<&!1_0.Named(DefId(0:10 ~ rust_ice[c3ad]::Desugared::callback::'a), "'a") u8>, found: Container<&'?0 u8> }))
                 0ms TRACE rustc_type_ir::relate return=Err(Sorts(ExpectedFound { expected: &!1_0.Named(DefId(0:10 ~ rust_ice[c3ad]::Desugared::callback::'a), "'a") Container<&!1_0.Named(DefId(0:10 ~ rust_ice[c3ad]::Desugared::callback::'a), "'a") u8>, found: Container<&'?0 u8> }))
             0ms TRACE rustc_type_ir::relate return=Err(Sorts(ExpectedFound { expected: &!1_0.Named(DefId(0:10 ~ rust_ice[c3ad]::Desugared::callback::'a), "'a") Container<&!1_0.Named(DefId(0:10 ~ rust_ice[c3ad]::Desugared::callback::'a), "'a") u8>, found: Container<&'?0 u8> }))
         0ms DEBUG rustc_infer::infer::snapshot commit_if_ok() -- r.is_ok() = false
         rustc_infer::infer::snapshot::rollback_to 
           0ms DEBUG rustc_infer::infer::snapshot::undo_log rollback_to(0)
           0ms DEBUG rustc_infer::infer::region_constraints RegionConstraintCollector: rollback_to(RegionSnapshot)
    11ms DEBUG rustc_infer::infer::snapshot commit_if_ok() -- r.is_ok() = false
     rustc_infer::infer::snapshot::rollback_to 
       0ms DEBUG rustc_infer::infer::snapshot::undo_log rollback_to(0)
       0ms DEBUG rustc_infer::infer::region_constraints RegionConstraintCollector: rollback_to(RegionSnapshot)
  17ms DEBUG rustc_errors::diagnostic Created new diagnostic
  17ms DEBUG rustc_infer::infer set_tainted_by_errors(ErrorGuaranteed)
  17ms DEBUG rustc_trait_selection::error_reporting::infer report_and_explain_type_error(trace=TypeTrace { cause: ObligationCause { span: src/main.rs:12:17: 12:25 (#0), body_id: DefId(0:11 ~ rust_ice[c3ad]::ice), code: Misc }, values: Terms(ExpectedFound { expected: Term::Ty(std::boxed::Box<dyn [Binder { value: Trait(MyFn<&'a Container<&'a u8>>), bound_vars: [Region(BrNamed(DefId(0:10 ~ rust_ice[c3ad]::Desugared::callback::'a), 'a))] }] + 'static, std::alloc::Global>), found: Term::Ty(std::boxed::Box<dyn [Binder { value: Trait(MyFn<Container<&'a u8>>), bound_vars: [Region(BrNamed(DefId(0:12 ~ rust_ice[c3ad]::ice::'a), 'a))] }] + 'static, std::alloc::Global>) }) }, terr=Sorts(ExpectedFound { expected: &!1_0.Named(DefId(0:10 ~ rust_ice[c3ad]::Desugared::callback::'a), "'a") Container<&!1_0.Named(DefId(0:10 ~ rust_ice[c3ad]::Desugared::callback::'a), "'a") u8>, found: Container<&'?0 u8> }))
ICE Backtrace
thread 'rustc' panicked at /home/user/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/ena-0.14.3/src/snapshot_vec.rs:199:10:
index out of bounds: the len is 0 but the index is 0
stack backtrace:
   0: begin_panic_handler
             at /home/user/external/rust/library/std/src/panicking.rs:697:5
   1: panic_fmt
             at /home/user/external/rust/library/core/src/panicking.rs:75:14
   2: panic_bounds_check
             at /home/user/external/rust/library/core/src/panicking.rs:280:5
   3: get<&mut alloc::vec::Vec<ena::unify::VarValue<rustc_infer::infer::unify_key::RegionVidKey>, alloc::alloc::Global>, ena::unify::backing_vec::Delegate<rustc_infer::infer::unify_key::RegionVidKey>, &mut rustc_infer::infer::snapshot::undo_log::InferCtxtUndoLogs>
             at /home/user/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/ena-0.14.3/src/snapshot_vec.rs:199:10
   4: value<ena::unify::backing_vec::InPlace<rustc_infer::infer::unify_key::RegionVidKey, &mut alloc::vec::Vec<ena::unify::VarValue<rustc_infer::infer::unify_key::RegionVidKey>, alloc::alloc::Global>, &mut rustc_infer::infer::snapshot::undo_log::InferCtxtUndoLogs>>
             at /home/user/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/ena-0.14.3/src/unify/mod.rs:302:21
   5: inlined_get_root_key<ena::unify::backing_vec::InPlace<rustc_infer::infer::unify_key::RegionVidKey, &mut alloc::vec::Vec<ena::unify::VarValue<rustc_infer::infer::unify_key::RegionVidKey>, alloc::alloc::Global>, &mut rustc_infer::infer::snapshot::undo_log::InferCtxtUndoLogs>>
             at /home/user/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/ena-0.14.3/src/unify/mod.rs:344:17
   6: uninlined_get_root_key<ena::unify::backing_vec::InPlace<rustc_infer::infer::unify_key::RegionVidKey, &mut alloc::vec::Vec<ena::unify::VarValue<rustc_infer::infer::unify_key::RegionVidKey>, alloc::alloc::Global>, &mut rustc_infer::infer::snapshot::undo_log::InferCtxtUndoLogs>>
             at /home/user/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/ena-0.14.3/src/unify/mod.rs:363:9
   7: unify_var_value<ena::unify::backing_vec::InPlace<rustc_infer::infer::unify_key::RegionVidKey, &mut alloc::vec::Vec<ena::unify::VarValue<rustc_infer::infer::unify_key::RegionVidKey>, alloc::alloc::Global>, &mut rustc_infer::infer::snapshot::undo_log::InferCtxtUndoLogs>, rustc_infer::infer::unify_key::RegionVidKey, rustc_infer::infer::unify_key::RegionVariableValue, rustc_type_ir::region_kind::RegionVid>
             at /home/user/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/ena-0.14.3/src/unify/mod.rs:548:22
   8: make_eqregion
             at /home/user/external/rust/compiler/rustc_infer/src/infer/region_constraints/mod.rs:443:24
   9: regions
             at /home/user/external/rust/compiler/rustc_infer/src/infer/relate/type_relating.rs:239:17
  10: <rustc_middle::ty::region::Region as rustc_type_ir::relate::Relate<rustc_middle::ty::context::TyCtxt>>::relate::<rustc_infer::infer::relate::type_relating::TypeRelating>
             at /home/user/external/rust/compiler/rustc_middle/src/ty/relate.rs:127:9
  11: <rustc_infer::infer::relate::type_relating::TypeRelating as rustc_type_ir::relate::TypeRelation<rustc_middle::ty::context::TyCtxt>>::relate::<rustc_middle::ty::region::Region>
             at /home/user/external/rust/compiler/rustc_type_ir/src/relate.rs:71:9
  12: {closure#0}<rustc_middle::ty::context::TyCtxt, rustc_infer::infer::relate::type_relating::TypeRelating>
             at /home/user/external/rust/compiler/rustc_type_ir/src/relate.rs:482:21
  13: structurally_relate_tys<rustc_middle::ty::context::TyCtxt, rustc_infer::infer::relate::type_relating::TypeRelating>
             at /home/user/external/rust/compiler/rustc_type_ir/src/relate.rs:363:1
  14: super_combine_tys<rustc_infer::infer::InferCtxt, rustc_middle::ty::context::TyCtxt, rustc_infer::infer::relate::type_relating::TypeRelating>
  15: tys
             at /home/user/external/rust/compiler/rustc_infer/src/infer/relate/type_relating.rs:204:17
  16: relate<rustc_infer::infer::relate::type_relating::TypeRelating>
             at /home/user/external/rust/compiler/rustc_middle/src/ty/relate.rs:41:9
  17: relate<rustc_infer::infer::relate::type_relating::TypeRelating, rustc_middle::ty::context::TyCtxt, rustc_middle::ty::Ty>
             at /home/user/external/rust/compiler/rustc_type_ir/src/relate.rs:71:9
  18: relate<rustc_infer::infer::relate::type_relating::TypeRelating>
             at /home/user/external/rust/compiler/rustc_middle/src/ty/relate.rs:177:20
  19: relate<rustc_infer::infer::relate::type_relating::TypeRelating, rustc_middle::ty::context::TyCtxt, rustc_middle::ty::generic_args::GenericArg>
             at /home/user/external/rust/compiler/rustc_type_ir/src/relate.rs:71:9
  20: relate_with_variance<rustc_middle::ty::generic_args::GenericArg>
             at /home/user/external/rust/compiler/rustc_infer/src/infer/relate/type_relating.rs:111:76
  21: {closure#0}<rustc_middle::ty::context::TyCtxt, rustc_infer::infer::relate::type_relating::TypeRelating>
             at /home/user/external/rust/compiler/rustc_type_ir/src/relate.rs:133:9
  22: call_once<((rustc_middle::ty::generic_args::GenericArg, rustc_middle::ty::generic_args::GenericArg)), rustc_type_ir::relate::relate_args_invariantly::{closure_env#0}<rustc_middle::ty::context::TyCtxt, rustc_infer::infer::relate::type_relating::TypeRelating>>
             at /home/user/external/rust/library/core/src/ops/function.rs:305:13
  23: map<(rustc_middle::ty::generic_args::GenericArg, rustc_middle::ty::generic_args::GenericArg), core::result::Result<rustc_middle::ty::generic_args::GenericArg, rustc_type_ir::error::TypeError<rustc_middle::ty::context::TyCtxt>>, &mut rustc_type_ir::relate::relate_args_invariantly::{closure_env#0}<rustc_middle::ty::context::TyCtxt, rustc_infer::infer::relate::type_relating::TypeRelating>>
             at /home/user/external/rust/library/core/src/option.rs:1144:29
  24: next<core::result::Result<rustc_middle::ty::generic_args::GenericArg, rustc_type_ir::error::TypeError<rustc_middle::ty::context::TyCtxt>>, core::iter::adapters::zip::Zip<core::iter::adapters::copied::Copied<core::slice::iter::Iter<rustc_middle::ty::generic_args::GenericArg>>, core::iter::adapters::copied::Copied<core::slice::iter::Iter<rustc_middle::ty::generic_args::GenericArg>>>, rustc_type_ir::relate::relate_args_invariantly::{closure_env#0}<rustc_middle::ty::context::TyCtxt, rustc_infer::infer::relate::type_relating::TypeRelating>>
             at /home/user/external/rust/library/core/src/iter/adapters/map.rs:107:26
  25: collect_and_apply<rustc_middle::ty::generic_args::GenericArg, &rustc_middle::ty::list::RawList<(), rustc_middle::ty::generic_args::GenericArg>, rustc_type_ir::error::TypeError<rustc_middle::ty::context::TyCtxt>, core::iter::adapters::map::Map<core::iter::adapters::zip::Zip<core::iter::adapters::copied::Copied<core::slice::iter::Iter<rustc_middle::ty::generic_args::GenericArg>>, core::iter::adapters::copied::Copied<core::slice::iter::Iter<rustc_middle::ty::generic_args::GenericArg>>>, rustc_type_ir::relate::relate_args_invariantly::{closure_env#0}<rustc_middle::ty::context::TyCtxt, rustc_infer::infer::relate::type_relating::TypeRelating>>, rustc_middle::ty::context::{impl#43}::mk_args_from_iter::{closure_env#0}<core::iter::adapters::map::Map<core::iter::adapters::zip::Zip<core::iter::adapters::copied::Copied<core::slice::iter::Iter<rustc_middle::ty::generic_args::GenericArg>>, core::iter::adapters::copied::Copied<core::slice::iter::Iter<rustc_middle::ty::generic_args::GenericArg>>>, rustc_type_ir::relate::relate_args_invariantly::{closure_env#0}<rustc_middle::ty::context::TyCtxt, rustc_infer::infer::relate::type_relating::TypeRelating>>, core::result::Result<rustc_middle::ty::generic_args::GenericArg, rustc_type_ir::error::TypeError<rustc_middle::ty::context::TyCtxt>>>>
             at /home/user/external/rust/compiler/rustc_type_ir/src/interner.rs:430:26
  26: mk_args_from_iter<core::iter::adapters::map::Map<core::iter::adapters::zip::Zip<core::iter::adapters::copied::Copied<core::slice::iter::Iter<rustc_middle::ty::generic_args::GenericArg>>, core::iter::adapters::copied::Copied<core::slice::iter::Iter<rustc_middle::ty::generic_args::GenericArg>>>, rustc_type_ir::relate::relate_args_invariantly::{closure_env#0}<rustc_middle::ty::context::TyCtxt, rustc_infer::infer::relate::type_relating::TypeRelating>>, core::result::Result<rustc_middle::ty::generic_args::GenericArg, rustc_type_ir::error::TypeError<rustc_middle::ty::context::TyCtxt>>>
             at /home/user/external/rust/compiler/rustc_middle/src/ty/context.rs:3052:9
  27: mk_args_from_iter<core::iter::adapters::map::Map<core::iter::adapters::zip::Zip<core::iter::adapters::copied::Copied<core::slice::iter::Iter<rustc_middle::ty::generic_args::GenericArg>>, core::iter::adapters::copied::Copied<core::slice::iter::Iter<rustc_middle::ty::generic_args::GenericArg>>>, rustc_type_ir::relate::relate_args_invariantly::{closure_env#0}<rustc_middle::ty::context::TyCtxt, rustc_infer::infer::relate::type_relating::TypeRelating>>, core::result::Result<rustc_middle::ty::generic_args::GenericArg, rustc_type_ir::error::TypeError<rustc_middle::ty::context::TyCtxt>>>
             at /home/user/external/rust/compiler/rustc_middle/src/ty/context.rs:287:9
  28: relate_args_invariantly<rustc_middle::ty::context::TyCtxt, rustc_infer::infer::relate::type_relating::TypeRelating>
             at /home/user/external/rust/compiler/rustc_type_ir/src/relate.rs:132:5
  29: relate_item_args
             at /home/user/external/rust/compiler/rustc_infer/src/infer/relate/type_relating.rs:92:13
  30: {closure#0}<rustc_middle::ty::context::TyCtxt, rustc_infer::infer::relate::type_relating::TypeRelating>
             at /home/user/external/rust/compiler/rustc_type_ir/src/relate.rs:403:24
  31: structurally_relate_tys<rustc_middle::ty::context::TyCtxt, rustc_infer::infer::relate::type_relating::TypeRelating>
             at /home/user/external/rust/compiler/rustc_type_ir/src/relate.rs:363:1
  32: super_combine_tys<rustc_infer::infer::InferCtxt, rustc_middle::ty::context::TyCtxt, rustc_infer::infer::relate::type_relating::TypeRelating>
  33: tys
             at /home/user/external/rust/compiler/rustc_infer/src/infer/relate/type_relating.rs:204:17
  34: relate<rustc_infer::infer::relate::type_relating::TypeRelating>
             at /home/user/external/rust/compiler/rustc_middle/src/ty/relate.rs:41:9
  35: relate<rustc_infer::infer::relate::type_relating::TypeRelating, rustc_middle::ty::context::TyCtxt, rustc_middle::ty::Ty>
             at /home/user/external/rust/compiler/rustc_type_ir/src/relate.rs:71:9
  36: eq_trace<rustc_middle::ty::Ty>
             at /home/user/external/rust/compiler/rustc_infer/src/infer/at.rs:236:13
  37: <rustc_infer::infer::at::At>::eq::<rustc_middle::ty::Ty>
             at /home/user/external/rust/compiler/rustc_infer/src/infer/at.rs:199:9
  38: eq<rustc_infer::traits::engine::ScrubbedTraitError, rustc_middle::ty::Ty>
             at /home/user/external/rust/compiler/rustc_trait_selection/src/traits/engine.rs:129:9
  39: {closure#0}<rustc_middle::ty::Ty>
             at /home/user/external/rust/compiler/rustc_trait_selection/src/infer.rs:25:26
  40: probe<bool, rustc_trait_selection::infer::{impl#0}::can_eq::{closure_env#0}<rustc_middle::ty::Ty>>
             at /home/user/external/rust/compiler/rustc_infer/src/infer/snapshot/mod.rs:106:17
  41: can_eq<rustc_middle::ty::Ty>
             at /home/user/external/rust/compiler/rustc_trait_selection/src/infer.rs:23:9
  42: annotate_mut_binding_to_immutable_binding
             at /home/user/external/rust/compiler/rustc_hir_typeck/src/demand.rs:835:16
  43: emit_coerce_suggestions
             at /home/user/external/rust/compiler/rustc_hir_typeck/src/demand.rs:87:12
  44: demand_coerce_diag
             at /home/user/external/rust/compiler/rustc_hir_typeck/src/demand.rs:282:9
  45: check_expr_struct_fields
             at /home/user/external/rust/compiler/rustc_hir_typeck/src/expr.rs:2116:24
  46: check_expr_struct
             at /home/user/external/rust/compiler/rustc_hir_typeck/src/expr.rs:2000:9
  47: check_expr_kind
             at /home/user/external/rust/compiler/rustc_hir_typeck/src/expr.rs:577:17
  48: {closure#0}
             at /home/user/external/rust/compiler/rustc_hir_typeck/src/expr.rs:257:18
  49: maybe_grow<rustc_middle::ty::Ty, rustc_hir_typeck::expr::{impl#0}::check_expr_with_expectation_and_args::{closure_env#0}>
             at /home/user/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/stacker-0.1.21/src/lib.rs:57:9
  50: ensure_sufficient_stack<rustc_middle::ty::Ty, rustc_hir_typeck::expr::{impl#0}::check_expr_with_expectation_and_args::{closure_env#0}>
             at /home/user/external/rust/compiler/rustc_data_structures/src/stack.rs:21:5
  51: check_expr_with_expectation_and_args
             at /home/user/external/rust/compiler/rustc_hir_typeck/src/expr.rs:252:18
  52: check_expr_with_expectation
             at /home/user/external/rust/compiler/rustc_hir_typeck/src/expr.rs:197:9
  53: {closure#0}
             at /home/user/external/rust/compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs:1865:44
  54: map<&rustc_hir::hir::Expr, (&rustc_hir::hir::Expr, rustc_middle::ty::Ty), rustc_hir_typeck::fn_ctxt::checks::{impl#0}::check_expr_block::{closure#0}::{closure_env#0}>
             at /home/user/external/rust/library/core/src/option.rs:1144:29
  55: {closure#0}
             at /home/user/external/rust/compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs:1865:17
  56: with_breakable_ctxt<rustc_hir_typeck::fn_ctxt::checks::{impl#0}::check_expr_block::{closure_env#0}, ()>
             at /home/user/external/rust/compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs:1563:22
  57: check_expr_block
             at /home/user/external/rust/compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs:1857:26
  58: check_expr_kind
             at /home/user/external/rust/compiler/rustc_hir_typeck/src/expr.rs:553:41
  59: {closure#0}
             at /home/user/external/rust/compiler/rustc_hir_typeck/src/expr.rs:257:18
  60: maybe_grow<rustc_middle::ty::Ty, rustc_hir_typeck::expr::{impl#0}::check_expr_with_expectation_and_args::{closure_env#0}>
             at /home/user/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/stacker-0.1.21/src/lib.rs:57:9
  61: ensure_sufficient_stack<rustc_middle::ty::Ty, rustc_hir_typeck::expr::{impl#0}::check_expr_with_expectation_and_args::{closure_env#0}>
             at /home/user/external/rust/compiler/rustc_data_structures/src/stack.rs:21:5
  62: check_expr_with_expectation_and_args
             at /home/user/external/rust/compiler/rustc_hir_typeck/src/expr.rs:252:18
  63: check_expr_with_expectation
             at /home/user/external/rust/compiler/rustc_hir_typeck/src/expr.rs:197:9
  64: check_expr_with_hint
             at /home/user/external/rust/compiler/rustc_hir_typeck/src/expr.rs:152:9
  65: check_return_or_body_tail
             at /home/user/external/rust/compiler/rustc_hir_typeck/src/expr.rs:1082:30
  66: check_fn
             at /home/user/external/rust/compiler/rustc_hir_typeck/src/check.rs:130:5
  67: {closure#0}
             at /home/user/external/rust/compiler/rustc_hir_typeck/src/lib.rs:172:9
  68: typeck_with_inspect
             at /home/user/external/rust/compiler/rustc_hir_typeck/src/lib.rs:104:1
  69: {closure#0}
             at /home/user/external/rust/compiler/rustc_query_impl/src/plumbing.rs:294:9
      [... omitted 33 frames ...]
  70: query_ensure<rustc_data_structures::vec_cache::VecCache<rustc_span::def_id::LocalDefId, rustc_middle::query::erase::Erased<[u8; 8]>, rustc_query_system::dep_graph::graph::DepNodeIndex>>
             at /home/user/external/rust/compiler/rustc_middle/src/query/plumbing.rs:198:9
  71: typeck<rustc_span::def_id::LocalDefId>
             at /home/user/external/rust/compiler/rustc_middle/src/query/plumbing.rs:233:9
  72: {closure#2}
             at /home/user/external/rust/compiler/rustc_hir_analysis/src/lib.rs:227:13
  73: {closure#0}<rustc_hir_analysis::check_crate::{closure_env#2}>
             at /home/user/external/rust/compiler/rustc_middle/src/hir/map.rs:340:79
  74: {closure#0}<&rustc_span::def_id::LocalDefId, &[rustc_span::def_id::LocalDefId], rustc_middle::hir::map::{impl#3}::par_hir_body_owners::{closure_env#0}<rustc_hir_analysis::check_crate::{closure_env#2}>>
             at /home/user/external/rust/compiler/rustc_data_structures/src/sync/parallel.rs:183:30
  75: call_once<(), rustc_data_structures::sync::parallel::par_for_each_in::{closure#0}::{closure#1}::{closure_env#0}<&rustc_span::def_id::LocalDefId, &[rustc_span::def_id::LocalDefId], rustc_middle::hir::map::{impl#3}::par_hir_body_owners::{closure_env#0}<rustc_hir_analysis::check_crate::{closure_env#2}>>>
             at /home/user/external/rust/library/core/src/panic/unwind_safe.rs:272:9
  76: do_call<core::panic::unwind_safe::AssertUnwindSafe<rustc_data_structures::sync::parallel::par_for_each_in::{closure#0}::{closure#1}::{closure_env#0}<&rustc_span::def_id::LocalDefId, &[rustc_span::def_id::LocalDefId], rustc_middle::hir::map::{impl#3}::par_hir_body_owners::{closure_env#0}<rustc_hir_analysis::check_crate::{closure_env#2}>>>, ()>
             at /home/user/external/rust/library/std/src/panicking.rs:589:40
  77: try<(), core::panic::unwind_safe::AssertUnwindSafe<rustc_data_structures::sync::parallel::par_for_each_in::{closure#0}::{closure#1}::{closure_env#0}<&rustc_span::def_id::LocalDefId, &[rustc_span::def_id::LocalDefId], rustc_middle::hir::map::{impl#3}::par_hir_body_owners::{closure_env#0}<rustc_hir_analysis::check_crate::{closure_env#2}>>>>
             at /home/user/external/rust/library/std/src/panicking.rs:552:19
  78: catch_unwind<core::panic::unwind_safe::AssertUnwindSafe<rustc_data_structures::sync::parallel::par_for_each_in::{closure#0}::{closure#1}::{closure_env#0}<&rustc_span::def_id::LocalDefId, &[rustc_span::def_id::LocalDefId], rustc_middle::hir::map::{impl#3}::par_hir_body_owners::{closure_env#0}<rustc_hir_analysis::check_crate::{closure_env#2}>>>, ()>
             at /home/user/external/rust/library/std/src/panic.rs:359:14
  79: run<(), rustc_data_structures::sync::parallel::par_for_each_in::{closure#0}::{closure#1}::{closure_env#0}<&rustc_span::def_id::LocalDefId, &[rustc_span::def_id::LocalDefId], rustc_middle::hir::map::{impl#3}::par_hir_body_owners::{closure_env#0}<rustc_hir_analysis::check_crate::{closure_env#2}>>>
             at /home/user/external/rust/compiler/rustc_data_structures/src/sync/parallel.rs:25:9
  80: {closure#1}<&rustc_span::def_id::LocalDefId, &[rustc_span::def_id::LocalDefId], rustc_middle::hir::map::{impl#3}::par_hir_body_owners::{closure_env#0}<rustc_hir_analysis::check_crate::{closure_env#2}>>
             at /home/user/external/rust/compiler/rustc_data_structures/src/sync/parallel.rs:183:17
  81: for_each<rustc_span::def_id::LocalDefId, rustc_data_structures::sync::parallel::par_for_each_in::{closure#0}::{closure_env#1}<&rustc_span::def_id::LocalDefId, &[rustc_span::def_id::LocalDefId], rustc_middle::hir::map::{impl#3}::par_hir_body_owners::{closure_env#0}<rustc_hir_analysis::check_crate::{closure_env#2}>>>
             at /home/user/external/rust/library/core/src/slice/iter/macros.rs:277:21
  82: {closure#0}<&rustc_span::def_id::LocalDefId, &[rustc_span::def_id::LocalDefId], rustc_middle::hir::map::{impl#3}::par_hir_body_owners::{closure_env#0}<rustc_hir_analysis::check_crate::{closure_env#2}>>
             at /home/user/external/rust/compiler/rustc_data_structures/src/sync/parallel.rs:182:13
  83: parallel_guard<(), rustc_data_structures::sync::parallel::par_for_each_in::{closure_env#0}<&rustc_span::def_id::LocalDefId, &[rustc_span::def_id::LocalDefId], rustc_middle::hir::map::{impl#3}::par_hir_body_owners::{closure_env#0}<rustc_hir_analysis::check_crate::{closure_env#2}>>>
             at /home/user/external/rust/compiler/rustc_data_structures/src/sync/parallel.rs:41:15
  84: par_for_each_in<&rustc_span::def_id::LocalDefId, &[rustc_span::def_id::LocalDefId], rustc_middle::hir::map::{impl#3}::par_hir_body_owners::{closure_env#0}<rustc_hir_analysis::check_crate::{closure_env#2}>>
             at /home/user/external/rust/compiler/rustc_data_structures/src/sync/parallel.rs:177:5
  85: par_hir_body_owners<rustc_hir_analysis::check_crate::{closure_env#2}>
             at /home/user/external/rust/compiler/rustc_middle/src/hir/map.rs:340:9
  86: check_crate
             at /home/user/external/rust/compiler/rustc_hir_analysis/src/lib.rs:210:5
  87: run_required_analyses
             at /home/user/external/rust/compiler/rustc_interface/src/passes.rs:982:5
  88: analysis
             at /home/user/external/rust/compiler/rustc_interface/src/passes.rs:1057:5
  89: {closure#0}
             at /home/user/external/rust/compiler/rustc_query_impl/src/plumbing.rs:294:9
      [... omitted 33 frames ...]
  90: query_ensure<rustc_query_system::query::caches::SingleCache<rustc_middle::query::erase::Erased<[u8; 0]>>>
             at /home/user/external/rust/compiler/rustc_middle/src/query/plumbing.rs:198:9
  91: analysis
             at /home/user/external/rust/compiler/rustc_middle/src/query/plumbing.rs:233:9
  92: {closure#2}
             at /home/user/external/rust/compiler/rustc_driver_impl/src/lib.rs:366:13
  93: {closure#0}<core::option::Option<rustc_interface::queries::Linker>, rustc_driver_impl::run_compiler::{closure#0}::{closure_env#2}>
             at /home/user/external/rust/compiler/rustc_interface/src/passes.rs:909:27
  94: {closure#1}<rustc_interface::passes::create_and_enter_global_ctxt::{closure#2}::{closure_env#0}<core::option::Option<rustc_interface::queries::Linker>, rustc_driver_impl::run_compiler::{closure#0}::{closure_env#2}>, core::option::Option<rustc_interface::queries::Linker>>
             at /home/user/external/rust/compiler/rustc_middle/src/ty/context.rs:1480:37
  95: {closure#0}<rustc_middle::ty::context::{impl#20}::enter::{closure_env#1}<rustc_interface::passes::create_and_enter_global_ctxt::{closure#2}::{closure_env#0}<core::option::Option<rustc_interface::queries::Linker>, rustc_driver_impl::run_compiler::{closure#0}::{closure_env#2}>, core::option::Option<rustc_interface::queries::Linker>>, core::option::Option<rustc_interface::queries::Linker>>
             at /home/user/external/rust/compiler/rustc_middle/src/ty/context/tls.rs:60:9
  96: try_with<core::cell::Cell<*const ()>, rustc_middle::ty::context::tls::enter_context::{closure_env#0}<rustc_middle::ty::context::{impl#20}::enter::{closure_env#1}<rustc_interface::passes::create_and_enter_global_ctxt::{closure#2}::{closure_env#0}<core::option::Option<rustc_interface::queries::Linker>, rustc_driver_impl::run_compiler::{closure#0}::{closure_env#2}>, core::option::Option<rustc_interface::queries::Linker>>, core::option::Option<rustc_interface::queries::Linker>>, core::option::Option<rustc_interface::queries::Linker>>
             at /home/user/external/rust/library/std/src/thread/local.rs:315:12
  97: with<core::cell::Cell<*const ()>, rustc_middle::ty::context::tls::enter_context::{closure_env#0}<rustc_middle::ty::context::{impl#20}::enter::{closure_env#1}<rustc_interface::passes::create_and_enter_global_ctxt::{closure#2}::{closure_env#0}<core::option::Option<rustc_interface::queries::Linker>, rustc_driver_impl::run_compiler::{closure#0}::{closure_env#2}>, core::option::Option<rustc_interface::queries::Linker>>, core::option::Option<rustc_interface::queries::Linker>>, core::option::Option<rustc_interface::queries::Linker>>
             at /home/user/external/rust/library/std/src/thread/local.rs:279:15
  98: enter_context<rustc_middle::ty::context::{impl#20}::enter::{closure_env#1}<rustc_interface::passes::create_and_enter_global_ctxt::{closure#2}::{closure_env#0}<core::option::Option<rustc_interface::queries::Linker>, rustc_driver_impl::run_compiler::{closure#0}::{closure_env#2}>, core::option::Option<rustc_interface::queries::Linker>>, core::option::Option<rustc_interface::queries::Linker>>
             at /home/user/external/rust/compiler/rustc_middle/src/ty/context/tls.rs:57:5
  99: enter<rustc_interface::passes::create_and_enter_global_ctxt::{closure#2}::{closure_env#0}<core::option::Option<rustc_interface::queries::Linker>, rustc_driver_impl::run_compiler::{closure#0}::{closure_env#2}>, core::option::Option<rustc_interface::queries::Linker>>
             at /home/user/external/rust/compiler/rustc_middle/src/ty/context.rs:1480:9
 100: create_global_ctxt<core::option::Option<rustc_interface::queries::Linker>, rustc_interface::passes::create_and_enter_global_ctxt::{closure#2}::{closure_env#0}<core::option::Option<rustc_interface::queries::Linker>, rustc_driver_impl::run_compiler::{closure#0}::{closure_env#2}>>
             at /home/user/external/rust/compiler/rustc_middle/src/ty/context.rs:1700:9
 101: {closure#2}<core::option::Option<rustc_interface::queries::Linker>, rustc_driver_impl::run_compiler::{closure#0}::{closure_env#2}>
             at /home/user/external/rust/compiler/rustc_interface/src/passes.rs:876:9
 102: call_once<rustc_interface::passes::create_and_enter_global_ctxt::{closure_env#2}<core::option::Option<rustc_interface::queries::Linker>, rustc_driver_impl::run_compiler::{closure#0}::{closure_env#2}>, (&rustc_session::session::Session, rustc_middle::ty::context::CurrentGcx, alloc::sync::Arc<rustc_data_structures::jobserver::Proxy, alloc::alloc::Global>, &std::sync::once_lock::OnceLock<rustc_middle::ty::context::GlobalCtxt>, &rustc_data_structures::sync::worker_local::WorkerLocal<rustc_middle::arena::Arena>, &rustc_data_structures::sync::worker_local::WorkerLocal<rustc_hir::Arena>, rustc_driver_impl::run_compiler::{closure#0}::{closure_env#2})>
             at /home/user/external/rust/library/core/src/ops/function.rs:250:5
 103: call_once<(&rustc_session::session::Session, rustc_middle::ty::context::CurrentGcx, alloc::sync::Arc<rustc_data_structures::jobserver::Proxy, alloc::alloc::Global>, &std::sync::once_lock::OnceLock<rustc_middle::ty::context::GlobalCtxt>, &rustc_data_structures::sync::worker_local::WorkerLocal<rustc_middle::arena::Arena>, &rustc_data_structures::sync::worker_local::WorkerLocal<rustc_hir::Arena>, rustc_driver_impl::run_compiler::{closure#0}::{closure_env#2}), dyn core::ops::function::FnOnce<(&rustc_session::session::Session, rustc_middle::ty::context::CurrentGcx, alloc::sync::Arc<rustc_data_structures::jobserver::Proxy, alloc::alloc::Global>, &std::sync::once_lock::OnceLock<rustc_middle::ty::context::GlobalCtxt>, &rustc_data_structures::sync::worker_local::WorkerLocal<rustc_middle::arena::Arena>, &rustc_data_structures::sync::worker_local::WorkerLocal<rustc_hir::Arena>, rustc_driver_impl::run_compiler::{closure#0}::{closure_env#2}), Output=core::option::Option<rustc_interface::queries::Linker>>, alloc::alloc::Global>
             at /home/user/external/rust/library/alloc/src/boxed.rs:1966:9
 104: create_and_enter_global_ctxt<core::option::Option<rustc_interface::queries::Linker>, rustc_driver_impl::run_compiler::{closure#0}::{closure_env#2}>
             at /home/user/external/rust/compiler/rustc_interface/src/passes.rs:917:5
 105: {closure#0}
             at /home/user/external/rust/compiler/rustc_driver_impl/src/lib.rs:339:22
 106: {closure#0}<(), rustc_driver_impl::run_compiler::{closure_env#0}>
             at /home/user/external/rust/compiler/rustc_interface/src/interface.rs:527:80
 107: call_once<(), rustc_interface::interface::run_compiler::{closure#1}::{closure_env#0}<(), rustc_driver_impl::run_compiler::{closure_env#0}>>
             at /home/user/external/rust/library/core/src/panic/unwind_safe.rs:272:9
 108: do_call<core::panic::unwind_safe::AssertUnwindSafe<rustc_interface::interface::run_compiler::{closure#1}::{closure_env#0}<(), rustc_driver_impl::run_compiler::{closure_env#0}>>, ()>
             at /home/user/external/rust/library/std/src/panicking.rs:589:40
 109: try<(), core::panic::unwind_safe::AssertUnwindSafe<rustc_interface::interface::run_compiler::{closure#1}::{closure_env#0}<(), rustc_driver_impl::run_compiler::{closure_env#0}>>>
             at /home/user/external/rust/library/std/src/panicking.rs:552:19
 110: catch_unwind<core::panic::unwind_safe::AssertUnwindSafe<rustc_interface::interface::run_compiler::{closure#1}::{closure_env#0}<(), rustc_driver_impl::run_compiler::{closure_env#0}>>, ()>
             at /home/user/external/rust/library/std/src/panic.rs:359:14
 111: {closure#1}<(), rustc_driver_impl::run_compiler::{closure_env#0}>
             at /home/user/external/rust/compiler/rustc_interface/src/interface.rs:527:23
 112: {closure#0}<rustc_interface::interface::run_compiler::{closure_env#1}<(), rustc_driver_impl::run_compiler::{closure_env#0}>, ()>
             at /home/user/external/rust/compiler/rustc_interface/src/util.rs:200:17
 113: {closure#0}<rustc_interface::util::run_in_thread_pool_with_globals::{closure_env#0}<rustc_interface::interface::run_compiler::{closure_env#1}<(), rustc_driver_impl::run_compiler::{closure_env#0}>, ()>, ()>
             at /home/user/external/rust/compiler/rustc_interface/src/util.rs:154:24
 114: set<rustc_span::SessionGlobals, rustc_interface::util::run_in_thread_with_globals::{closure#0}::{closure#0}::{closure_env#0}<rustc_interface::util::run_in_thread_pool_with_globals::{closure_env#0}<rustc_interface::interface::run_compiler::{closure_env#1}<(), rustc_driver_impl::run_compiler::{closure_env#0}>, ()>, ()>, ()>
             at /home/user/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/scoped-tls-1.0.1/src/lib.rs:137:9
 115: create_session_globals_then<(), rustc_interface::util::run_in_thread_with_globals::{closure#0}::{closure#0}::{closure_env#0}<rustc_interface::util::run_in_thread_pool_with_globals::{closure_env#0}<rustc_interface::interface::run_compiler::{closure_env#1}<(), rustc_driver_impl::run_compiler::{closure_env#0}>, ()>, ()>>
             at /home/user/external/rust/compiler/rustc_span/src/lib.rs:144:5
 116: {closure#0}<rustc_interface::util::run_in_thread_pool_with_globals::{closure_env#0}<rustc_interface::interface::run_compiler::{closure_env#1}<(), rustc_driver_impl::run_compiler::{closure_env#0}>, ()>, ()>
             at /home/user/external/rust/compiler/rustc_interface/src/util.rs:150:17
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

I'm not sure if there are other scenarios that could reliably trigger this behavior. At least for the example in this issue it appears to center around lifetimes with HRTB. Can make the lifetimes more explicit like so:

//! This will reproduce the same ICE
trait MyFn<P> {}

struct Container<T> {
    data: T,
}

struct Desugared {
    // ICE also reproduces with for <'a, 'b>
    callback: Box<dyn for<'a> MyFn<&'a Container<&'a u8>>>,
}

fn ice(callback: Box<dyn for<'a> MyFn<Container<&'a u8>>>) -> Desugared {
    Desugared { callback }
}

Compare this to a version that does not reproduce the error by removing the HRTB on the initial value

struct Desugared {
    callback: Box<dyn for<'a> MyFn<&'a Container<&'a u8>>>,
}

fn no_ice<'a>(callback: Box<dyn MyFn<Container<&'a u8>>>) -> Desugared {
    Desugared { callback }
}

In this case it doesn't trigger the issue because it doesn't have a rolled back lifetime variable in found.

ExpectedFound {
    expected: Term::Ty(Container<&!1_0.Named(DefId(0:10 ~ rust_ice[c3ad]::Desugared::callback::'a), "'a") u8>),
    found: Term::Ty(Container<&ReLateParam(DefId(0:11 ~ rust_ice[c3ad]::ice), LateNamed(DefId(0:12 ~ rust_ice[c3ad]::ice::'a), 'a)) u8>),
},

@bors bors closed this as completed in 42ed69c May 20, 2025
rust-timer added a commit to rust-lang-ci/rust that referenced this issue May 20, 2025
Rollup merge of rust-lang#141236 - jagunter:issue-140823, r=compiler-errors

Resolved issue with mismatched types triggering ICE in certain scenarios

## Background

The function `annotate_mut_binding_to_immutable_binding` called in `emit_coerce_suggestions` performs a type comparison between the `expected` and `found` types from `ExpectedFound` in the `TypeError`. This can fail if the `found` type contains a region variable that's been rolled back.

## What is being changed?

This updates `annotate_mut_binding_to_immutable_binding` to use `expr_ty` and `expected` from the parent function instead of the types from the `TypeError`. This sidesteps the issue of using `found` from `TypeError` which may leak lingering inference region variables.

This does change the diagnostic behavior to _only_ support cases where the expected outermost type is `&T`, but that seems to be the intended functionality.

Also fixed the example in the `annotate_mut_binding_to_immutable_binding` rustdocs.

r? rust-lang/types

Fixes rust-lang#140823
@jieyouxu jieyouxu removed the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label May 21, 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. I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ S-has-bisection Status: A bisection has been found for this issue 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

Successfully merging a pull request may close this issue.

4 participants