Skip to content

Passing a lifetime GAT to a closure can cause the compiler to run indefinitely #108826

Description

@jihiggins

Code

I tried to pair it down more, but so far this is the smallest version I could get to cause the issue:

pub trait LendingIterator {
    type Item<'a>
    where
        Self: 'a;

    fn next<'a>(&'a mut self) -> Self::Item<'a>
    where
        Self: 'a;
        
    
    fn filter<P>(self, predicate: P) -> Filter<Self, P>
    where
        Self: Sized,
        for<'a> P: FnMut(&Self::Item<'a>) -> bool,
    {
        Filter {
            stream: self,
            predicate,
        }
    }
}

#[derive(Clone, Debug)]
pub struct Filter<S, P> {
    stream: S,
    predicate: P,
}

impl<S, P> LendingIterator for Filter<S, P>
where
    S: LendingIterator + Unpin,
    for<'a> P: FnMut(&Self::Item<'a>) -> bool,
{
    type Item<'a> = S::Item<'a> where S: 'a;

    fn next<'a>(&'a mut self) -> Self::Item<'a>
    where
        Self: 'a,
    {
        loop {
            let next = self.stream.next()?;
            if (self.predicate)(&next) {
                return Some(());
            }
        }
    }
}

https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=5042463788e49bf6964dfa80dcb5b9bb

Meta

rustc --version --verbose:

rustc 1.70.0-nightly (7820b62d2 2023-03-05)
binary: rustc
commit-hash: 7820b62d20bc548096d4632a3487987308cb4b5d
commit-date: 2023-03-05
host: x86_64-pc-windows-msvc
release: 1.70.0-nightly
LLVM version: 15.0.7

and

rustc 1.67.1 (d5a82bbd2 2023-02-07)
binary: rustc
commit-hash: d5a82bbd26e1ad8b7401f6a718a9c57c96905483
commit-date: 2023-02-07
host: x86_64-pc-windows-gnu
release: 1.67.1
LLVM version: 15.0.6

Error output

There isn't any error output, since the compiler runs indefinitely when encountering this issue.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-GATsArea: Generic associated types (GATs)C-bugCategory: This is a bug.I-hangIssue: The compiler never terminates, due to infinite loops, deadlock, livelock, etc.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.fixed-by-next-solverFixed by the next-generation trait solver, `-Znext-solver`.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions