rust-lang / rust

Empowering everyone to build reliable and efficient software.
https://www.rust-lang.org
Other
97.57k stars 12.62k forks source link

Stall from overflow caused by `for<'a> F: FnOnce(Self::Gat<'a>)` #87758

Open hlb8122 opened 3 years ago

hlb8122 commented 3 years ago

I tried this code:

#![feature(generic_associated_types)]

pub struct Structure<F> {
    f: F,
}

pub trait Trait {
    type Gat<'a>;
}

impl<F> Trait for Structure<F>
where
    for<'a> F: FnOnce(Self::Gat<'a>),
{
    type Gat<'a> = String;
}

https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=fbd726c1f1bd90e514358a91016581ec

Given that https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=f889ed7bfeffb6a6f3179bf6d94f438c causes an overflow evaluating the requirement Structure<F>: Trait I would expect the problem code to give a similar message.

Instead, rustc stalls indefinitely.

Meta

rustc --version --verbose:

rustc 1.56.0-nightly (a6ece5615 2021-08-03)
Backtrace

No backtrace produced due to stall.

jackh726 commented 3 years ago

Looking at logs, this looks really similar to #87755 (or more so impl-wf-cycle-1.rs)

We try to project <F as std::ops::FnOnce<(<Structure<F> as Trait>::Gat<'a>,)>> That requires us to prove that Structure<F>: Trait So we match against <Structure<_#0t> as Trait> So we have to prove that <_#0t as std::ops::FnOnce<(<Structure<F> as Trait>::Gat<'a>,)>> And we end up in a cycle where match_impl continues to use a new type var

hlb8122 commented 3 years ago

@jackh726 You can also stall it like this https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=e3ff465815d5621c29ec242951ade429

Un-GATed, this would also cause an overflow https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=d1ce6b3a5e256f61f74476662a88bc8c

jackh726 commented 2 years ago

GATs issue triage: not blocking. Not strictly a GATs issue; you can run into similar problems with a where clause on the trait itself (as showed in the previous comment). Unfortunate, but a larger class of issues.