@@ -327,7 +327,14 @@ enum FnDeclKind {
327327}
328328
329329impl FnDeclKind {
330- fn impl_trait_allowed ( & self , tcx : TyCtxt < ' _ > ) -> bool {
330+ fn param_impl_trait_allowed ( & self ) -> bool {
331+ match self {
332+ FnDeclKind :: Fn | FnDeclKind :: Inherent | FnDeclKind :: Impl | FnDeclKind :: Trait => true ,
333+ _ => false ,
334+ }
335+ }
336+
337+ fn return_impl_trait_allowed ( & self , tcx : TyCtxt < ' _ > ) -> bool {
331338 match self {
332339 FnDeclKind :: Fn | FnDeclKind :: Inherent => true ,
333340 FnDeclKind :: Impl if tcx. features ( ) . return_position_impl_trait_in_trait => true ,
@@ -1267,7 +1274,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
12671274 generic_params,
12681275 unsafety : self . lower_unsafety ( f. unsafety ) ,
12691276 abi : self . lower_extern ( f. ext ) ,
1270- decl : self . lower_fn_decl ( & f. decl , None , t. span , FnDeclKind :: Pointer , None ) ,
1277+ decl : self . lower_fn_decl ( & f. decl , t . id , t. span , FnDeclKind :: Pointer , None ) ,
12711278 param_names : self . lower_fn_params_to_names ( & f. decl ) ,
12721279 } ) )
12731280 }
@@ -1671,7 +1678,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
16711678 fn lower_fn_decl (
16721679 & mut self ,
16731680 decl : & FnDecl ,
1674- fn_node_id : Option < NodeId > ,
1681+ fn_node_id : NodeId ,
16751682 fn_span : Span ,
16761683 kind : FnDeclKind ,
16771684 make_ret_async : Option < ( NodeId , Span ) > ,
@@ -1686,23 +1693,21 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
16861693 inputs = & inputs[ ..inputs. len ( ) - 1 ] ;
16871694 }
16881695 let inputs = self . arena . alloc_from_iter ( inputs. iter ( ) . map ( |param| {
1689- if fn_node_id . is_some ( ) {
1690- self . lower_ty_direct ( & param . ty , & ImplTraitContext :: Universal )
1696+ let itctx = if kind . param_impl_trait_allowed ( ) {
1697+ ImplTraitContext :: Universal
16911698 } else {
1692- self . lower_ty_direct (
1693- & param. ty ,
1694- & ImplTraitContext :: Disallowed ( match kind {
1695- FnDeclKind :: Fn | FnDeclKind :: Inherent => {
1696- unreachable ! ( "fn should allow in-band lifetimes" )
1697- }
1698- FnDeclKind :: ExternFn => ImplTraitPosition :: ExternFnParam ,
1699- FnDeclKind :: Closure => ImplTraitPosition :: ClosureParam ,
1700- FnDeclKind :: Pointer => ImplTraitPosition :: PointerParam ,
1701- FnDeclKind :: Trait => ImplTraitPosition :: TraitParam ,
1702- FnDeclKind :: Impl => ImplTraitPosition :: ImplParam ,
1703- } ) ,
1704- )
1705- }
1699+ ImplTraitContext :: Disallowed ( match kind {
1700+ FnDeclKind :: Fn | FnDeclKind :: Inherent => {
1701+ unreachable ! ( "fn should allow APIT" )
1702+ }
1703+ FnDeclKind :: ExternFn => ImplTraitPosition :: ExternFnParam ,
1704+ FnDeclKind :: Closure => ImplTraitPosition :: ClosureParam ,
1705+ FnDeclKind :: Pointer => ImplTraitPosition :: PointerParam ,
1706+ FnDeclKind :: Trait => ImplTraitPosition :: TraitParam ,
1707+ FnDeclKind :: Impl => ImplTraitPosition :: ImplParam ,
1708+ } )
1709+ } ;
1710+ self . lower_ty_direct ( & param. ty , & itctx)
17061711 } ) ) ;
17071712
17081713 let output = if let Some ( ( ret_id, span) ) = make_ret_async {
@@ -1725,22 +1730,21 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
17251730
17261731 self . lower_async_fn_ret_ty (
17271732 & decl. output ,
1728- fn_node_id. expect ( "`make_ret_async` but no `fn_def_id`" ) ,
1733+ fn_node_id,
17291734 ret_id,
17301735 matches ! ( kind, FnDeclKind :: Trait ) ,
17311736 )
17321737 } else {
17331738 match & decl. output {
17341739 FnRetTy :: Ty ( ty) => {
1735- let mut context = match fn_node_id {
1736- Some ( fn_node_id) if kind. impl_trait_allowed ( self . tcx ) => {
1737- let fn_def_id = self . local_def_id ( fn_node_id) ;
1738- ImplTraitContext :: ReturnPositionOpaqueTy {
1739- origin : hir:: OpaqueTyOrigin :: FnReturn ( fn_def_id) ,
1740- in_trait : matches ! ( kind, FnDeclKind :: Trait ) ,
1741- }
1740+ let mut context = if kind. return_impl_trait_allowed ( self . tcx ) {
1741+ let fn_def_id = self . local_def_id ( fn_node_id) ;
1742+ ImplTraitContext :: ReturnPositionOpaqueTy {
1743+ origin : hir:: OpaqueTyOrigin :: FnReturn ( fn_def_id) ,
1744+ in_trait : matches ! ( kind, FnDeclKind :: Trait ) ,
17421745 }
1743- _ => ImplTraitContext :: Disallowed ( match kind {
1746+ } else {
1747+ ImplTraitContext :: Disallowed ( match kind {
17441748 FnDeclKind :: Fn | FnDeclKind :: Inherent => {
17451749 unreachable ! ( "fn should allow in-band lifetimes" )
17461750 }
@@ -1749,7 +1753,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
17491753 FnDeclKind :: Pointer => ImplTraitPosition :: PointerReturn ,
17501754 FnDeclKind :: Trait => ImplTraitPosition :: TraitReturn ,
17511755 FnDeclKind :: Impl => ImplTraitPosition :: ImplReturn ,
1752- } ) ,
1756+ } )
17531757 } ;
17541758 hir:: FnRetTy :: Return ( self . lower_ty ( ty, & mut context) )
17551759 }
@@ -1761,6 +1765,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
17611765 inputs,
17621766 output,
17631767 c_variadic,
1768+ lifetime_elision_allowed : self . resolver . lifetime_elision_allowed . contains ( & fn_node_id) ,
17641769 implicit_self : decl. inputs . get ( 0 ) . map_or ( hir:: ImplicitSelfKind :: None , |arg| {
17651770 let is_mutable_pat = matches ! (
17661771 arg. pat. kind,
0 commit comments