@@ -17,30 +17,9 @@ use tracing::debug;
1717
1818pub ( super ) fn object_lifetime_defaults (
1919 tcx : TyCtxt < ' _ > ,
20- def_id : DefId ,
20+ def_id : LocalDefId ,
2121) -> Option < & [ ObjectLifetimeDefault ] > {
22- if let Some ( def_id) = def_id. as_local ( ) {
23- match tcx. hir ( ) . get_by_def_id ( def_id) {
24- Node :: Item ( item) => compute_object_lifetime_defaults ( tcx, item) ,
25- _ => None ,
26- }
27- } else {
28- Some ( tcx. arena . alloc_from_iter ( tcx. generics_of ( def_id) . params . iter ( ) . filter_map ( |param| {
29- match param. kind {
30- GenericParamDefKind :: Type { object_lifetime_default, .. } => {
31- Some ( object_lifetime_default)
32- }
33- GenericParamDefKind :: Const { .. } => Some ( Set1 :: Empty ) ,
34- GenericParamDefKind :: Lifetime => None ,
35- }
36- } ) ) )
37- }
38- }
39-
40- fn compute_object_lifetime_defaults < ' tcx > (
41- tcx : TyCtxt < ' tcx > ,
42- item : & hir:: Item < ' _ > ,
43- ) -> Option < & ' tcx [ ObjectLifetimeDefault ] > {
22+ let Node :: Item ( item) = tcx. hir ( ) . get_by_def_id ( def_id) else { return None } ;
4423 match item. kind {
4524 hir:: ItemKind :: Struct ( _, ref generics)
4625 | hir:: ItemKind :: Union ( _, ref generics)
@@ -61,24 +40,18 @@ fn compute_object_lifetime_defaults<'tcx>(
6140 let object_lifetime_default_reprs: String = result
6241 . iter ( )
6342 . map ( |set| match * set {
64- Set1 :: Empty => "BaseDefault" . into ( ) ,
65- Set1 :: One ( Region :: Static ) => "'static" . into ( ) ,
66- Set1 :: One ( Region :: EarlyBound ( mut i, _) ) => generics
67- . params
68- . iter ( )
69- . find_map ( |param| match param. kind {
70- GenericParamKind :: Lifetime { .. } => {
71- if i == 0 {
72- return Some ( param. name . ident ( ) . to_string ( ) . into ( ) ) ;
73- }
74- i -= 1 ;
75- None
76- }
77- _ => None ,
78- } )
79- . unwrap ( ) ,
80- Set1 :: One ( _) => bug ! ( ) ,
81- Set1 :: Many => "Ambiguous" . into ( ) ,
43+ ObjectLifetimeDefault :: Empty => "BaseDefault" . into ( ) ,
44+ ObjectLifetimeDefault :: Static => "'static" . into ( ) ,
45+ ObjectLifetimeDefault :: Param ( def_id) => {
46+ let def_id = def_id. expect_local ( ) ;
47+ generics
48+ . params
49+ . iter ( )
50+ . find ( |param| tcx. hir ( ) . local_def_id ( param. hir_id ) == def_id)
51+ . map ( |param| param. name . ident ( ) . to_string ( ) . into ( ) )
52+ . unwrap ( )
53+ }
54+ ObjectLifetimeDefault :: Ambiguous => "Ambiguous" . into ( ) ,
8255 } )
8356 . collect :: < Vec < Cow < ' static , str > > > ( )
8457 . join ( "," ) ;
@@ -133,32 +106,20 @@ fn object_lifetime_defaults_for_item<'tcx>(
133106 }
134107
135108 Some ( match set {
136- Set1 :: Empty => Set1 :: Empty ,
137- Set1 :: One ( hir:: LifetimeName :: Static ) => Set1 :: One ( Region :: Static ) ,
138- Set1 :: One ( hir:: LifetimeName :: Param ( def_id, _) ) => generics
139- . params
140- . iter ( )
141- . filter_map ( |param| match param. kind {
142- GenericParamKind :: Lifetime { .. } => {
143- let param_def_id = tcx. hir ( ) . local_def_id ( param. hir_id ) ;
144- Some ( param_def_id)
145- }
146- _ => None ,
147- } )
148- . enumerate ( )
149- . find ( |& ( _, param_def_id) | param_def_id == def_id)
150- . map_or ( Set1 :: Many , |( i, _) | {
151- Set1 :: One ( Region :: EarlyBound ( i as u32 , def_id. to_def_id ( ) ) )
152- } ) ,
153- Set1 :: One ( _) | Set1 :: Many => Set1 :: Many ,
109+ Set1 :: Empty => ObjectLifetimeDefault :: Empty ,
110+ Set1 :: One ( hir:: LifetimeName :: Static ) => ObjectLifetimeDefault :: Static ,
111+ Set1 :: One ( hir:: LifetimeName :: Param ( def_id, _) ) => {
112+ ObjectLifetimeDefault :: Param ( def_id. to_def_id ( ) )
113+ }
114+ Set1 :: One ( _) | Set1 :: Many => ObjectLifetimeDefault :: Ambiguous ,
154115 } )
155116 }
156117 GenericParamKind :: Const { .. } => {
157118 // Generic consts don't impose any constraints.
158119 //
159120 // We still store a dummy value here to allow generic parameters
160121 // in an arbitrary order.
161- Some ( Set1 :: Empty )
122+ Some ( ObjectLifetimeDefault :: Empty )
162123 }
163124 } ;
164125
@@ -529,9 +490,7 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
529490 loop {
530491 match * scope {
531492 Scope :: Root => break false ,
532-
533493 Scope :: Body => break true ,
534-
535494 Scope :: Binder { s, .. }
536495 | Scope :: Static { s, .. }
537496 | Scope :: ObjectLifetimeDefault { s, .. } => {
@@ -540,28 +499,37 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
540499 }
541500 }
542501 } ;
543-
544- let set_to_region = |set : & ObjectLifetimeDefault | match * set {
545- Set1 :: Empty => {
502+ let generics = self . tcx . generics_of ( def_id ) ;
503+ let set_to_region = |set : ObjectLifetimeDefault | match set {
504+ ObjectLifetimeDefault :: Empty => {
546505 if in_body {
547506 None
548507 } else {
549508 Some ( Region :: Static )
550509 }
551510 }
552- Set1 :: One ( Region :: EarlyBound ( index, _) ) => {
553- let mut lifetimes = generic_args. args . iter ( ) . filter_map ( |arg| match arg {
554- GenericArg :: Lifetime ( lt) => Some ( lt) ,
511+ ObjectLifetimeDefault :: Static => Some ( Region :: Static ) ,
512+ ObjectLifetimeDefault :: Param ( def_id) => {
513+ let index = generics. param_def_id_to_index [ & def_id] ;
514+ generic_args. args . get ( index as usize ) . and_then ( |arg| match arg {
515+ GenericArg :: Lifetime ( lt) => self . tcx . named_region ( lt. hir_id ) ,
555516 _ => None ,
556- } ) ;
557- lifetimes
558- . nth ( index as usize )
559- . and_then ( |lifetime| self . tcx . named_region ( lifetime. hir_id ) )
517+ } )
560518 }
561- Set1 :: One ( r) => Some ( r) ,
562- Set1 :: Many => None ,
519+ ObjectLifetimeDefault :: Ambiguous => None ,
563520 } ;
564- self . tcx . object_lifetime_defaults ( def_id) . unwrap ( ) . iter ( ) . map ( set_to_region) . collect ( )
521+ generics
522+ . params
523+ . iter ( )
524+ . filter_map ( |param| match param. kind {
525+ GenericParamDefKind :: Type { object_lifetime_default, .. } => {
526+ Some ( object_lifetime_default)
527+ }
528+ GenericParamDefKind :: Const { .. } => Some ( ObjectLifetimeDefault :: Empty ) ,
529+ GenericParamDefKind :: Lifetime => None ,
530+ } )
531+ . map ( set_to_region)
532+ . collect ( )
565533 } ) ;
566534 debug ! ( ?object_lifetime_defaults) ;
567535
0 commit comments