Skip to content

ICE index out of bounds: the len is 0 but the index is 0 #127804

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
gurry opened this issue Jul 16, 2024 · 1 comment · Fixed by #135057
Closed

ICE index out of bounds: the len is 0 but the index is 0 #127804

gurry opened this issue Jul 16, 2024 · 1 comment · Fixed by #135057
Labels
C-bug Category: This is a bug. I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ S-bug-has-test Status: This bug is tracked inside the repo by a `known-bug` test. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@gurry
Copy link
Contributor

gurry commented Jul 16, 2024

Code

struct Thing;

pub trait Every {
    type Assoc;
}
impl<T: ?Sized> Every for Thing {
    type Assoc = T;
}

fn foo(_: <Thing as Every>::Assoc) {}

Meta

rustc --version --verbose:

rustc 1.81.0-dev
binary: rustc
commit-hash: unknown
commit-date: unknown
host: aarch64-apple-darwin
release: 1.81.0-dev
LLVM version: 18.1.7

Error output

Compiling playground v0.0.1 (/playground)
error[E0207]: the type parameter `T` is not constrained by the impl trait, self type, or predicates
 --> src/lib.rs:6:6
  |
6 | impl<T: ?Sized> Every for Thing {
  |      ^ unconstrained type parameter

error[E0282]: type annotations needed
  --> src/lib.rs:10:11
   |
10 | fn foo(_: <Thing as Every>::Assoc) {}
   |           ^^^^^^^^^^^^^^^^^^^^^^^ cannot infer type for type parameter `T`

error[E0282]: type annotations needed
  --> src/lib.rs:10:8
   |
10 | fn foo(_: <Thing as Every>::Assoc) {}
   |        ^ cannot infer type for type parameter `T`
Backtrace

thread 'rustc' panicked at /rust/deps/ena-0.14.2/src/snapshot_vec.rs:199:10:
index out of bounds: the len is 0 but the index is 0
stack backtrace:
   0:     0x7f0d4f6d5035 - std::backtrace_rs::backtrace::libunwind::trace::h1a07e5dba0da0cd2
                               at /rustc/129f3b9964af4d4a709d1383930ade12dfe7c081/library/std/src/../../backtrace/src/backtrace/libunwind.rs:105:5
   1:     0x7f0d4f6d5035 - std::backtrace_rs::backtrace::trace_unsynchronized::h61b9b8394328c0bc
                               at /rustc/129f3b9964af4d4a709d1383930ade12dfe7c081/library/std/src/../../backtrace/src/backtrace/mod.rs:66:5
   2:     0x7f0d4f6d5035 - std::sys_common::backtrace::_print_fmt::h1c5e18b460934cff
                               at /rustc/129f3b9964af4d4a709d1383930ade12dfe7c081/library/std/src/sys_common/backtrace.rs:68:5
   3:     0x7f0d4f6d5035 - <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt::h1e1a1972118942ad
                               at /rustc/129f3b9964af4d4a709d1383930ade12dfe7c081/library/std/src/sys_common/backtrace.rs:44:22
   4:     0x7f0d4f72429b - core::fmt::rt::Argument::fmt::h07af2b4071d536cd
                               at /rustc/129f3b9964af4d4a709d1383930ade12dfe7c081/library/core/src/fmt/rt.rs:165:63
   5:     0x7f0d4f72429b - core::fmt::write::hc090a2ffd6b28c4a
                               at /rustc/129f3b9964af4d4a709d1383930ade12dfe7c081/library/core/src/fmt/mod.rs:1157:21
   6:     0x7f0d4f6c9bdf - std::io::Write::write_fmt::h8898bac6ff039a23
                               at /rustc/129f3b9964af4d4a709d1383930ade12dfe7c081/library/std/src/io/mod.rs:1832:15
   7:     0x7f0d4f6d4e0e - std::sys_common::backtrace::_print::h4e80c5803d4ee35b
                               at /rustc/129f3b9964af4d4a709d1383930ade12dfe7c081/library/std/src/sys_common/backtrace.rs:47:5
   8:     0x7f0d4f6d4e0e - std::sys_common::backtrace::print::ha96650907276675e
                               at /rustc/129f3b9964af4d4a709d1383930ade12dfe7c081/library/std/src/sys_common/backtrace.rs:34:9
   9:     0x7f0d4f6d7779 - std::panicking::default_hook::{{closure}}::h215c2a0a8346e0e0
  10:     0x7f0d4f6d74bd - std::panicking::default_hook::h207342be97478370
                               at /rustc/129f3b9964af4d4a709d1383930ade12dfe7c081/library/std/src/panicking.rs:298:9
  11:     0x7f0d525d21b7 - std[3c8ba8ebcf555201]::panicking::update_hook::<alloc[bfbae7e348dce413]::boxed::Box<rustc_driver_impl[c88438ade88661f4]::install_ice_hook::{closure#0}>>::{closure#0}
  12:     0x7f0d4f6d7e76 - <alloc::boxed::Box<F,A> as core::ops::function::Fn<Args>>::call::ha9c3bc81d312fd83
                               at /rustc/129f3b9964af4d4a709d1383930ade12dfe7c081/library/alloc/src/boxed.rs:2036:9
  13:     0x7f0d4f6d7e76 - std::panicking::rust_panic_with_hook::hac8bdceee1e4fe2c
                               at /rustc/129f3b9964af4d4a709d1383930ade12dfe7c081/library/std/src/panicking.rs:799:13
  14:     0x7f0d4f6d7c24 - std::panicking::begin_panic_handler::{{closure}}::h00d785e82757ce3c
                               at /rustc/129f3b9964af4d4a709d1383930ade12dfe7c081/library/std/src/panicking.rs:664:13
  15:     0x7f0d4f6d54f9 - std::sys_common::backtrace::__rust_end_short_backtrace::h1628d957bcd06996
                               at /rustc/129f3b9964af4d4a709d1383930ade12dfe7c081/library/std/src/sys_common/backtrace.rs:171:18
  16:     0x7f0d4f6d7957 - rust_begin_unwind
                               at /rustc/129f3b9964af4d4a709d1383930ade12dfe7c081/library/std/src/panicking.rs:652:5
  17:     0x7f0d4f720763 - core::panicking::panic_fmt::hdc63834ffaaefae5
                               at /rustc/129f3b9964af4d4a709d1383930ade12dfe7c081/library/core/src/panicking.rs:72:14
  18:     0x7f0d4f720987 - core::panicking::panic_bounds_check::hd7e618b1b39cc1c3
                               at /rustc/129f3b9964af4d4a709d1383930ade12dfe7c081/library/core/src/panicking.rs:275:5
  19:     0x7f0d54a5f768 - <ena[7ba0c23a1613b29]::unify::UnificationTable<ena[7ba0c23a1613b29]::unify::backing_vec::InPlace<rustc_infer[abd41066181445be]::infer::type_variable::TyVidEqKey, &mut alloc[bfbae7e348dce413]::vec::Vec<ena[7ba0c23a1613b29]::unify::VarValue<rustc_infer[abd41066181445be]::infer::type_variable::TyVidEqKey>>, &mut rustc_infer[abd41066181445be]::infer::snapshot::undo_log::InferCtxtUndoLogs>>>::uninlined_get_root_key.llvm.6537034541696690420.cold
  20:     0x7f0d507a0045 - <rustc_infer[abd41066181445be]::infer::canonical::canonicalizer::Canonicalizer as rustc_type_ir[a145be3c51398263]::fold::TypeFolder<rustc_middle[3ff731b746e7b038]::ty::context::TyCtxt>>::fold_ty
  21:     0x7f0d53dd6b38 - <rustc_infer[abd41066181445be]::infer::canonical::canonicalizer::Canonicalizer as rustc_type_ir[a145be3c51398263]::fold::FallibleTypeFolder<rustc_middle[3ff731b746e7b038]::ty::context::TyCtxt>>::try_fold_predicate
  22:     0x7f0d5115701f - rustc_trait_selection[173dcaf5d960508e]::traits::type_known_to_meet_bound_modulo_regions
  23:     0x7f0d53ba840b - rustc_query_impl[a12402620de91e8]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[a12402620de91e8]::query_impl::is_copy_raw::dynamic_query::{closure#2}::{closure#0}, rustc_middle[3ff731b746e7b038]::query::erase::Erased<[u8; 1usize]>>
  24:     0x7f0d53ba93c6 - rustc_query_system[4e189ce2c77124d]::query::plumbing::try_execute_query::<rustc_query_impl[a12402620de91e8]::DynamicConfig<rustc_query_system[4e189ce2c77124d]::query::caches::DefaultCache<rustc_middle[3ff731b746e7b038]::ty::ParamEnvAnd<rustc_middle[3ff731b746e7b038]::ty::Ty>, rustc_middle[3ff731b746e7b038]::query::erase::Erased<[u8; 1usize]>>, false, false, false>, rustc_query_impl[a12402620de91e8]::plumbing::QueryCtxt, false>
  25:     0x7f0d53ba90ce - rustc_query_impl[a12402620de91e8]::query_impl::is_copy_raw::get_query_non_incr::__rust_end_short_backtrace
  26:     0x7f0d53baa0eb - rustc_ty_utils[b79b74a9f824867d]::needs_drop::needs_drop_raw
  27:     0x7f0d53ba9b59 - rustc_query_impl[a12402620de91e8]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[a12402620de91e8]::query_impl::needs_drop_raw::dynamic_query::{closure#2}::{closure#0}, rustc_middle[3ff731b746e7b038]::query::erase::Erased<[u8; 1usize]>>
  28:     0x7f0d53ba936a - rustc_query_system[4e189ce2c77124d]::query::plumbing::try_execute_query::<rustc_query_impl[a12402620de91e8]::DynamicConfig<rustc_query_system[4e189ce2c77124d]::query::caches::DefaultCache<rustc_middle[3ff731b746e7b038]::ty::ParamEnvAnd<rustc_middle[3ff731b746e7b038]::ty::Ty>, rustc_middle[3ff731b746e7b038]::query::erase::Erased<[u8; 1usize]>>, false, false, false>, rustc_query_impl[a12402620de91e8]::plumbing::QueryCtxt, false>
  29:     0x7f0d53ba900e - rustc_query_impl[a12402620de91e8]::query_impl::needs_drop_raw::get_query_non_incr::__rust_end_short_backtrace
  30:     0x7f0d50b12f93 - <rustc_mir_transform[2984d3182075647]::elaborate_drops::ElaborateDrops as rustc_middle[3ff731b746e7b038]::mir::MirPass>::run_pass
  31:     0x7f0d5394a4cd - rustc_mir_transform[2984d3182075647]::pass_manager::run_passes_inner
  32:     0x7f0d5141bff7 - rustc_mir_transform[2984d3182075647]::mir_drops_elaborated_and_const_checked
  33:     0x7f0d53a5ec31 - rustc_query_impl[a12402620de91e8]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[a12402620de91e8]::query_impl::mir_drops_elaborated_and_const_checked::dynamic_query::{closure#2}::{closure#0}, rustc_middle[3ff731b746e7b038]::query::erase::Erased<[u8; 8usize]>>
  34:     0x7f0d53a5ef2d - rustc_query_system[4e189ce2c77124d]::query::plumbing::try_execute_query::<rustc_query_impl[a12402620de91e8]::DynamicConfig<rustc_query_system[4e189ce2c77124d]::query::caches::VecCache<rustc_span[8c7415e9d33ddd75]::def_id::LocalDefId, rustc_middle[3ff731b746e7b038]::query::erase::Erased<[u8; 8usize]>>, false, false, false>, rustc_query_impl[a12402620de91e8]::plumbing::QueryCtxt, false>
  35:     0x7f0d53a5e78c - rustc_query_impl[a12402620de91e8]::query_impl::mir_drops_elaborated_and_const_checked::get_query_non_incr::__rust_end_short_backtrace
  36:     0x7f0d542d2432 - rustc_interface[640972162e3c086f]::passes::analysis
  37:     0x7f0d542d1aef - rustc_query_impl[a12402620de91e8]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[a12402620de91e8]::query_impl::analysis::dynamic_query::{closure#2}::{closure#0}, rustc_middle[3ff731b746e7b038]::query::erase::Erased<[u8; 1usize]>>
  38:     0x7f0d5462b022 - rustc_query_system[4e189ce2c77124d]::query::plumbing::try_execute_query::<rustc_query_impl[a12402620de91e8]::DynamicConfig<rustc_query_system[4e189ce2c77124d]::query::caches::SingleCache<rustc_middle[3ff731b746e7b038]::query::erase::Erased<[u8; 1usize]>>, false, false, false>, rustc_query_impl[a12402620de91e8]::plumbing::QueryCtxt, false>
  39:     0x7f0d5462adc9 - rustc_query_impl[a12402620de91e8]::query_impl::analysis::get_query_non_incr::__rust_end_short_backtrace
  40:     0x7f0d544cc149 - rustc_interface[640972162e3c086f]::interface::run_compiler::<core[868bc93c3f2beb33]::result::Result<(), rustc_span[8c7415e9d33ddd75]::ErrorGuaranteed>, rustc_driver_impl[c88438ade88661f4]::run_compiler::{closure#0}>::{closure#1}
  41:     0x7f0d54489f8b - std[3c8ba8ebcf555201]::sys_common::backtrace::__rust_begin_short_backtrace::<rustc_interface[640972162e3c086f]::util::run_in_thread_with_globals<rustc_interface[640972162e3c086f]::interface::run_compiler<core[868bc93c3f2beb33]::result::Result<(), rustc_span[8c7415e9d33ddd75]::ErrorGuaranteed>, rustc_driver_impl[c88438ade88661f4]::run_compiler::{closure#0}>::{closure#1}, core[868bc93c3f2beb33]::result::Result<(), rustc_span[8c7415e9d33ddd75]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[868bc93c3f2beb33]::result::Result<(), rustc_span[8c7415e9d33ddd75]::ErrorGuaranteed>>
  42:     0x7f0d54489d80 - <<std[3c8ba8ebcf555201]::thread::Builder>::spawn_unchecked_<rustc_interface[640972162e3c086f]::util::run_in_thread_with_globals<rustc_interface[640972162e3c086f]::interface::run_compiler<core[868bc93c3f2beb33]::result::Result<(), rustc_span[8c7415e9d33ddd75]::ErrorGuaranteed>, rustc_driver_impl[c88438ade88661f4]::run_compiler::{closure#0}>::{closure#1}, core[868bc93c3f2beb33]::result::Result<(), rustc_span[8c7415e9d33ddd75]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[868bc93c3f2beb33]::result::Result<(), rustc_span[8c7415e9d33ddd75]::ErrorGuaranteed>>::{closure#2} as core[868bc93c3f2beb33]::ops::function::FnOnce<()>>::call_once::{shim:vtable#0}
  43:     0x7f0d4f6e1cab - <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once::h09e5a4c541afa800
                               at /rustc/129f3b9964af4d4a709d1383930ade12dfe7c081/library/alloc/src/boxed.rs:2022:9
  44:     0x7f0d4f6e1cab - <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once::h9c8b03c22f4e7026
                               at /rustc/129f3b9964af4d4a709d1383930ade12dfe7c081/library/alloc/src/boxed.rs:2022:9
  45:     0x7f0d4f6e1cab - std::sys::pal::unix::thread::Thread::new::thread_start::h522bc89a54da820a
                               at /rustc/129f3b9964af4d4a709d1383930ade12dfe7c081/library/std/src/sys/pal/unix/thread.rs:108:17
  46:     0x7f0d4f5ef609 - start_thread
  47:     0x7f0d4f512353 - clone
  48:                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.79.0 (129f3b996 2024-06-10) running on x86_64-unknown-linux-gnu

note: compiler flags: --crate-type lib -C embed-bitcode=no -C codegen-units=1 -C debuginfo=2

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

query stack during panic:
#0 [is_copy_raw] computing whether `_` is `Copy`
#1 [needs_drop_raw] computing whether `_` needs drop
#2 [mir_drops_elaborated_and_const_checked] elaborating drops for `foo`
#3 [analysis] running analysis passes on this crate
end of query stack
Some errors have detailed explanations: E0207, E0282.
For more information about an error, try `rustc --explain E0207`.
error: could not compile `playground` (lib) due to 3 previous errors

@gurry gurry 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 Jul 16, 2024
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Jul 16, 2024
@gurry
Copy link
Contributor Author

gurry commented Jul 16, 2024

First reported here: #126942 (comment)

@saethlin saethlin removed the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Jul 17, 2024
@matthiaskrgr matthiaskrgr added the S-bug-has-test Status: This bug is tracked inside the repo by a `known-bug` test. label Sep 18, 2024
bors added a commit to rust-lang-ci/rust that referenced this issue Jan 3, 2025
…d, r=<try>

Project to `TyKind::Error` when there are unconstrained non-lifetime (ty/const) impl params

I think this is a bit less invasive of an approach compared to rust-lang#127973.

It splits the `enforce_impl_params_are_constrained` function into lifetime/non-lifetime, and queryfies the latter. We can then use the result of the latter query (`Result<(), ErrorGuaranteed>`) to intercept projection and constrain the projected type to `TyKind::Error`, which ensures that we leak no ty or const vars to places that don't expect them, like `normalize_erasing_regions`.

The reason we split `enforce_impl_params_are_constrained` into two parts is because we only error for *lifetimes* if the lifetime ends up showing up in any of the associated types of the impl (e.g. we allow `impl<'a> Foo { type Assoc = (); }`). However, in order to compute the `type_of` query for the anonymous associated type of an RPITIT, we need to do trait solving (in `query collect_return_position_impl_trait_in_trait_tys`). That would induce cycles. Luckily, it turns out for lifetimes we don't even care about if they're unconstrained, since they're erased in all contexts that we are trying to fix ICEs. So it's sufficient to keep this check separated out of the query.

Fixes rust-lang#123141
Fixes rust-lang#125874
Fixes rust-lang#126942
Fixes rust-lang#127804
Fixes rust-lang#130967

r? oli-obk
bors added a commit to rust-lang-ci/rust that referenced this issue Jan 3, 2025
…d, r=<try>

Project to `TyKind::Error` when there are unconstrained non-lifetime (ty/const) impl params

It splits the `enforce_impl_params_are_constrained` function into lifetime/non-lifetime, and queryfies the latter. We can then use the result of the latter query (`Result<(), ErrorGuaranteed>`) to intercept projection and constrain the projected type to `TyKind::Error`, which ensures that we leak no ty or const vars to places that don't expect them, like `normalize_erasing_regions`.

The reason we split `enforce_impl_params_are_constrained` into two parts is because we only error for *lifetimes* if the lifetime ends up showing up in any of the associated types of the impl (e.g. we allow `impl<'a> Foo { type Assoc = (); }`). However, in order to compute the `type_of` query for the anonymous associated type of an RPITIT, we need to do trait solving (in `query collect_return_position_impl_trait_in_trait_tys`). That would induce cycles. Luckily, it turns out for lifetimes we don't even care about if they're unconstrained, since they're erased in all contexts that we are trying to fix ICEs. So it's sufficient to keep this check separated out of the query.

I think this is a bit less invasive of an approach compared to rust-lang#127973. The major difference between this PR and that PR is that we queryify the check instead of merging it into the `explicit_predicates_of` query, and we use the result to taint just projection goals, rather than trait goals too. This doesn't require a lot of new tracking in `ItemCtxt` and `GenericPredicates`, and it also seems to not require any other changes to typeck like that PR did.

Fixes rust-lang#123141
Fixes rust-lang#125874
Fixes rust-lang#126942
Fixes rust-lang#127804
Fixes rust-lang#130967

r? oli-obk
bors added a commit to rust-lang-ci/rust that referenced this issue Jan 3, 2025
…d, r=oli-obk

Project to `TyKind::Error` when there are unconstrained non-lifetime (ty/const) impl params

It splits the `enforce_impl_params_are_constrained` function into lifetime/non-lifetime, and queryfies the latter. We can then use the result of the latter query (`Result<(), ErrorGuaranteed>`) to intercept projection and constrain the projected type to `TyKind::Error`, which ensures that we leak no ty or const vars to places that don't expect them, like `normalize_erasing_regions`.

The reason we split `enforce_impl_params_are_constrained` into two parts is because we only error for *lifetimes* if the lifetime ends up showing up in any of the associated types of the impl (e.g. we allow `impl<'a> Foo { type Assoc = (); }`). However, in order to compute the `type_of` query for the anonymous associated type of an RPITIT, we need to do trait solving (in `query collect_return_position_impl_trait_in_trait_tys`). That would induce cycles. Luckily, it turns out for lifetimes we don't even care about if they're unconstrained, since they're erased in all contexts that we are trying to fix ICEs. So it's sufficient to keep this check separated out of the query.

I think this is a bit less invasive of an approach compared to rust-lang#127973. The major difference between this PR and that PR is that we queryify the check instead of merging it into the `explicit_predicates_of` query, and we use the result to taint just projection goals, rather than trait goals too. This doesn't require a lot of new tracking in `ItemCtxt` and `GenericPredicates`, and it also seems to not require any other changes to typeck like that PR did.

Fixes rust-lang#123141
Fixes rust-lang#125874
Fixes rust-lang#126942
Fixes rust-lang#127804
Fixes rust-lang#130967

r? oli-obk
@bors bors closed this as completed in 7349f6b Jan 4, 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-bug-has-test Status: This bug is tracked inside the repo by a `known-bug` test. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
4 participants