@@ -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 ,
@@ -1255,7 +1262,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
1255
1262
} else {
1256
1263
self . next_node_id ( )
1257
1264
} ;
1258
- let span = self . tcx . sess . source_map ( ) . start_point ( t. span ) ;
1265
+ let span = self . tcx . sess . source_map ( ) . start_point ( t. span ) . shrink_to_hi ( ) ;
1259
1266
Lifetime { ident : Ident :: new ( kw:: UnderscoreLifetime , span) , id }
1260
1267
} ) ;
1261
1268
let lifetime = self . lower_lifetime ( & region) ;
@@ -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
}
@@ -1546,15 +1553,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
1546
1553
let lifetimes =
1547
1554
self . arena . alloc_from_iter ( collected_lifetimes. into_iter ( ) . map ( |( _, lifetime) | {
1548
1555
let id = self . next_node_id ( ) ;
1549
- let span = lifetime. ident . span ;
1550
-
1551
- let ident = if lifetime. ident . name == kw:: UnderscoreLifetime {
1552
- Ident :: with_dummy_span ( kw:: UnderscoreLifetime )
1553
- } else {
1554
- lifetime. ident
1555
- } ;
1556
-
1557
- let l = self . new_named_lifetime ( lifetime. id , id, span, ident) ;
1556
+ let l = self . new_named_lifetime ( lifetime. id , id, lifetime. ident ) ;
1558
1557
hir:: GenericArg :: Lifetime ( l)
1559
1558
} ) ) ;
1560
1559
debug ! ( ?lifetimes) ;
@@ -1679,7 +1678,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
1679
1678
fn lower_fn_decl (
1680
1679
& mut self ,
1681
1680
decl : & FnDecl ,
1682
- fn_node_id : Option < NodeId > ,
1681
+ fn_node_id : NodeId ,
1683
1682
fn_span : Span ,
1684
1683
kind : FnDeclKind ,
1685
1684
make_ret_async : Option < ( NodeId , Span ) > ,
@@ -1694,23 +1693,21 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
1694
1693
inputs = & inputs[ ..inputs. len ( ) - 1 ] ;
1695
1694
}
1696
1695
let inputs = self . arena . alloc_from_iter ( inputs. iter ( ) . map ( |param| {
1697
- if fn_node_id . is_some ( ) {
1698
- self . lower_ty_direct ( & param . ty , & ImplTraitContext :: Universal )
1696
+ let itctx = if kind . param_impl_trait_allowed ( ) {
1697
+ ImplTraitContext :: Universal
1699
1698
} else {
1700
- self . lower_ty_direct (
1701
- & param. ty ,
1702
- & ImplTraitContext :: Disallowed ( match kind {
1703
- FnDeclKind :: Fn | FnDeclKind :: Inherent => {
1704
- unreachable ! ( "fn should allow in-band lifetimes" )
1705
- }
1706
- FnDeclKind :: ExternFn => ImplTraitPosition :: ExternFnParam ,
1707
- FnDeclKind :: Closure => ImplTraitPosition :: ClosureParam ,
1708
- FnDeclKind :: Pointer => ImplTraitPosition :: PointerParam ,
1709
- FnDeclKind :: Trait => ImplTraitPosition :: TraitParam ,
1710
- FnDeclKind :: Impl => ImplTraitPosition :: ImplParam ,
1711
- } ) ,
1712
- )
1713
- }
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)
1714
1711
} ) ) ;
1715
1712
1716
1713
let output = if let Some ( ( ret_id, span) ) = make_ret_async {
@@ -1733,22 +1730,21 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
1733
1730
1734
1731
self . lower_async_fn_ret_ty (
1735
1732
& decl. output ,
1736
- fn_node_id. expect ( "`make_ret_async` but no `fn_def_id`" ) ,
1733
+ fn_node_id,
1737
1734
ret_id,
1738
1735
matches ! ( kind, FnDeclKind :: Trait ) ,
1739
1736
)
1740
1737
} else {
1741
1738
match & decl. output {
1742
1739
FnRetTy :: Ty ( ty) => {
1743
- let mut context = match fn_node_id {
1744
- Some ( fn_node_id) if kind. impl_trait_allowed ( self . tcx ) => {
1745
- let fn_def_id = self . local_def_id ( fn_node_id) ;
1746
- ImplTraitContext :: ReturnPositionOpaqueTy {
1747
- origin : hir:: OpaqueTyOrigin :: FnReturn ( fn_def_id) ,
1748
- in_trait : matches ! ( kind, FnDeclKind :: Trait ) ,
1749
- }
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 ) ,
1750
1745
}
1751
- _ => ImplTraitContext :: Disallowed ( match kind {
1746
+ } else {
1747
+ ImplTraitContext :: Disallowed ( match kind {
1752
1748
FnDeclKind :: Fn | FnDeclKind :: Inherent => {
1753
1749
unreachable ! ( "fn should allow in-band lifetimes" )
1754
1750
}
@@ -1757,7 +1753,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
1757
1753
FnDeclKind :: Pointer => ImplTraitPosition :: PointerReturn ,
1758
1754
FnDeclKind :: Trait => ImplTraitPosition :: TraitReturn ,
1759
1755
FnDeclKind :: Impl => ImplTraitPosition :: ImplReturn ,
1760
- } ) ,
1756
+ } )
1761
1757
} ;
1762
1758
hir:: FnRetTy :: Return ( self . lower_ty ( ty, & mut context) )
1763
1759
}
@@ -1769,6 +1765,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
1769
1765
inputs,
1770
1766
output,
1771
1767
c_variadic,
1768
+ lifetime_elision_allowed : self . resolver . lifetime_elision_allowed . contains ( & fn_node_id) ,
1772
1769
implicit_self : decl. inputs . get ( 0 ) . map_or ( hir:: ImplicitSelfKind :: None , |arg| {
1773
1770
let is_mutable_pat = matches ! (
1774
1771
arg. pat. kind,
@@ -2010,18 +2007,10 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
2010
2007
let generic_args = self . arena . alloc_from_iter ( collected_lifetimes. into_iter ( ) . map (
2011
2008
|( _, lifetime, res) | {
2012
2009
let id = self . next_node_id ( ) ;
2013
- let span = lifetime. ident . span ;
2014
-
2015
- let ident = if lifetime. ident . name == kw:: UnderscoreLifetime {
2016
- Ident :: with_dummy_span ( kw:: UnderscoreLifetime )
2017
- } else {
2018
- lifetime. ident
2019
- } ;
2020
-
2021
2010
let res = res. unwrap_or (
2022
2011
self . resolver . get_lifetime_res ( lifetime. id ) . unwrap_or ( LifetimeRes :: Error ) ,
2023
2012
) ;
2024
- hir:: GenericArg :: Lifetime ( self . new_named_lifetime_with_res ( id, span , ident, res) )
2013
+ hir:: GenericArg :: Lifetime ( self . new_named_lifetime_with_res ( id, lifetime . ident , res) )
2025
2014
} ,
2026
2015
) ) ;
2027
2016
@@ -2091,43 +2080,40 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
2091
2080
}
2092
2081
2093
2082
fn lower_lifetime ( & mut self , l : & Lifetime ) -> & ' hir hir:: Lifetime {
2094
- let span = self . lower_span ( l. ident . span ) ;
2095
2083
let ident = self . lower_ident ( l. ident ) ;
2096
- self . new_named_lifetime ( l. id , l. id , span , ident)
2084
+ self . new_named_lifetime ( l. id , l. id , ident)
2097
2085
}
2098
2086
2099
2087
#[ instrument( level = "debug" , skip( self ) ) ]
2100
2088
fn new_named_lifetime_with_res (
2101
2089
& mut self ,
2102
2090
id : NodeId ,
2103
- span : Span ,
2104
2091
ident : Ident ,
2105
2092
res : LifetimeRes ,
2106
2093
) -> & ' hir hir:: Lifetime {
2107
- let name = match res {
2094
+ let res = match res {
2108
2095
LifetimeRes :: Param { param, .. } => {
2109
- let p_name = ParamName :: Plain ( ident) ;
2110
2096
let param = self . get_remapped_def_id ( param) ;
2111
-
2112
- hir:: LifetimeName :: Param ( param, p_name)
2097
+ hir:: LifetimeName :: Param ( param)
2113
2098
}
2114
2099
LifetimeRes :: Fresh { param, .. } => {
2115
- debug_assert_eq ! ( ident. name, kw:: UnderscoreLifetime ) ;
2116
2100
let param = self . local_def_id ( param) ;
2117
-
2118
- hir:: LifetimeName :: Param ( param, ParamName :: Fresh )
2101
+ hir:: LifetimeName :: Param ( param)
2119
2102
}
2120
2103
LifetimeRes :: Infer => hir:: LifetimeName :: Infer ,
2121
2104
LifetimeRes :: Static => hir:: LifetimeName :: Static ,
2122
2105
LifetimeRes :: Error => hir:: LifetimeName :: Error ,
2123
- res => panic ! ( "Unexpected lifetime resolution {:?} for {:?} at {:?}" , res, ident, span) ,
2106
+ res => panic ! (
2107
+ "Unexpected lifetime resolution {:?} for {:?} at {:?}" ,
2108
+ res, ident, ident. span
2109
+ ) ,
2124
2110
} ;
2125
2111
2126
- debug ! ( ?name ) ;
2112
+ debug ! ( ?res ) ;
2127
2113
self . arena . alloc ( hir:: Lifetime {
2128
2114
hir_id : self . lower_node_id ( id) ,
2129
- span : self . lower_span ( span ) ,
2130
- name ,
2115
+ ident : self . lower_ident ( ident ) ,
2116
+ res ,
2131
2117
} )
2132
2118
}
2133
2119
@@ -2136,11 +2122,10 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
2136
2122
& mut self ,
2137
2123
id : NodeId ,
2138
2124
new_id : NodeId ,
2139
- span : Span ,
2140
2125
ident : Ident ,
2141
2126
) -> & ' hir hir:: Lifetime {
2142
2127
let res = self . resolver . get_lifetime_res ( id) . unwrap_or ( LifetimeRes :: Error ) ;
2143
- self . new_named_lifetime_with_res ( new_id, span , ident, res)
2128
+ self . new_named_lifetime_with_res ( new_id, ident, res)
2144
2129
}
2145
2130
2146
2131
fn lower_generic_params_mut < ' s > (
@@ -2552,8 +2537,8 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
2552
2537
fn elided_dyn_bound ( & mut self , span : Span ) -> & ' hir hir:: Lifetime {
2553
2538
let r = hir:: Lifetime {
2554
2539
hir_id : self . next_id ( ) ,
2555
- span : self . lower_span ( span) ,
2556
- name : hir:: LifetimeName :: ImplicitObjectLifetimeDefault ,
2540
+ ident : Ident :: new ( kw :: Empty , self . lower_span ( span) ) ,
2541
+ res : hir:: LifetimeName :: ImplicitObjectLifetimeDefault ,
2557
2542
} ;
2558
2543
debug ! ( "elided_dyn_bound: r={:?}" , r) ;
2559
2544
self . arena . alloc ( r)
0 commit comments