Closed
Description
The compiler fails to exploit a 'static
bound, requiring a verbose workaround via supertrait.
I tried the following code (playground).
trait Foo {
type Assoc;
}
trait Bar<'a> {
type Assoc: 'static;
}
struct S<T>(T);
impl<'a, T> Foo for S<T>
where
T: Bar<'a>,
{
type Assoc = <T as Bar<'a>>::Assoc;
}
I expected it to compile. Instead I was greeted by E0207, lamenting that 'a
is unconstrained. However, the following code works without issue (playground).
trait Foo {
type Assoc;
}
trait Bar0 {
type Assoc;
}
trait Bar<'a>: Bar0 {}
struct S<T>(T);
impl<'a, T> Foo for S<T>
where
T: Bar<'a>,
{
type Assoc = <T as Bar0>::Assoc;
}
Note that introducing the supertrait allows referring to Bar0::Assoc
without mentioning the lifetime 'a
. However, unless I'm missing any subtleties, the compiler should be able to deduce (due to the bound on Bar::Assoc
) that <T as Bar<'a>>::Assoc: 'static
and that therefore it does not matter that the lifetime is unconstrained.
Meta
Affects all rustc versions, including 1.69.0-nightly (5b8f284 2023-02-12).