Open athre0z opened 4 years ago
I don't think this is a bug. The following compiles:
#![feature(generic_associated_types)]
trait Foo {
type In<'a>;
}
struct _Simple<'a>(std::marker::PhantomData<&'a u32>);
fn _somefn_simple(_f: for<'a> fn(_Simple<'a>) -> _Simple<'a>) {
// compiles.
}
fn _somefn_gat<'a, T: Foo>(_f: for<'b> fn(T::In<'b>) -> T::In<'a>) {
// errors.
}
fn main() {}
@b-naber With your modifications 'a
and 'b
are possibly unrelated without extra outlives bounds. I think a better way to do this is?:
fn somefn_gat<'a, T: Foo>(f: fn(T::In<'a>) -> T::In<'a>) {
}
Unfortunately, this code isn't equivalent. With your proposed variant, 'a
needs to be defined by the call site. somefn_gat
can't, for example, pass a reference to a local variable to f
.
Comparing two playgrounds (without GAT, to not run into the issue described in the original post of this issue):
https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=18e76ca1267c0d8f9ee00b1c30475d4c vs https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=d3a835097a1f867242f7a3a75352d6f4
I don't really understand how you want to call somefn_gat
in your original example. How is the compiler supposed to infer T
? You have to somewhere supply an instance that implements Foo
or let somefn_gat
be a trait method of Foo
. Basically in your current example the function parameter has a type something like for<'a, T : Foo> fn(T::In<'a>) -> T::In<'a>
, which as far as I know is not possible because the compiler uses monomorphization.
This isn't specific to GATs.
trait Foo<'a> {
type In;
}
struct Simple<'a>(std::marker::PhantomData<&'a u32>);
fn somefn_simple(f: for<'a> fn(Simple<'a>) -> Simple<'a>) {
// compiles.
}
fn somefn_gat<T: for<'r> Foo<'r>>(f: for<'a> fn(<T as Foo<'a>>::In) -> <T as Foo<'a>>::In) {
// errors.
}
fn main() {}
This is intended as <T as Foo<'a>>::In
might be independent of 'a
and so 'a
cannot be inferred from the input type of the function. In this exact case this is fine because the output will also be independent of 'a
in that case, but for something like for<'a> fn(<T as Foo<'a>>::In) -> &'a i32)
would not be.
I agree this is not a bug. The error message could use work. The key point is that the lifetime 'a
is not constrained by the function parameter types -- it is referenced, but not in the right way.
I would expect
somefn_gat
to build just likesomefn_simple
does, however it currently results in this error:rustc --version --verbose
:Click here for playground