Change:
<!-- Thank you for filing a bug report! Please provide a short summary of the bug, along with any information you feel relevant to replicating the bug. --. Prior to , these types were rejected with this error: Currently, they're accepted. Based on the assumption that generic parameters on opaque type are not constrained (see Niko's comment ), I believe the previous behavior is correct. CC: label F-typealiasimpl_trait A-impl-trait T-compiler A-lifetimes
Out of curiosity, here's an exploit derived from . It type-checks but fails at monomorphization.
The above exploit is and should be easier to do with , where indirection through would not be necessary, so label I-unsound requires-nightly T-types
if the same code were written without lifetime params and used arguments instead, we'd see the same problem before , right?
Sorry, I've clearly missed the notification :sweat_smile:. I think you already know the answer given that you've just pushed a fix :).

Discussion:
// ignore the inputs to a projection, as they may not appear // in the normalized form if self.just_constrained { <del> if let ty::Projection(..) = t.kind() { </del> <ins> if let ty::Projection(..) | ty::Opaque(..) = t.kind() { </ins> return ControlFlow::CONTINUE; } }

// though we can easily give a hint that ought to be // relevant. err.note( <del> "lifetimes appearing in an associated type are not considered constrained", </del> <ins> "lifetimes appearing in an associated or opaque type are not considered constrained", </ins> ); <ins> err.note("consider introducing a named lifetime parameter"); </ins> } err.emit();

fn foo(x: impl Fn(<S as T<'_>>::A) -> <S as T<'_>>::A) {} //~^ ERROR binding for associated type `Output` references an anonymous lifetime <del> //~^^ NOTE lifetimes appearing in an associated type are not considered constrained </del> <ins> //~| NOTE lifetimes appearing in an associated or opaque type are not considered constrained //~| NOTE consider introducing a named lifetime parameter </ins> fn main() {}

LL | fn foo(x: impl Fn(<S as T<'_>>::A) -> <S as T<'_>>::A) {} |                                       ^^^^^^^^^^^^^^^ | <del> = note: lifetimes appearing in an associated type are not considered constrained </del> <ins> = note: lifetimes appearing in an associated or opaque type are not considered constrained = note: consider introducing a named lifetime parameter </ins> error: aborting due to previous error

LL | fn f(_: X) -> X { |               ^ | <del> = note: lifetimes appearing in an associated type are not considered constrained </del> <ins> = note: lifetimes appearing in an associated or opaque type are not considered constrained = note: consider introducing a named lifetime parameter </ins> error[E0581]: return type references lifetime `'a`, which is not constrained by the fn input types --> $DIR/issue-47511.rs:12:23

<del> // check-pass </del> #![feature(type_alias_impl_trait)] <del> mod foo { </del> <ins> mod lifetime_params { </ins> type Ty<'a> = impl Sized; fn defining(s: &str) -> Ty<'_> { s } fn execute(ty: Ty<'_>) -> &str { todo!() } <ins> //~^ ERROR return type references an anonymous lifetime, which is not constrained by the fn input types type BadFnSig = fn(Ty<'_>) -> &str; //~^ ERROR return type references an anonymous lifetime, which is not constrained by the fn input types type BadTraitRef = dyn Fn(Ty<'_>) -> &str; //~^ ERROR binding for associated type `Output` references an anonymous lifetime </ins> } <del> mod bar { </del> <ins> mod lifetime_params_2 { </ins> type Ty<'a> = impl FnOnce() -> &'a str; fn defining(s: &str) -> Ty<'_> { move || s } fn execute(ty: Ty<'_>) -> &str { ty() } <ins> //~^ ERROR return type references an anonymous lifetime, which is not constrained by the fn input types } // regression test for https://github.com/rust-lang/rust/issues/97104 mod type_params { type Ty<T> = impl Sized; fn define<T>(s: T) -> Ty<T> { s } type BadFnSig = fn(Ty<&str>) -> &str; //~^ ERROR return type references an anonymous lifetime, which is not constrained by the fn input types type BadTraitRef = dyn Fn(Ty<&str>) -> &str; //~^ ERROR binding for associated type `Output` references an anonymous lifetime </ins> } fn main() {}

<ins> error[E0581]: return type references an anonymous lifetime, which is not constrained by the fn input types --> $DIR/constrain_inputs.rs:6:31 | LL |     fn execute(ty: Ty<'_>) -> &str { todo!() } |                               ^^^^ | = note: lifetimes appearing in an associated or opaque type are not considered constrained = note: consider introducing a named lifetime parameter error[E0581]: return type references an anonymous lifetime, which is not constrained by the fn input types --> $DIR/constrain_inputs.rs:9:35 | LL |     type BadFnSig = fn(Ty<'_>) -> &str; |                                   ^^^^ | = note: lifetimes appearing in an associated or opaque type are not considered constrained = note: consider introducing a named lifetime parameter error[E0582]: binding for associated type `Output` references an anonymous lifetime, which does not appear in the trait input types --> $DIR/constrain_inputs.rs:11:42 | LL |     type BadTraitRef = dyn Fn(Ty<'_>) -> &str; |                                          ^^^^ | = note: lifetimes appearing in an associated or opaque type are not considered constrained = note: consider introducing a named lifetime parameter error[E0581]: return type references an anonymous lifetime, which is not constrained by the fn input types --> $DIR/constrain_inputs.rs:18:31 | LL |     fn execute(ty: Ty<'_>) -> &str { ty() } |                               ^^^^ | = note: lifetimes appearing in an associated or opaque type are not considered constrained = note: consider introducing a named lifetime parameter error[E0581]: return type references an anonymous lifetime, which is not constrained by the fn input types --> $DIR/constrain_inputs.rs:27:37 | LL |     type BadFnSig = fn(Ty<&str>) -> &str; |                                     ^^^^ | = note: lifetimes appearing in an associated or opaque type are not considered constrained = note: consider introducing a named lifetime parameter error[E0582]: binding for associated type `Output` references an anonymous lifetime, which does not appear in the trait input types --> $DIR/constrain_inputs.rs:29:44 | LL |     type BadTraitRef = dyn Fn(Ty<&str>) -> &str; |                                            ^^^^ | = note: lifetimes appearing in an associated or opaque type are not considered constrained = note: consider introducing a named lifetime parameter error: aborting due to 6 previous errors Some errors have detailed explanations: E0581, E0582. For more information about an error, try `rustc --explain E0581`. </ins>

<ins> #![feature(type_alias_impl_trait)] trait Static: 'static {} impl Static for () {} type Gal<T> = impl Static; fn _defining<T>() -> Gal<T> {} trait Callable<Arg> { type Output; } /// We can infer `<C as Callable<Arg>>::Output: 'static`, /// because we know `C: 'static` and `Arg: 'static`, fn box_str<C, Arg>(s: C::Output) -> Box<dyn AsRef<str> + 'static> where Arg: Static, C: ?Sized + Callable<Arg> + 'static, C::Output: AsRef<str>, { Box::new(s) } fn extend_lifetime(s: &str) -> Box<dyn AsRef<str> + 'static> { type MalformedTy = dyn for<'a> Callable<Gal<&'a ()>, Output = &'a str>; //~^ ERROR binding for associated type `Output` references lifetime `'a` box_str::<MalformedTy, _>(s) } fn main() { let extended = extend_lifetime(&String::from("hello")); println!("{}", extended.as_ref().as_ref()); } </ins>

<ins> error[E0582]: binding for associated type `Output` references lifetime `'a`, which does not appear in the trait input types --> $DIR/constrain_inputs_unsound.rs:23:58 | LL |     type MalformedTy = dyn for<'a> Callable<Gal<&'a ()>, Output = &'a str>; |                                                          ^^^^^^^^^^^^^^^^ error: aborting due to previous error For more information about this error, try `rustc --explain E0582`. </ins>