Skip to content

Recursive generics #118449

Open
Open
@notdanilo

Description

@notdanilo

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-type-systemArea: Type systemC-bugCategory: This is a bug.I-hangIssue: The compiler never terminates, due to infinite loops, deadlock, livelock, etc.P-lowLow priorityS-has-mcveStatus: A Minimal Complete and Verifiable Example has been found for this issueT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.T-typesRelevant to the types team, which will review and decide on the PR/issue.regression-from-stable-to-stablePerformance or correctness regression from one stable version to another.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions