HRTB impl selection does not cover type parameters not directly related to the trait. #30867
Closed
Description
The following doesn't compile, as it tries to assign U
a concrete &'x i32
instead of &'a i32
:
trait Unary<T> {}
impl<T, U, F: Fn(T) -> U> Unary<T> for F {}
fn unary<F: for<'a> Unary<&'a T>, T>() {}
fn test<F: for<'a> Fn(&'a i32) -> &'a i32>() {
unary::<F, i32>()
}
Removing the U
type parameter makes it work - U
is only used for the associated type Fn::Output
anyway:
#![feature(unboxed_closures)]
trait Unary<T> {}
impl<T, F: Fn<(T,)>> Unary<T> for F {}
fn unary<F: for<'a> Unary<&'a T>, T>() {}
fn test<F: for<'a> Fn(&'a i32) -> &'a i32>() {
unary::<F, i32>()
}
AFAICT, the selection results in for<'a, U> F: Fn<(&'a i32,), Output=U>
in the first case, and for<'a> F: Fn<(&'a i32,)>
in the second case, and we refuse to allow U
to be parameterized by 'a
in the former case, even though it's only used to satisfy the sugary form of the Fn
trait.
Metadata
Assignees
Labels
Area: Higher-ranked things (e.g., lifetimes, types, trait bounds aka HRTBs)Area: Trait systemArea: Type systemCategory: This is a bug.Call for participation: An issue has been fixed and does not reproduce, but no test has been added.Relevant to the compiler team, which will review and decide on the PR/issue.Relevant to the types team, which will review and decide on the PR/issue.