@@ -191,7 +191,9 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
191191 self . lower_angle_bracketed_parameter_data ( data, param_mode, itctx)
192192 }
193193 GenericArgs :: Parenthesized ( ref data) => match parenthesized_generic_args {
194- ParenthesizedGenericArgs :: Ok => self . lower_parenthesized_parameter_data ( data) ,
194+ ParenthesizedGenericArgs :: Ok => {
195+ self . lower_parenthesized_parameter_data ( data, itctx)
196+ }
195197 ParenthesizedGenericArgs :: Err => {
196198 // Suggest replacing parentheses with angle brackets `Trait(params...)` to `Trait<params...>`
197199 let sub = if !data. inputs . is_empty ( ) {
@@ -344,6 +346,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
344346 fn lower_parenthesized_parameter_data (
345347 & mut self ,
346348 data : & ParenthesizedArgs ,
349+ itctx : & ImplTraitContext ,
347350 ) -> ( GenericArgsCtor < ' hir > , bool ) {
348351 // Switch to `PassThrough` mode for anonymous lifetimes; this
349352 // means that we permit things like `&Ref<T>`, where `Ref` has
@@ -355,6 +358,17 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
355358 self . lower_ty_direct ( ty, & ImplTraitContext :: Disallowed ( ImplTraitPosition :: FnTraitParam ) )
356359 } ) ) ;
357360 let output_ty = match output {
361+ // Only allow `impl Trait` in return position. i.e.:
362+ // ```rust
363+ // fn f(_: impl Fn() -> impl Debug) -> impl Fn() -> impl Debug
364+ // // disallowed --^^^^^^^^^^ allowed --^^^^^^^^^^
365+ // ```
366+ FnRetTy :: Ty ( ty)
367+ if matches ! ( itctx, ImplTraitContext :: ReturnPositionOpaqueTy { .. } )
368+ && self . tcx . features ( ) . impl_trait_in_fn_trait_return =>
369+ {
370+ self . lower_ty ( & ty, itctx)
371+ }
358372 FnRetTy :: Ty ( ty) => {
359373 self . lower_ty ( & ty, & ImplTraitContext :: Disallowed ( ImplTraitPosition :: FnTraitReturn ) )
360374 }
0 commit comments