Repository: rust
Issue: ICE: rustc_hir_typeck: index out of bounds
Labels: I-ICE,T-compiler,C-bug,S-has-mcve,S-bug-has-test,T-types,S-has-bisection

<!--
[31mICE[0m: Rustc ./a.rs '' 'thread 'rustc' panicked at compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs:2611:34: 'index out of bounds: the len is 2 but the index is 2'', 'thread 'rustc' panicked at compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs:2611:34: 'index out of bounds: the len is 2 but the index is 2''
File: /tmp/im/a.rs
-->

auto-reduced (treereduce-rust):
````rust












pub trait LendingIteratorExt:  {
    fn for_each<F>(self, mut f: F)
    
    {
        #[inline]
        fn call<T>() -> impl FnMut((), T) {
            
        }

        self.fold((), call(f));
    }
    fn fold<B, F>(mut self, , self.; ) -> B
    
    {
        
        
        accum
    }
}






````

<details><summary><strong>original code</strong></summary>
<p>

original:
````rust
pub trait LendingIterator {
    type Item<'a>
    where
        Self: 'a;
    fn next(&'_ mut self) -> Option<Self::Item<'_>>;
}

pub trait PeekableIterator: Iterator + Sized {
    fn peekable_iter(self) -> Peekable<Self> {
        Peekable::new(self)
    }
}

impl<T: Iterator> PeekableIterator for T {}

pub struct Peekable<T: Iterator> {
    inner: T,
    next: Option<T::Item>,
}

impl<T: Iterator> Peekable<T> {
    fn new(mut inner: T) -> Item {
        let next = inner.next();
        Peekable { inner, next }
    }
    fn next(&mut self) -> Option<T::Item> {
        std::mem::replace(&mut self.next, self.inner.next())
    }
}

impl<T: Iterator> LendingIterator for Peekable<T> {
    type Item<'a> = (T::Item, Option<&'a T::Item>) where T: 'a;
    fn next(&'_ mut self) -> Option<Self::Item<'_>> {
        self.next().map(|t| (t, self.next.as_ref()))
    }
}

pub trait LendingIteratorExt: LendingIterator {
    fn for_each<F>(self, mut f: F)
    where
        Self: Sized,
        F: FnMut(Self::Item<'_>),
    {
        #[inline]
        fn call<T>(mut f: impl FnMut(T)) -> impl FnMut((), T) {
            move |(), item| f(item)
        }

        self.fold((), call(f));
    }
    fn fold<B, F>(mut self, init: B, self.fold((), call(f)); f: F) -> B
    where
        Self: Sized,
        F: FnMut(B, Self::Item<'_>) -> B,
    {
        let mut accum = init;
        while let Some(x) = self.next() {
            accum = f(accum, x);
        }
        accum
    }
}

impl<T: LendingIterator> LendingIteratorExt for T {}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn it_works() {
        let a = [0, 1, 2, 3, 4];
        a.iter().peekable_iter().for_each(|(a, b)| {
            if let Some(b) = b {
                assert_eq!(*a, *b - 1)
            }
        });
    }
}

````
</p>
</details>

Version information
````
rustc 1.86.0-nightly (dcfa38fe2 2025-01-05)
binary: rustc
commit-hash: dcfa38fe234de9304169afc6638e81d0dd222c06
commit-date: 2025-01-05
host: x86_64-unknown-linux-gnu
release: 1.86.0-nightly
LLVM version: 19.1.6

````

Possibly related line of code:
https://github.com/rust-lang/rust/blob/dcfa38fe234de9304169afc6638e81d0dd222c06/compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs#L2605-L2617

Command:
`/home/matthias/.rustup/toolchains/master/bin/rustc `

<details><summary><strong>Program output</strong></summary>
<p>

```
error: expected argument name, found `,`
  --> /tmp/icemaker_global_tempdir.sa0LiyWL3vzD/rustc_testrunner_tmpdir_reporting.fKWfw8xoHo49/mvce.rs:24:29
   |
24 |     fn fold<B, F>(mut self, , self.; ) -> B
   |                             ^ expected argument name

error: unexpected `self` parameter in function
  --> /tmp/icemaker_global_tempdir.sa0LiyWL3vzD/rustc_testrunner_tmpdir_reporting.fKWfw8xoHo49/mvce.rs:24:31
   |
24 |     fn fold<B, F>(mut self, , self.; ) -> B
   |                               ^^^^ must be the first parameter of an associated function

error: expected argument name, found `;`
  --> /tmp/icemaker_global_tempdir.sa0LiyWL3vzD/rustc_testrunner_tmpdir_reporting.fKWfw8xoHo49/mvce.rs:24:36
   |
24 |     fn fold<B, F>(mut self, , self.; ) -> B
   |                                    ^ expected argument name

error: expected one of `)`, `,`, or `:`, found `.`
  --> /tmp/icemaker_global_tempdir.sa0LiyWL3vzD/rustc_testrunner_tmpdir_reporting.fKWfw8xoHo49/mvce.rs:24:35
   |
24 |     fn fold<B, F>(mut self, , self.; ) -> B
   |                                   ^
   |                                   |
   |                                   expected one of `)`, `,`, or `:`
   |                                   help: missing `,`

error[E0415]: identifier `self` is bound more than once in this parameter list
  --> /tmp/icemaker_global_tempdir.sa0LiyWL3vzD/rustc_testrunner_tmpdir_reporting.fKWfw8xoHo49/mvce.rs:24:31
   |
24 |     fn fold<B, F>(mut self, , self.; ) -> B
   |                               ^^^^ used as parameter more than once

error[E0425]: cannot find value `accum` in this scope
  --> /tmp/icemaker_global_tempdir.sa0LiyWL3vzD/rustc_testrunner_tmpdir_reporting.fKWfw8xoHo49/mvce.rs:29:9
   |
29 |         accum
   |         ^^^^^ not found in this scope

warning: anonymous parameters are deprecated and will be removed in the next edition
  --> /tmp/icemaker_global_tempdir.sa0LiyWL3vzD/rustc_testrunner_tmpdir_reporting.fKWfw8xoHo49/mvce.rs:24:27
   |
24 |     fn fold<B, F>(mut self, , self.; ) -> B
   |                           ^ help: try naming the parameter or explicitly ignoring it: `_: ,`
   |
   = warning: this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2018!
   = note: for more information, see issue #41686 <https://github.com/rust-lang/rust/issues/41686>
   = note: `#[warn(anonymous_parameters)]` on by default

error[E0601]: `main` function not found in crate `mvce`
  --> /tmp/icemaker_global_tempdir.sa0LiyWL3vzD/rustc_testrunner_tmpdir_reporting.fKWfw8xoHo49/mvce.rs:31:2
   |
31 | }
   |  ^ consider adding a `main` function to `/tmp/icemaker_global_tempdir.sa0LiyWL3vzD/rustc_testrunner_tmpdir_reporting.fKWfw8xoHo49/mvce.rs`

error[E0277]: expected a `FnMut((), T)` closure, found `()`
  --> /tmp/icemaker_global_tempdir.sa0LiyWL3vzD/rustc_testrunner_tmpdir_reporting.fKWfw8xoHo49/mvce.rs:18:25
   |
18 |         fn call<T>() -> impl FnMut((), T) {
   |                         ^^^^^^^^^^^^^^^^^ expected an `FnMut((), T)` closure, found `()`
   |
   = help: the trait `FnMut((), T)` is not implemented for `()`

error[E0277]: the size for values of type `Self` cannot be known at compilation time
  --> /tmp/icemaker_global_tempdir.sa0LiyWL3vzD/rustc_testrunner_tmpdir_reporting.fKWfw8xoHo49/mvce.rs:14:20
   |
14 |     fn for_each<F>(self, mut f: F)
   |                    ^^^^ doesn't have a size known at compile-time
   |
   = help: unsized fn params are gated as an unstable feature
help: consider further restricting `Self`
   |
14 |     fn for_each<F>(self, mut f: F) where Self: Sized
   |                                    +++++++++++++++++
help: function arguments must have a statically known size, borrowed types always have a known size
   |
14 |     fn for_each<F>(&self, mut f: F)
   |                    +

error[E0061]: this function takes 0 arguments but 1 argument was supplied
  --> /tmp/icemaker_global_tempdir.sa0LiyWL3vzD/rustc_testrunner_tmpdir_reporting.fKWfw8xoHo49/mvce.rs:22:23
   |
22 |         self.fold((), call(f));
   |                       ^^^^ - unexpected argument of type `F`
   |
note: function defined here
  --> /tmp/icemaker_global_tempdir.sa0LiyWL3vzD/rustc_testrunner_tmpdir_reporting.fKWfw8xoHo49/mvce.rs:18:12
   |
18 |         fn call<T>() -> impl FnMut((), T) {
   |            ^^^^
help: remove the extra argument
   |
22 -         self.fold((), call(f));
22 +         self.fold((), call());
   |


thread 'rustc' panicked at compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs:2611:34:
index out of bounds: the len is 2 but the index is 2
stack backtrace:
   0:     0x7d8412cd47ca - <std::sys::backtrace::BacktraceLock::print::DisplayBacktrace as core::fmt::Display>::fmt::hadf8e2df861efce2
   1:     0x7d84134135e6 - core::fmt::write::h7a55e8c402e63d48
   2:     0x7d84143127d1 - std::io::Write::write_fmt::h3751c3424aa517d0
   3:     0x7d8412cd4622 - std::sys::backtrace::BacktraceLock::print::hda228978e42208f7
   4:     0x7d8412cd6bc7 - std::panicking::default_hook::{{closure}}::hd850557dd952fc8c
   5:     0x7d8412cd69b0 - std::panicking::default_hook::hb302f704a65842a5
   6:     0x7d8411e52928 - std[3100b7b695602b1a]::panicking::update_hook::<alloc[5f744d0bf49b6096]::boxed::Box<rustc_driver_impl[736abb8093ca35b1]::install_ice_hook::{closure#1}>>::{closure#0}
   7:     0x7d8412cd7413 - std::panicking::rust_panic_with_hook::ha601460835d6d839
   8:     0x7d8412cd710a - std::panicking::begin_panic_handler::{{closure}}::h81a3663d6cb5d12c
   9:     0x7d8412cd4c99 - std::sys::backtrace::__rust_end_short_backtrace::h4ff0aa96a7064803
  10:     0x7d8412cd6dcd - rust_begin_unwind
  11:     0x7d840f99e9e0 - core::panicking::panic_fmt::h6dfbe1ba47369a2f
  12:     0x7d8411480519 - core::panicking::panic_bounds_check::hef42013b2602f98b
  13:     0x7d8412178f9b - <rustc_hir_typeck[87f76b69f31a82ca]::fn_ctxt::FnCtxt>::label_generic_mismatches
  14:     0x7d8412172047 - <rustc_hir_typeck[87f76b69f31a82ca]::fn_ctxt::FnCtxt>::report_arg_errors
  15:     0x7d8413554017 - <rustc_hir_typeck[87f76b69f31a82ca]::fn_ctxt::FnCtxt>::check_argument_types
  16:     0x7d8414109c40 - <rustc_hir_typeck[87f76b69f31a82ca]::fn_ctxt::FnCtxt>::check_expr_with_expectation_and_args
  17:     0x7d84140f33f9 - <rustc_hir_typeck[87f76b69f31a82ca]::fn_ctxt::FnCtxt>::check_expr_block
  18:     0x7d84140fad1f - <rustc_hir_typeck[87f76b69f31a82ca]::fn_ctxt::FnCtxt>::check_expr_with_expectation_and_args
  19:     0x7d8413796f14 - rustc_hir_typeck[87f76b69f31a82ca]::check::check_fn
  20:     0x7d84137a221b - rustc_hir_typeck[87f76b69f31a82ca]::typeck_with_fallback::{closure#0}
  21:     0x7d84137a040c - rustc_query_impl[ac2bab59562664c7]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[ac2bab59562664c7]::query_impl::typeck::dynamic_query::{closure#2}::{closure#0}, rustc_middle[ccb2eb78bedf1942]::query::erase::Erased<[u8; 8usize]>>
  22:     0x7d84137c580e - rustc_query_system[9e3640c263c85fee]::query::plumbing::try_execute_query::<rustc_query_impl[ac2bab59562664c7]::DynamicConfig<rustc_data_structures[52740ce709e2e05d]::vec_cache::VecCache<rustc_span[8e152531e0bcd89e]::def_id::LocalDefId, rustc_middle[ccb2eb78bedf1942]::query::erase::Erased<[u8; 8usize]>, rustc_query_system[9e3640c263c85fee]::dep_graph::graph::DepNodeIndex>, false, false, false>, rustc_query_impl[ac2bab59562664c7]::plumbing::QueryCtxt, false>
  23:     0x7d84137c49e3 - rustc_query_impl[ac2bab59562664c7]::query_impl::typeck::get_query_non_incr::__rust_end_short_backtrace
  24:     0x7d84137c469d - <rustc_middle[ccb2eb78bedf1942]::hir::map::Map>::par_body_owners::<rustc_hir_analysis[48e47dfc185f7fea]::check_crate::{closure#4}>::{closure#0}
  25:     0x7d84137c2751 - rustc_hir_analysis[48e47dfc185f7fea]::check_crate
  26:     0x7d84138950a8 - rustc_interface[21548936fe144d05]::passes::run_required_analyses
  27:     0x7d841431665e - rustc_interface[21548936fe144d05]::passes::analysis
  28:     0x7d841431662f - rustc_query_impl[ac2bab59562664c7]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[ac2bab59562664c7]::query_impl::analysis::dynamic_query::{closure#2}::{closure#0}, rustc_middle[ccb2eb78bedf1942]::query::erase::Erased<[u8; 0usize]>>
  29:     0x7d841437f7d5 - rustc_query_system[9e3640c263c85fee]::query::plumbing::try_execute_query::<rustc_query_impl[ac2bab59562664c7]::DynamicConfig<rustc_query_system[9e3640c263c85fee]::query::caches::SingleCache<rustc_middle[ccb2eb78bedf1942]::query::erase::Erased<[u8; 0usize]>>, false, false, false>, rustc_query_impl[ac2bab59562664c7]::plumbing::QueryCtxt, false>
  30:     0x7d841437f50e - rustc_query_impl[ac2bab59562664c7]::query_impl::analysis::get_query_non_incr::__rust_end_short_backtrace
  31:     0x7d841433ecde - rustc_interface[21548936fe144d05]::passes::create_and_enter_global_ctxt::<core[5eafa33fb0699880]::option::Option<rustc_interface[21548936fe144d05]::queries::Linker>, rustc_driver_impl[736abb8093ca35b1]::run_compiler::{closure#0}::{closure#2}>::{closure#2}::{closure#0}
  32:     0x7d8414358524 - rustc_interface[21548936fe144d05]::interface::run_compiler::<(), rustc_driver_impl[736abb8093ca35b1]::run_compiler::{closure#0}>::{closure#1}
  33:     0x7d8414206187 - std[3100b7b695602b1a]::sys::backtrace::__rust_begin_short_backtrace::<rustc_interface[21548936fe144d05]::util::run_in_thread_with_globals<rustc_interface[21548936fe144d05]::util::run_in_thread_pool_with_globals<rustc_interface[21548936fe144d05]::interface::run_compiler<(), rustc_driver_impl[736abb8093ca35b1]::run_compiler::{closure#0}>::{closure#1}, ()>::{closure#0}, ()>::{closure#0}::{closure#0}, ()>
  34:     0x7d8414206624 - <<std[3100b7b695602b1a]::thread::Builder>::spawn_unchecked_<rustc_interface[21548936fe144d05]::util::run_in_thread_with_globals<rustc_interface[21548936fe144d05]::util::run_in_thread_pool_with_globals<rustc_interface[21548936fe144d05]::interface::run_compiler<(), rustc_driver_impl[736abb8093ca35b1]::run_compiler::{closure#0}>::{closure#1}, ()>::{closure#0}, ()>::{closure#0}::{closure#0}, ()>::{closure#1} as core[5eafa33fb0699880]::ops::function::FnOnce<()>>::call_once::{shim:vtable#0}
  35:     0x7d8414207c01 - std::sys::pal::unix::thread::Thread::new::thread_start::he5122ef3561b8e92
  36:     0x7d840e6a339d - <unknown>
  37:     0x7d840e72849c - <unknown>
  38:                0x0 - <unknown>

error: the compiler unexpectedly panicked. this is a bug.

note: we would appreciate a bug report: https://github.com/rust-lang/rust/issues/new?labels=C-bug%2C+I-ICE%2C+T-compiler&template=ice.md

note: please make sure that you have updated to the latest nightly

note: rustc 1.86.0-nightly (dcfa38fe2 2025-01-05) running on x86_64-unknown-linux-gnu

query stack during panic:
#0 [typeck] type-checking `LendingIteratorExt::for_each`
#1 [analysis] running analysis passes on this crate
end of query stack
error[E0277]: the size for values of type `Self` cannot be known at compilation time
  --> /tmp/icemaker_global_tempdir.sa0LiyWL3vzD/rustc_testrunner_tmpdir_reporting.fKWfw8xoHo49/mvce.rs:24:23
   |
24 |     fn fold<B, F>(mut self, , self.; ) -> B
   |                       ^^^^ doesn't have a size known at compile-time
   |
   = help: unsized fn params are gated as an unstable feature
help: consider further restricting `Self`
   |
24 |     fn fold<B, F>(mut self, , self.; ) -> B where Self: Sized
   |                                             +++++++++++++++++
help: function arguments must have a statically known size, borrowed types always have a known size
   |
24 |     fn fold<B, F>(mut &self, , self.; ) -> B
   |                       +

error: aborting due to 11 previous errors; 1 warning emitted

Some errors have detailed explanations: E0061, E0277, E0415, E0425, E0601.
For more information about an error, try `rustc --explain E0061`.

```

</p>
</details>

<!--
query stack:
#0 [typeck] type-checking `LendingIteratorExt::for_each`
#1 [analysis] running analysis passes on this crate

-->