Open udoprog opened 2 years ago
Here is a reproduction without GAT:
trait FooBad: for<'a> TheItem<'a> {
fn items<F>(walker: F)
where
F: FnOnce(<Self as TheItem<'_>>::Item);
}
trait TheItem<'a> {
type Item;
}
struct Struct<T>(T);
impl<'a, T> TheItem<'a> for Struct<T>
where
Self: 'a,
{
type Item = &'a mut T;
}
impl<T> FooBad for Struct<T> {
}
A workaround from #96831 :
impl<T> Trait for Struct<T> {
type Gat<'this> = &'this mut T where Self: 'this;
fn function<F>(self, _: F)
where
- F: FnOnce(Self::Gat<'_>),
+ F: FnOnce(&mut T),
{
}
}
@rustbot label F-generic_associated_types
@aliemjay that is... actually a workable solution (all though patch looks backwards ;)! Thanks!
So sadly while the proposed solution gets past the initial issue, there are more similar downstream issues that happen like this one:
#![feature(generic_associated_types)]
use std::marker;
trait Trait {
type Gat<'this>
where
Self: 'this;
fn function<F>(self, _: F)
where
F: FnOnce(Self::Gat<'_>);
fn function2(self);
}
struct Struct<T>(marker::PhantomData<T>);
impl<T> Trait for Struct<T> {
type Gat<'this> = &'this mut T where Self: 'this;
fn function<F>(self, callback: F)
where
F: FnOnce(&mut T),
{
}
fn function2(self) {
self.function(|value| {})
}
}
error[[E0311]](https://doc.rust-lang.org/nightly/error-index.html#E0311): the parameter type `T` may not live long enough
[--> src/lib.rs:29:14
](https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=f4b62bb6c9773f5807cd8fe25ae8266a#) |
29 | self.function(|value| {})
| ^^^^^^^^ ...so that the type `Struct<T>` will meet its required lifetime bounds
|
help: consider adding an explicit lifetime bound...
|
19 | impl<T: 'a> Trait for Struct<T> {
| ++++
I tried this code:
Playground
I expected to see this happen: Since the GAT has the
Self: 'this
bound I'd expectT
as being part ofStruct<T>
which isSelf
to outlive'this
and compile.Instead, this happens and we get a (confusing) lifetime error which proposes to add a bound for a type
'a
which isn't specified in the code:I'm assuming this is a name for the HRTB
'_
but I'm not sure. However, this bound should not be necessary sinceT
is part of the impl associated withStruct<T>
. This HRTB working w/o complaints making me unsure what'a
is referring to:Related issues
At first seemed to be related to #95331 but I was unable to reproduce without GATs. I.e. this compiles:
Playground
Meta
rustc --version --verbose
: