Compiler hangs on some types threading lifetimes through a carrier trait #102966
Open
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.
- Remove the "carrier trait" NodeGenerics, replace it with
T: 'a
and useT
instead ofG::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 ofKind
. It makes everything a lot neater.) - Changing
RGen<T>
to justRGen
and its implementation ofNodeGenerics
to have a constanttype R = ()
. - Don't use lifetimes at all, put
'static
bounds in everywhere instead, i.e. make NodeGenerics taketype R: 'static
.
I don't know why it's hanging on this, but rustc should never hang, so there you go. Bug report filed.