Skip to content
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

ICE with core::iter::adapters::peekable + recursion + pattern matching on peek #131342

Open
Borgerr opened this issue Oct 7, 2024 · 5 comments
Open
Labels
A-traits Area: Trait system 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

@Borgerr
Copy link
Contributor

Borgerr commented Oct 7, 2024

I tried this code:

fn main() {
  let mut items = vec![1, 2, 3, 4, 5].into_iter();
  problem_thingy(&mut items);
}

fn problem_thingy(items: &mut impl Iterator<Item = u8>) {
  let mut peeker = items.peekable();
  match peeker.peek() {
    Some(_) => (),
    None => return (),
  }
  problem_thingy(&mut peeker);
}

I expected to see this happen: either continuously have the first item be "peeked" at, or return () on the final iteration after exhausting other peeks.

Instead, this happened: compiler panicked with an unbearably large error message. I believe this has to do with allocation and pointer safety with Peekable, but I could be way off.

Meta

rustc --version --verbose:

rustc 1.81.0 (eeb90cda1 2024-09-04)                                                  
binary: rustc                                                                        
commit-hash: eeb90cda1969383f56a2637cbd3037bdf598841c                                
commit-date: 2024-09-04                                                              
host: x86_64-unknown-linux-gnu                                                       
release: 1.81.0                                                                      
LLVM version: 18.1.7        

Backtrace and compiler output are unbearably large for this issue, so I've attached them below.

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

bare_build.txt
backtrace_build.txt

@Borgerr Borgerr added the C-bug Category: This is a bug. label Oct 7, 2024
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Oct 7, 2024
@Borgerr
Copy link
Contributor Author

Borgerr commented Oct 7, 2024

@rustbot label T-compiler A-allocators A-traits

@rustbot rustbot added A-allocators Area: Custom and system allocators A-traits Area: Trait system T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Oct 7, 2024
@Borgerr Borgerr changed the title core::iter::adapters::peekable + recursion + pattern matching on peek causes compiler panic COMPILER PANIC with core::iter::adapters::peekable + recursion + pattern matching on peek Oct 7, 2024
@coravacav
Copy link

fn main() {
    let mut items = vec![1, 2, 3, 4, 5].into_iter();
    problem_thingy(&mut items);
}

fn problem_thingy(items: &mut impl Iterator<Item = u8>) {
    let mut peeker = items.peekable();
    problem_thingy(&mut peeker);
}

This correctly doesn't panic with

error[E0275]: overflow evaluating the requirement `Peekable<&mut std::vec::IntoIter<u8>>: Iterator`

Looks like that operation in the middle makes the compiler somehow not catch it.

@saethlin saethlin changed the title COMPILER PANIC with core::iter::adapters::peekable + recursion + pattern matching on peek ICE with core::iter::adapters::peekable + recursion + pattern matching on peek Oct 7, 2024
@saethlin saethlin added I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ and removed A-allocators Area: Custom and system allocators needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Oct 7, 2024
@matthiaskrgr
Copy link
Member

This crashes with -Cdebuginfo=" or incr comp.
Probably related to #123953 / #122823

@cyrgani
Copy link
Contributor

cyrgani commented Oct 7, 2024

Reduced to not depend on Iterator or Peekable from std and on reduced local versions instead:

fn main() {
    problem_thingy(Once);
}

struct Once;

impl Iterator for Once {
    type Item = ();
}

fn problem_thingy(items: impl Iterator) {
    let peeker = items.peekable();
    problem_thingy(&peeker);
}

trait Iterator {
    type Item;

    fn peekable(self) -> Peekable<Self>
    where
        Self: Sized,
    {
        loop {}
    }
}

struct Peekable<I: Iterator> {
    _peeked: I::Item,
}

impl<I: Iterator> Iterator for Peekable<I> {
    type Item = I::Item;
}

impl<I: Iterator + ?Sized> Iterator for &I {
    type Item = I::Item;
}

This also ICEs with just rustc main.rs, though the error message is different.

It's possible to construct some other horrible error messages from this too, such as by replacing the last impl with:

impl<I: Iterator> Iterator for &I {
    type Item = ();
}

@RalfJung
Copy link
Member

FWIW #127731 makes the reduced example above not crash any more, for unknown reasons. The original example still crashes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-traits Area: Trait system 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
Development

No branches or pull requests

7 participants