Skip to content

Compiler indefinitely takes resources when HRTB is used in conjunction with lifetime in GAT #121821

Open
@dodomorandi

Description

@dodomorandi

I tried this code:

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

    fn run(&mut self) -> Self::Item<'_>;
}

#[derive(Debug)]
pub struct S<F>(F);

impl<F> A for S<F>
where
    for<'a> F: FnMut(&Self::Item<'a>),
{
    type Item<'a> = i32
    where
        Self: 'a;

    fn run(&mut self) -> Self::Item<'_> {
        todo!()
    }
}

I expected to see this happen: the code should compile or return a compilation error in a short time.

Instead, this happened: the compiler start working and taking resources, apparently indefinitely.

Notice that removing the bound for F stops the issue to occur.


EDIT: the code triggers infinite recursion, the bound should be FnMut(&I::Item<'a>). As suggested by others, the issue of infinite recursion is managed by the next solver (using -Znext-solver), and thanks to that is also easy to spot the problem in the code.

Meta

rustc --version --verbose:
Stable:

rustc 1.76.0 (07dca489a 2024-02-04)
binary: rustc
commit-hash: 07dca489ac2d933c78d3c5158e3f43beefeb02ce
commit-date: 2024-02-04
host: x86_64-unknown-linux-gnu
release: 1.76.0
LLVM version: 17.0.6

Nightly:

rustc 1.78.0-nightly (c475e2303 2024-02-28)
binary: rustc
commit-hash: c475e2303b551d726307c646181e0677af1e0069
commit-date: 2024-02-28
host: x86_64-unknown-linux-gnu
release: 1.78.0-nightly
LLVM version: 18.1.0

The issue occurs both on stable and nightly.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-GATsArea: Generic associated types (GATs)A-higher-rankedArea: Higher-ranked things (e.g., lifetimes, types, trait bounds aka HRTBs)A-lifetimesArea: Lifetimes / regionsA-trait-systemArea: Trait systemC-bugCategory: This is a bug.I-compilememIssue: Problems and improvements with respect to memory usage during compilation.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