rust-lang / rust-lang/rust

Recursive generics

Open
#118,449 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-type-system C-bug I-hang P-low regression-from-stable-to-stable S-has-mcve T-compiler T-types
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

I tried this code alone:

trait HasParent {
    type Parent: HasParent;
}

struct Visitor<'a, Type>
where Type: HasParent
{
    parent: &'a Visitor<'a, Type::Parent>,
    current: &'a Type
}

I expected to see this happen: Being able to compile OR error message.

I also expect the following evaluation to be possible, breaking the recursion, because no further monomorphization is necessary:

struct Library;
struct Module;

impl HasParent for Module {
    type Parent = Library;
}

impl HasParent for Library {
    type Parent = ();
}

impl HasParent for () {
    type Parent = ();
}

which would produce


struct Visitor<'a, ()> {
    parent: &'a Visitor<'a, ()>,
    current: &'a ()
}

struct Visitor<'a, Library> {
    parent: &'a Visitor<'a, ()>,
    current: &'a Library
}

struct Visitor<'a, Module> {
    parent: &'a Visitor<'a, Library>,
    current: &'a Module
}

Instead, this happened: The compiler freezes.

Meta

rustc --version --verbose:

rustc 1.74.0 (79e9716c9 2023-11-13)
binary: rustc
commit-hash: 79e9716c980570bfd1f666e3b16ac583f0168962
commit-date: 2023-11-13
host: x86_64-pc-windows-msvc
release: 1.74.0
LLVM version: 17.0.4
Backtrace

No backtrace available

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 compiling the standalone recursive-generics reproducer with the reported rustc 1.74.0 version and compare whether current versions still freeze. Investigate how the compiler handles the recursive associated-type instantiations in the example. Done means the compiler terminates with a diagnostic or successfully compiles the finite Module, Library, and unit chain.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.