Repository: rust
Issue: ICE error: internal compiler error: no region-bound-pairs for HirId when compiling benchmark
Labels: I-ICE,T-compiler,C-bug

<!--
Thank you for finding an Internal Compiler Error! 🧊  If possible, try to provide
a minimal verifiable example. You can read "Rust Bug Minimization Patterns" for
how to create smaller examples.

http://blog.pnkfx.org/blog/2019/11/18/rust-bug-minimization-patterns/

-->

I ran into this when trying to run `./x.py bench library/core` while working on iterators in `core`. Thankfully it was straight-forward to extract the code into an external crate.

### Code

```Rust
/// lib.rs
#![feature(trusted_random_access)]
#![feature(min_specialization)]
#![feature(test)]

extern crate test;

use std::iter::TrustedRandomAccessNoCoerce;

trait IterExtension: Iterator {
    #[inline]
    fn _loop_opt(self) -> ForLoopDesugar<Self>
        where
            Self: Sized,
    {
        ForLoopDesugar::new(self)
    }
}

impl<I: Iterator> IterExtension for I {}

#[derive(Debug)]
pub struct ForLoopDesugar<I> {
    iter: I,
    idx: usize
}

impl<I: Iterator> ForLoopDesugar<I> {
    #[inline]
    pub fn new<INTO>(into_iter: INTO) -> Self
        where INTO: IntoIterator<Item = I::Item, IntoIter = I>
    {
        ForLoopDesugar { iter: into_iter.into_iter(), idx: 0 }
    }
}

impl<I: Iterator> Iterator for ForLoopDesugar<I> {
    type Item = I::Item;

    #[inline]
    default fn next(&mut self) -> Option<Self::Item> {
        self.iter.next()
    }
}


impl<I: TrustedRandomAccessNoCoerce + Iterator> Iterator for ForLoopDesugar<I> {

    #[inline]
    fn next(&mut self) -> Option<Self::Item> {
        let idx = self.idx;
        if idx < self.iter.size() {
            self.idx = idx + 1;
            //self.idx = unsafe { idx.unchecked_add(1) };
            Some(unsafe { self.iter.__iterator_get_unchecked(idx) })
        } else {
            None
        }
    }
}

fn main() {
    println!("Hello, world!");
}

#[cfg(test)]
mod b {
    use test::{Bencher, black_box};
    use crate::IterExtension;

    #[bench]
    fn bench_desugar(b: &mut Bencher) {
        let vec1: Vec<_> = (0usize..100000).collect();
        let vec2 = black_box(vec1.clone());
        b.iter(|| {
            let iter = vec1.iter()
                .copied()
                .enumerate()
                .map(|(idx, e)| idx.wrapping_add(e))
                .zip(vec2.iter().copied())
                .map(|(a, b)| a.wrapping_add(b))
                .fuse();
            let mut acc = 0;
            for i in iter._loop_opt() {
                acc += i;
            }
            acc
        })
    }
}

```


### Meta
<!--
If you're using the stable version of the compiler, you should also check if the
bug also exists in the beta or nightly versions.
-->

`rustc --version --verbose`:
```
rustc 1.59.0-nightly (c14569225 2022-01-01)
binary: rustc
commit-hash: c145692254e86974941f2c92c643a23df0f13e82
commit-date: 2022-01-01
host: x86_64-unknown-linux-gnu
release: 1.59.0-nightly
LLVM version: 13.0.0
```

### Error output

```
$ cargo +nightly bench
   Compiling ice_repro v0.1.0 (/tmp/ice_repro)
error: internal compiler error: no region-bound-pairs for HirId { owner: DefId(0:0 ~ ice_repro[7cdb]), local_id: 0 }
  |
  = note: delayed at compiler/rustc_infer/src/infer/outlives/obligations.rs:189:31

thread 'rustc' panicked at 'no errors encountered even though `delay_span_bug` issued', compiler/rustc_errors/src/lib.rs:1188:13
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

error: internal compiler error: unexpected panic

note: 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.59.0-nightly (c14569225 2022-01-01) running on x86_64-unknown-linux-gnu

note: compiler flags: -C opt-level=3 -C embed-bitcode=no -C target-cpu=native

note: some of the compiler flags provided by cargo are hidden

query stack during panic:
end of query stack
error: could not compile `ice_repro`
```

<!--
Include a backtrace in the code block by setting `RUST_BACKTRACE=1` in your
environment. E.g. `RUST_BACKTRACE=1 cargo build`.
-->
<details><summary><strong>Backtrace</strong></summary>
<p>

```
thread 'rustc' panicked at 'no errors encountered even though `delay_span_bug` issued', compiler/rustc_errors/src/lib.rs:1188:13
stack backtrace:
   0: rust_begin_unwind
             at /rustc/c145692254e86974941f2c92c643a23df0f13e82/library/std/src/panicking.rs:498:5
   1: core::panicking::panic_fmt
             at /rustc/c145692254e86974941f2c92c643a23df0f13e82/library/core/src/panicking.rs:107:14
   2: core::panicking::panic_display::<&str>
   3: <rustc_errors::HandlerInner>::flush_delayed
   4: <rustc_errors::HandlerInner as core::ops::drop::Drop>::drop
   5: core::ptr::drop_in_place::<rustc_session::parse::ParseSess>
   6: <alloc::rc::Rc<rustc_session::session::Session> as core::ops::drop::Drop>::drop
   7: core::ptr::drop_in_place::<rustc_interface::interface::Compiler>
   8: rustc_span::with_source_map::<core::result::Result<(), rustc_errors::ErrorReported>, rustc_interface::interface::create_compiler_and_run<core::result::Result<(), rustc_errors::ErrorReported>, rustc_driver::run_compiler::{closure#1}>::{closure#1}>
   9: rustc_interface::interface::create_compiler_and_run::<core::result::Result<(), rustc_errors::ErrorReported>, rustc_driver::run_compiler::{closure#1}>
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
```

</p>
</details>