Skip to content

Compiler hangs on some types threading lifetimes through a carrier trait #102966

Open
@cormacrelf

Description

The following code hangs rustc stable 1.64, beta and nightly. It sits on 100% CPU doing rustc_middle things inside a rustc_typeck::outlives::implicit_infer::infer_predicates > insert_outlives_predicate operation. (Edit: by "rustc_middle things" I mean <rustc_middle::ty::TyS as core::cmp::Ord>::cmp.) I've let it run for at least half an hour to no avail.

struct Node<'a, G: NodeGenerics<'a>> {
    kind: Kind<'a, G>,
}

enum Kind<'a, G: NodeGenerics<'a>> {
    Empty,
    Var(Var<'a, G::R>),
}

trait NodeGenerics<'a> {
    type R: 'a;
}

struct RGen<T>(std::marker::PhantomData<T>);
impl<'a, T: 'a> NodeGenerics<'a> for RGen<T> {
    type R = T;
}

struct Var<'a, R: 'a> {
    node: Box<Node<'a, RGen<R>>>,
    _phantom: std::marker::PhantomData<R>,
}

You can change the code in a couple of small ways such that it won't hang.

  1. Remove the "carrier trait" NodeGenerics, replace it with T: 'a and use T instead of G::R. You would think that adding a carrier trait with the same bounds would not really change anything. But it does. (In my code, NodeGenerics has a bunch of different types attached to it, which are used in many different arms of Kind. It makes everything a lot neater.)
  2. Changing RGen<T> to just RGen and its implementation of NodeGenerics to have a constant type R = ().
  3. Don't use lifetimes at all, put 'static bounds in everywhere instead, i.e. make NodeGenerics take type R: 'static.

I don't know why it's hanging on this, but rustc should never hang, so there you go. Bug report filed.

Metadata

Assignees

No one assigned

    Labels

    A-implied-boundsArea: Implied bounds / inferred outlives-boundsC-bugCategory: This is a bug.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.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions