Compiler indefinitely takes resources when HRTB is used in conjunction with lifetime in GAT #121821
Open
Description
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
Assignees
Labels
Area: Generic associated types (GATs)Area: Higher-ranked things (e.g., lifetimes, types, trait bounds aka HRTBs)Area: Lifetimes / regionsArea: Trait systemCategory: This is a bug.Issue: Problems and improvements with respect to memory usage during compilation.Issue: The compiler never terminates, due to infinite loops, deadlock, livelock, etc.Relevant to the compiler team, which will review and decide on the PR/issue.Fixed by the next-generation trait solver, `-Znext-solver`.
Activity