@@ -327,7 +327,14 @@ enum FnDeclKind {
327
327
}
328
328
329
329
impl 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 {
331
338
match self {
332
339
FnDeclKind :: Fn | FnDeclKind :: Inherent => true ,
333
340
FnDeclKind :: Impl if tcx. features ( ) . return_position_impl_trait_in_trait => true ,
@@ -1267,7 +1274,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
1267
1274
generic_params,
1268
1275
unsafety : self . lower_unsafety ( f. unsafety ) ,
1269
1276
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 ) ,
1271
1278
param_names : self . lower_fn_params_to_names ( & f. decl ) ,
1272
1279
} ) )
1273
1280
}
@@ -1671,7 +1678,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
1671
1678
fn lower_fn_decl (
1672
1679
& mut self ,
1673
1680
decl : & FnDecl ,
1674
- fn_node_id : Option < NodeId > ,
1681
+ fn_node_id : NodeId ,
1675
1682
fn_span : Span ,
1676
1683
kind : FnDeclKind ,
1677
1684
make_ret_async : Option < ( NodeId , Span ) > ,
@@ -1686,23 +1693,21 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
1686
1693
inputs = & inputs[ ..inputs. len ( ) - 1 ] ;
1687
1694
}
1688
1695
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
1691
1698
} 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)
1706
1711
} ) ) ;
1707
1712
1708
1713
let output = if let Some ( ( ret_id, span) ) = make_ret_async {
@@ -1725,22 +1730,21 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
1725
1730
1726
1731
self . lower_async_fn_ret_ty (
1727
1732
& decl. output ,
1728
- fn_node_id. expect ( "`make_ret_async` but no `fn_def_id`" ) ,
1733
+ fn_node_id,
1729
1734
ret_id,
1730
1735
matches ! ( kind, FnDeclKind :: Trait ) ,
1731
1736
)
1732
1737
} else {
1733
1738
match & decl. output {
1734
1739
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 ) ,
1742
1745
}
1743
- _ => ImplTraitContext :: Disallowed ( match kind {
1746
+ } else {
1747
+ ImplTraitContext :: Disallowed ( match kind {
1744
1748
FnDeclKind :: Fn | FnDeclKind :: Inherent => {
1745
1749
unreachable ! ( "fn should allow in-band lifetimes" )
1746
1750
}
@@ -1749,7 +1753,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
1749
1753
FnDeclKind :: Pointer => ImplTraitPosition :: PointerReturn ,
1750
1754
FnDeclKind :: Trait => ImplTraitPosition :: TraitReturn ,
1751
1755
FnDeclKind :: Impl => ImplTraitPosition :: ImplReturn ,
1752
- } ) ,
1756
+ } )
1753
1757
} ;
1754
1758
hir:: FnRetTy :: Return ( self . lower_ty ( ty, & mut context) )
1755
1759
}
@@ -1761,6 +1765,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
1761
1765
inputs,
1762
1766
output,
1763
1767
c_variadic,
1768
+ lifetime_elision_allowed : self . resolver . lifetime_elision_allowed . contains ( & fn_node_id) ,
1764
1769
implicit_self : decl. inputs . get ( 0 ) . map_or ( hir:: ImplicitSelfKind :: None , |arg| {
1765
1770
let is_mutable_pat = matches ! (
1766
1771
arg. pat. kind,
0 commit comments