rust-lang / rust-lang/rust

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

Open
#121,821 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-GATs A-higher-ranked A-lifetimes A-trait-system C-bug fixed-by-next-solver I-compilemem I-hang T-compiler
Dominant language
Rust
Stars
119k
Forks
16.2k
PR merge metrics
PR metrics pending

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the provided HRTB and GAT example with the listed stable and nightly rustc versions, then compare behavior with -Znext-solver. Investigate the compiler's trait-solver handling of the recursive bound; done means compilation terminates with an appropriate diagnostic or succeeds without indefinitely consuming resources, with a regression test for the reproducer.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.