@@ -55,8 +55,8 @@ use rustc_errors::{DiagArgFromDisplay, DiagCtxtHandle, StashKey};
5555use rustc_hir:: def:: { DefKind , LifetimeRes , Namespace , PartialRes , PerNS , Res } ;
5656use rustc_hir:: def_id:: { CRATE_DEF_ID , LOCAL_CRATE , LocalDefId } ;
5757use rustc_hir:: {
58- self as hir, ConstArg , GenericArg , HirId , IsAnonInPath , ItemLocalMap , LangItem , ParamName ,
59- TraitCandidate ,
58+ self as hir, ConstArg , GenericArg , HirId , ItemLocalMap , LangItem , LifetimeSource ,
59+ LifetimeSyntax , ParamName , TraitCandidate ,
6060} ;
6161use rustc_index:: { Idx , IndexSlice , IndexVec } ;
6262use rustc_macros:: extension;
@@ -1080,7 +1080,11 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
10801080 itctx : ImplTraitContext ,
10811081 ) -> hir:: GenericArg < ' hir > {
10821082 match arg {
1083- ast:: GenericArg :: Lifetime ( lt) => GenericArg :: Lifetime ( self . lower_lifetime ( lt) ) ,
1083+ ast:: GenericArg :: Lifetime ( lt) => GenericArg :: Lifetime ( self . lower_lifetime (
1084+ lt,
1085+ LifetimeSource :: Path { with_angle_brackets : true } ,
1086+ lt. ident . into ( ) ,
1087+ ) ) ,
10841088 ast:: GenericArg :: Type ( ty) => {
10851089 // We cannot just match on `TyKind::Infer` as `(_)` is represented as
10861090 // `TyKind::Paren(TyKind::Infer)` and should also be lowered to `GenericArg::Infer`
@@ -1199,35 +1203,11 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
11991203 TyKind :: Slice ( ty) => hir:: TyKind :: Slice ( self . lower_ty ( ty, itctx) ) ,
12001204 TyKind :: Ptr ( mt) => hir:: TyKind :: Ptr ( self . lower_mt ( mt, itctx) ) ,
12011205 TyKind :: Ref ( region, mt) => {
1202- let region = region. unwrap_or_else ( || {
1203- let id = if let Some ( LifetimeRes :: ElidedAnchor { start, end } ) =
1204- self . resolver . get_lifetime_res ( t. id )
1205- {
1206- debug_assert_eq ! ( start. plus( 1 ) , end) ;
1207- start
1208- } else {
1209- self . next_node_id ( )
1210- } ;
1211- let span = self . tcx . sess . source_map ( ) . start_point ( t. span ) . shrink_to_hi ( ) ;
1212- Lifetime { ident : Ident :: new ( kw:: UnderscoreLifetime , span) , id }
1213- } ) ;
1214- let lifetime = self . lower_lifetime ( & region) ;
1206+ let lifetime = self . lower_ty_direct_lifetime ( t, * region) ;
12151207 hir:: TyKind :: Ref ( lifetime, self . lower_mt ( mt, itctx) )
12161208 }
12171209 TyKind :: PinnedRef ( region, mt) => {
1218- let region = region. unwrap_or_else ( || {
1219- let id = if let Some ( LifetimeRes :: ElidedAnchor { start, end } ) =
1220- self . resolver . get_lifetime_res ( t. id )
1221- {
1222- debug_assert_eq ! ( start. plus( 1 ) , end) ;
1223- start
1224- } else {
1225- self . next_node_id ( )
1226- } ;
1227- let span = self . tcx . sess . source_map ( ) . start_point ( t. span ) . shrink_to_hi ( ) ;
1228- Lifetime { ident : Ident :: new ( kw:: UnderscoreLifetime , span) , id }
1229- } ) ;
1230- let lifetime = self . lower_lifetime ( & region) ;
1210+ let lifetime = self . lower_ty_direct_lifetime ( t, * region) ;
12311211 let kind = hir:: TyKind :: Ref ( lifetime, self . lower_mt ( mt, itctx) ) ;
12321212 let span = self . lower_span ( t. span ) ;
12331213 let arg = hir:: Ty { kind, span, hir_id : self . next_id ( ) } ;
@@ -1303,7 +1283,11 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
13031283 }
13041284 GenericBound :: Outlives ( lifetime) => {
13051285 if lifetime_bound. is_none ( ) {
1306- lifetime_bound = Some ( this. lower_lifetime ( lifetime) ) ;
1286+ lifetime_bound = Some ( this. lower_lifetime (
1287+ lifetime,
1288+ LifetimeSource :: Other ,
1289+ lifetime. ident . into ( ) ,
1290+ ) ) ;
13071291 }
13081292 None
13091293 }
@@ -1394,6 +1378,31 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
13941378 hir:: Ty { kind, span : self . lower_span ( t. span ) , hir_id : self . lower_node_id ( t. id ) }
13951379 }
13961380
1381+ fn lower_ty_direct_lifetime (
1382+ & mut self ,
1383+ t : & Ty ,
1384+ region : Option < Lifetime > ,
1385+ ) -> & ' hir hir:: Lifetime {
1386+ let ( region, syntax) = match region {
1387+ Some ( region) => ( region, region. ident . into ( ) ) ,
1388+
1389+ None => {
1390+ let id = if let Some ( LifetimeRes :: ElidedAnchor { start, end } ) =
1391+ self . resolver . get_lifetime_res ( t. id )
1392+ {
1393+ debug_assert_eq ! ( start. plus( 1 ) , end) ;
1394+ start
1395+ } else {
1396+ self . next_node_id ( )
1397+ } ;
1398+ let span = self . tcx . sess . source_map ( ) . start_point ( t. span ) . shrink_to_hi ( ) ;
1399+ let region = Lifetime { ident : Ident :: new ( kw:: UnderscoreLifetime , span) , id } ;
1400+ ( region, LifetimeSyntax :: Hidden )
1401+ }
1402+ } ;
1403+ self . lower_lifetime ( & region, LifetimeSource :: Reference , syntax)
1404+ }
1405+
13971406 /// Lowers a `ReturnPositionOpaqueTy` (`-> impl Trait`) or a `TypeAliasesOpaqueTy` (`type F =
13981407 /// impl Trait`): this creates the associated Opaque Type (TAIT) definition and then returns a
13991408 /// HIR type that references the TAIT.
@@ -1475,9 +1484,9 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
14751484 precise_capturing_args : & [ PreciseCapturingArg ] ,
14761485 ) -> & ' hir [ hir:: PreciseCapturingArg < ' hir > ] {
14771486 self . arena . alloc_from_iter ( precise_capturing_args. iter ( ) . map ( |arg| match arg {
1478- PreciseCapturingArg :: Lifetime ( lt) => {
1479- hir :: PreciseCapturingArg :: Lifetime ( self . lower_lifetime ( lt) )
1480- }
1487+ PreciseCapturingArg :: Lifetime ( lt) => hir :: PreciseCapturingArg :: Lifetime (
1488+ self . lower_lifetime ( lt, LifetimeSource :: PreciseCapturing , lt . ident . into ( ) ) ,
1489+ ) ,
14811490 PreciseCapturingArg :: Arg ( path, id) => {
14821491 let [ segment] = path. segments . as_slice ( ) else {
14831492 panic ! ( ) ;
@@ -1745,22 +1754,40 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
17451754 ) -> hir:: GenericBound < ' hir > {
17461755 match tpb {
17471756 GenericBound :: Trait ( p) => hir:: GenericBound :: Trait ( self . lower_poly_trait_ref ( p, itctx) ) ,
1748- GenericBound :: Outlives ( lifetime) => {
1749- hir:: GenericBound :: Outlives ( self . lower_lifetime ( lifetime) )
1750- }
1757+ GenericBound :: Outlives ( lifetime) => hir:: GenericBound :: Outlives ( self . lower_lifetime (
1758+ lifetime,
1759+ LifetimeSource :: OutlivesBound ,
1760+ lifetime. ident . into ( ) ,
1761+ ) ) ,
17511762 GenericBound :: Use ( args, span) => hir:: GenericBound :: Use (
17521763 self . lower_precise_capturing_args ( args) ,
17531764 self . lower_span ( * span) ,
17541765 ) ,
17551766 }
17561767 }
17571768
1758- fn lower_lifetime ( & mut self , l : & Lifetime ) -> & ' hir hir:: Lifetime {
1759- self . new_named_lifetime ( l. id , l. id , l. ident , IsAnonInPath :: No )
1769+ fn lower_lifetime (
1770+ & mut self ,
1771+ l : & Lifetime ,
1772+ source : LifetimeSource ,
1773+ syntax : LifetimeSyntax ,
1774+ ) -> & ' hir hir:: Lifetime {
1775+ self . new_named_lifetime ( l. id , l. id , l. ident , source, syntax)
17601776 }
17611777
1762- fn lower_lifetime_anon_in_path ( & mut self , id : NodeId , span : Span ) -> & ' hir hir:: Lifetime {
1763- self . new_named_lifetime ( id, id, Ident :: new ( kw:: UnderscoreLifetime , span) , IsAnonInPath :: Yes )
1778+ fn lower_lifetime_hidden_in_path (
1779+ & mut self ,
1780+ id : NodeId ,
1781+ span : Span ,
1782+ with_angle_brackets : bool ,
1783+ ) -> & ' hir hir:: Lifetime {
1784+ self . new_named_lifetime (
1785+ id,
1786+ id,
1787+ Ident :: new ( kw:: UnderscoreLifetime , span) ,
1788+ LifetimeSource :: Path { with_angle_brackets } ,
1789+ LifetimeSyntax :: Hidden ,
1790+ )
17641791 }
17651792
17661793 #[ instrument( level = "debug" , skip( self ) ) ]
@@ -1769,7 +1796,8 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
17691796 id : NodeId ,
17701797 new_id : NodeId ,
17711798 ident : Ident ,
1772- is_anon_in_path : IsAnonInPath ,
1799+ source : LifetimeSource ,
1800+ syntax : LifetimeSyntax ,
17731801 ) -> & ' hir hir:: Lifetime {
17741802 debug_assert_ne ! ( ident. name, kw:: Empty ) ;
17751803 let res = self . resolver . get_lifetime_res ( id) . unwrap_or ( LifetimeRes :: Error ) ;
@@ -1794,17 +1822,13 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
17941822 }
17951823 } ;
17961824
1797- #[ cfg( debug_assertions) ]
1798- if is_anon_in_path == IsAnonInPath :: Yes {
1799- debug_assert_eq ! ( ident. name, kw:: UnderscoreLifetime ) ;
1800- }
1801-
18021825 debug ! ( ?res) ;
18031826 self . arena . alloc ( hir:: Lifetime :: new (
18041827 self . lower_node_id ( new_id) ,
18051828 self . lower_ident ( ident) ,
18061829 res,
1807- is_anon_in_path,
1830+ source,
1831+ syntax,
18081832 ) )
18091833 }
18101834
@@ -2393,7 +2417,8 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
23932417 self . next_id ( ) ,
23942418 Ident :: new ( kw:: UnderscoreLifetime , self . lower_span ( span) ) ,
23952419 hir:: LifetimeName :: ImplicitObjectLifetimeDefault ,
2396- IsAnonInPath :: No ,
2420+ LifetimeSource :: Other ,
2421+ LifetimeSyntax :: Hidden ,
23972422 ) ;
23982423 debug ! ( "elided_dyn_bound: r={:?}" , r) ;
23992424 self . arena . alloc ( r)
0 commit comments