Hi, I'm working in a big codebase and ran into an issue recently that I would like to understand better.
I have reproduced a minimal example of the issue:
trait Ty {}
impl Ty for u32 {}
trait IntoFunc<A> {}
impl<F, A> IntoFunc<A> for F
where
F: Fn(A),
A: Ty,
{
}
fn func<A>(_: impl IntoFunc<A>) {}
trait T<'a> {
type Type;
}
struct S {}
impl T<'_> for S {
type Type = u32;
}
fn test(_: <S as T>::Type) {}
fn main() {
func(test);
}
The problem is that <S as T>::Type
is not equal to u32
in this example. From my understanding it should be the case because of the definition:
impl T<'_> for S {
type Type = u32;
}
If I define the test function as fn test(_: <S as T<'static>>::Type) {}
everything compiles fine, but why is the 'static
required here?
Shouldn't <S as T>::Type
just be a type alias for u32
in this case?
Additionally, if the structure S
takes a lifetime it also needs to be explicitly written down as 'static
for the code to compile e.g. <S<'static> as T<'static>>::Type
.
I can't easily work around this because this part of the code <S as T>::Type
is generated in a macro and I can't add 'static
lifetimes to it, so I'm trying to understand why the compiler can't figure out that it just resolves to u32
.