Skip to content

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

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
LennyPhoenix opened this issue Feb 12, 2025 · 4 comments · Fixed by #136951
Closed

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

LennyPhoenix opened this issue Feb 12, 2025 · 4 comments · Fixed by #136951
Assignees
Labels
C-bug Category: This is a bug. F-trait_alias `#![feature(trait_alias)]` 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

@LennyPhoenix
Copy link

rustc-ice-2025-02-12T19_02_34-223082.txt

Code

futures = "0.3.31"
#![feature(trait_alias)]

use futures::channel::mpsc::{channel, Sender};
use futures::future::BoxFuture;
use futures::StreamExt;
use std::future::Future;
use std::marker::PhantomData;

pub trait Spawn: Send {
    fn spawn(future: impl Future<Output = ()> + Send + 'static);
}

pub trait FnOnceTrait<'a, T: 'a, U> = FnOnce(&'a mut T) -> BoxFuture<'a, U> + Send + 'static;
pub type BoxFn<T> = Box<dyn for<'a> FnOnceTrait<'a, T, ()>>;

pub struct Critical<S, T>(Sender<BoxFn<T>>, PhantomData<S>);

impl<S: Spawn, T: Send + 'static> Critical<S, T> {
    pub fn new(mut value: T) -> Self {
        let (sink, mut stream) = channel::<BoxFn<T>>(1024);

        S::spawn(async move {
            while let Some(f) = stream.next().await {
                f(&mut value).await;
            }
        });

        Self(sink, PhantomData)
    }
}

Meta

rustc --version --verbose:

rustc 1.86.0-nightly (92bedea1c 2025-02-11)
binary: rustc
commit-hash: 92bedea1c51e3a969d60972be854506ffd8c5cb6
commit-date: 2025-02-11
host: x86_64-unknown-linux-gnu
release: 1.86.0-nightly
LLVM version: 19.1.7

Error output

   Compiling error_test v0.1.0 (/home/lenny/Projects/error_test)

thread 'rustc' panicked at compiler/rustc_infer/src/infer/mod.rs:1204:26:
index out of bounds: the len is 0 but the index is 0
note: run with `RUST_BACKTRACE=1` environment variable to display a 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: please make sure that you have updated to the latest nightly

note: please attach the file at `/home/lenny/Projects/error_test/rustc-ice-2025-02-12T19_00_29-220361.txt` to your bug report

note: compiler flags: --crate-type bin -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 [evaluate_obligation] evaluating trait selection obligation `alloc::boxed::Box<(dyn for<'a> core::ops::function::FnOnce(&'a mut T) -> core::pin::Pin<alloc::boxed::Box<(dyn core::future:
:future::Future<Output = ()> + core::marker::Send + 'a)>> + core::marker::Send + 'b)>: core::ops::function::FnOnce<(^1_1,)>`
#1 [typeck] type-checking `<impl at src/main.rs:18:1: 18:49>::new`
... and 1 other queries... use `env RUST_BACKTRACE=1` to see the full query stack
error: could not compile `error_test` (bin "error_test")

Caused by:
  process didn't exit successfully: `/home/lenny/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/bin/rustc --crate-name error_test --edition=2021 src/main.rs --error-format=json --json
=diagnostic-rendered-ansi,artifacts,future-incompat --diagnostic-width=188 --crate-type bin --emit=dep-info,link -C embed-bitcode=no -C debuginfo=2 --check-cfg 'cfg(docsrs,test)' --check-c
fg 'cfg(feature, values())' -C metadata=2766be39fb0fc4e7 -C extra-filename=-e06ef52b36ba5195 --out-dir /home/lenny/Projects/error_test/target/debug/deps -C incremental=/home/lenny/Projects
/error_test/target/debug/incremental -L dependency=/home/lenny/Projects/error_test/target/debug/deps --extern chrono=/home/lenny/Projects/error_test/target/debug/deps/libchrono-4f2b9403fa2
f8267.rlib --extern chrono_tz=/home/lenny/Projects/error_test/target/debug/deps/libchrono_tz-2bfc3c953512e5e9.rlib --extern futures=/home/lenny/Projects/error_test/target/debug/deps/libfut
ures-b2e5c549630d83b4.rlib --extern thiserror=/home/lenny/Projects/error_test/target/debug/deps/libthiserror-91714834ffdcbc83.rlib --extern tokio=/home/lenny/Projects/error_test/target/deb
ug/deps/libtokio-c4e8920040c3c7f3.rlib --extern tokio_stream=/home/lenny/Projects/error_test/target/debug/deps/libtokio_stream-e2c0de47bcea37dc.rlib --extern tokio_util=/home/lenny/Project
s/error_test/target/debug/deps/libtokio_util-858aafb43cee218e.rlib` (exit status: 101)

Backtrace

   Compiling error_test v0.1.0 (/home/lenny/Projects/error_test)

thread 'rustc' panicked at compiler/rustc_infer/src/infer/mod.rs:1204:26:
index out of bounds: the len is 0 but the index is 0
stack backtrace:
   0: rust_begin_unwind
   1: core::panicking::panic_fmt
   2: core::panicking::panic_bounds_check
   3: <rustc_middle::ty::fold::BoundVarReplacer<<rustc_infer::infer::InferCtxt>::instantiate_binder_with_fresh_vars::ToFreshVars> as rustc_type_ir::fold::TypeFolder<rustc_middle::ty::conte
xt::TyCtxt>>::fold_ty
   4: <&rustc_middle::ty::list::RawList<(), rustc_middle::ty::Ty> as rustc_type_ir::fold::TypeFoldable<rustc_middle::ty::context::TyCtxt>>::try_fold_with::<rustc_middle::ty::fold::BoundVar
Replacer<<rustc_infer::infer::InferCtxt>::instantiate_binder_with_fresh_vars::ToFreshVars>>
   5: <rustc_middle::ty::fold::BoundVarReplacer<<rustc_infer::infer::InferCtxt>::instantiate_binder_with_fresh_vars::ToFreshVars> as rustc_type_ir::fold::TypeFolder<rustc_middle::ty::conte
xt::TyCtxt>>::fold_ty
   6: <&rustc_middle::ty::list::RawList<(), rustc_middle::ty::generic_args::GenericArg> as rustc_type_ir::fold::TypeFoldable<rustc_middle::ty::context::TyCtxt>>::try_fold_with::<rustc_midd
le::ty::fold::BoundVarReplacer<<rustc_infer::infer::InferCtxt>::instantiate_binder_with_fresh_vars::ToFreshVars>>
   7: <rustc_infer::infer::InferCtxt>::instantiate_binder_with_fresh_vars::<rustc_type_ir::predicate::TraitRef<rustc_middle::ty::context::TyCtxt>>
   8: <rustc_trait_selection::traits::select::SelectionContext>::match_normalize_trait_ref
   9: <rustc_trait_selection::traits::select::SelectionContext>::candidate_from_obligation::{closure#0}::{closure#0}
  10: <rustc_trait_selection::traits::select::SelectionContext>::candidate_from_obligation::{closure#0}
  11: <rustc_trait_selection::traits::select::SelectionContext>::evaluate_trait_predicate_recursively
  12: <rustc_trait_selection::traits::select::SelectionContext>::evaluate_candidate::{closure#0}
  13: <rustc_trait_selection::traits::select::SelectionContext>::evaluate_trait_predicate_recursively
  14: <rustc_trait_selection::traits::select::SelectionContext>::evaluate_root_obligation
  15: rustc_traits::evaluate_obligation::evaluate_obligation
      [... omitted 1 frame ...]
  16: <rustc_infer::infer::InferCtxt as rustc_trait_selection::traits::query::evaluate_obligation::InferCtxtExt>::evaluate_obligation_no_overflow
  17: <rustc_hir_typeck::fn_ctxt::FnCtxt>::lookup_method_in_trait
  18: <rustc_hir_typeck::fn_ctxt::FnCtxt>::try_overloaded_call_traits
  19: <rustc_hir_typeck::fn_ctxt::FnCtxt>::check_expr_with_expectation_and_args
  20: <rustc_hir_typeck::fn_ctxt::FnCtxt>::confirm_builtin_call
  21: <rustc_hir_typeck::fn_ctxt::FnCtxt>::check_expr_with_expectation_and_args
  22: <rustc_hir_typeck::fn_ctxt::FnCtxt>::check_expr_match::{closure#0}
  23: <rustc_hir_typeck::fn_ctxt::FnCtxt>::check_expr_with_expectation_and_args
  24: <rustc_hir_typeck::fn_ctxt::FnCtxt>::check_expr_block
  25: <rustc_hir_typeck::fn_ctxt::FnCtxt>::check_expr_with_expectation_and_args
  26: <rustc_hir_typeck::fn_ctxt::FnCtxt>::check_expr_with_expectation_and_args
  27: <rustc_hir_typeck::fn_ctxt::FnCtxt>::check_expr_block
  28: <rustc_hir_typeck::fn_ctxt::FnCtxt>::check_expr_with_expectation_and_args
  29: <rustc_hir_typeck::fn_ctxt::FnCtxt>::check_expr_block
  30: <rustc_hir_typeck::fn_ctxt::FnCtxt>::check_expr_with_expectation_and_args
  31: rustc_hir_typeck::check::check_fn
  32: <rustc_hir_typeck::fn_ctxt::FnCtxt>::check_expr_closure
  33: <rustc_hir_typeck::fn_ctxt::FnCtxt>::check_expr_with_expectation_and_args
  34: <rustc_hir_typeck::fn_ctxt::FnCtxt>::confirm_builtin_call
  35: <rustc_hir_typeck::fn_ctxt::FnCtxt>::check_expr_with_expectation_and_args
  36: <rustc_hir_typeck::fn_ctxt::FnCtxt>::check_expr_block
  37: <rustc_hir_typeck::fn_ctxt::FnCtxt>::check_expr_with_expectation_and_args
  38: rustc_hir_typeck::check::check_fn
  39: rustc_hir_typeck::typeck_with_inspect::{closure#0}
      [... omitted 1 frame ...]
  40: <rustc_middle::hir::map::Map>::par_body_owners::<rustc_hir_analysis::check_crate::{closure#3}>::{closure#0}
  41: rustc_hir_analysis::check_crate
  42: rustc_interface::passes::run_required_analyses
  43: rustc_interface::passes::analysis
      [... omitted 1 frame ...]
  44: 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}
  45: 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: please make sure that you have updated to the latest nightly

note: please attach the file at `/home/lenny/Projects/error_test/rustc-ice-2025-02-12T19_01_14-220588.txt` to your bug report

note: compiler flags: --crate-type bin -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 [evaluate_obligation] evaluating trait selection obligation `alloc::boxed::Box<(dyn for<'a> core::ops::function::FnOnce(&'a mut T) -> core::pin::Pin<alloc::boxed::Box<(dyn core::future:
:future::Future<Output = ()> + core::marker::Send + 'a)>> + core::marker::Send + 'b)>: core::ops::function::FnOnce<(^1_1,)>`
#1 [typeck] type-checking `<impl at src/main.rs:18:1: 18:49>::new`
#2 [analysis] running analysis passes on this crate
end of query stack
error: could not compile `error_test` (bin "error_test")

Caused by:
  process didn't exit successfully: `/home/lenny/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/bin/rustc --crate-name error_test --edition=2021 src/main.rs --error-format=json --json
=diagnostic-rendered-ansi,artifacts,future-incompat --diagnostic-width=188 --crate-type bin --emit=dep-info,link -C embed-bitcode=no -C debuginfo=2 --check-cfg 'cfg(docsrs,test)' --check-c
fg 'cfg(feature, values())' -C metadata=2766be39fb0fc4e7 -C extra-filename=-e06ef52b36ba5195 --out-dir /home/lenny/Projects/error_test/target/debug/deps -C incremental=/home/lenny/Projects
/error_test/target/debug/incremental -L dependency=/home/lenny/Projects/error_test/target/debug/deps --extern chrono=/home/lenny/Projects/error_test/target/debug/deps/libchrono-4f2b9403fa2
f8267.rlib --extern chrono_tz=/home/lenny/Projects/error_test/target/debug/deps/libchrono_tz-2bfc3c953512e5e9.rlib --extern futures=/home/lenny/Projects/error_test/target/debug/deps/libfut
ures-b2e5c549630d83b4.rlib --extern thiserror=/home/lenny/Projects/error_test/target/debug/deps/libthiserror-91714834ffdcbc83.rlib --extern tokio=/home/lenny/Projects/error_test/target/deb
ug/deps/libtokio-c4e8920040c3c7f3.rlib --extern tokio_stream=/home/lenny/Projects/error_test/target/debug/deps/libtokio_stream-e2c0de47bcea37dc.rlib --extern tokio_util=/home/lenny/Project
s/error_test/target/debug/deps/libtokio_util-858aafb43cee218e.rlib` (exit status: 101)

@LennyPhoenix LennyPhoenix added C-bug Category: This is a bug. I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Feb 12, 2025
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Feb 12, 2025
@LennyPhoenix LennyPhoenix changed the title thread 'rustc' panicked at compiler/rustc_infer/src/infer/mod.rs ICE: index out of bounds: the len is 0 but the index is 0 Feb 12, 2025
@timmypidashev
Copy link

The ICE seems to happen during trait selection when trying to handle higher-rank trait bounds (HRTB) with a trait alias. The empty slice access suggests it might be failing to properly handle the lifetimes or parameters in the trait selection process.

Specifically, when evaluating the trait selection:

evaluating trait selection obligation `alloc::boxed::Box<(dyn for<'a> core::ops::function::FnOnce(&'a mut T) -> core::pin::Pin<alloc::boxed::Box<(dyn core::future::future::Future<Output = ()> + core::marker::Send + 'a)>> + core::marker::Send + 'b)>: core::ops::function::FnOnce<(^1_1,)>`

I am way in over my head here, but I'll build the rust compiler locally and attempt to resolve a fix.

Seems the lead we have is the ranking of the trait bounds causing the panic...

@cyrgani
Copy link
Contributor

cyrgani commented Feb 12, 2025

Reduced:

#![feature(trait_alias)]
trait FnOnceTrait<'a> = FnOnce(&'a ());

fn g() -> Box<dyn for<'a> FnOnceTrait<'a>> {
    todo!()
}

fn main() {
    g().foo();
}

bisection:
searched nightlies: from nightly-2024-12-16 to nightly-2025-02-12
regressed nightly: nightly-2025-01-22
searched commit range: f3d1d47...ed43cbc
regressed commit: cd805f0

bisected with cargo-bisect-rustc v0.6.9

Host triple: x86_64-unknown-linux-gnu
Reproduce with:

cargo bisect-rustc --start=2024-12-16 --script=run.sh 

@rustbot label:S-has-mcve S-has-bisection F-trait_alias

@rustbot rustbot added S-has-mcve Status: A Minimal Complete and Verifiable Example has been found for this issue F-trait_alias `#![feature(trait_alias)]` S-has-bisection Status: a bisection has been found for this issue labels Feb 12, 2025
@timmypidashev
Copy link

timmypidashev commented Feb 12, 2025

bisection: searched nightlies: from nightly-2024-12-16 to nightly-2025-02-12 regressed nightly: nightly-2025-01-22 searched commit range: f3d1d47...ed43cbc regressed commit: cd805f0

bisected with cargo-bisect-rustc v0.6.9
@rustbot label:S-has-mcve S-has-bisection F-trait_alias

Great work. That commit in question was a fix pertaining to trait aliases.

I've never seen a bisection work so well before 🙂

@lqd
Copy link
Member

lqd commented Feb 12, 2025

cc @compiler-errors on that bisection

@compiler-errors compiler-errors self-assigned this Feb 12, 2025
@saethlin saethlin removed the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Feb 13, 2025
workingjubilee added a commit to workingjubilee/rustc that referenced this issue Feb 14, 2025
Use the right binder for rebinding `PolyTraitRef`

Fixes rust-lang#136940

I committed a slightly different test which still demonstrates the issue.
@bors bors closed this as completed in 3957eaa Feb 14, 2025
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Feb 14, 2025
Rollup merge of rust-lang#136951 - compiler-errors:clause-binder, r=lqd

Use the right binder for rebinding `PolyTraitRef`

Fixes rust-lang#136940

I committed a slightly different test which still demonstrates the issue.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug. F-trait_alias `#![feature(trait_alias)]` 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.

7 participants