@@ -17,30 +17,9 @@ use tracing::debug;
17
17
18
18
pub ( super ) fn object_lifetime_defaults (
19
19
tcx : TyCtxt < ' _ > ,
20
- def_id : DefId ,
20
+ def_id : LocalDefId ,
21
21
) -> 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 } ;
44
23
match item. kind {
45
24
hir:: ItemKind :: Struct ( _, ref generics)
46
25
| hir:: ItemKind :: Union ( _, ref generics)
@@ -61,24 +40,18 @@ fn compute_object_lifetime_defaults<'tcx>(
61
40
let object_lifetime_default_reprs: String = result
62
41
. iter ( )
63
42
. 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 ( ) ,
82
55
} )
83
56
. collect :: < Vec < Cow < ' static , str > > > ( )
84
57
. join ( "," ) ;
@@ -133,32 +106,20 @@ fn object_lifetime_defaults_for_item<'tcx>(
133
106
}
134
107
135
108
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 ,
154
115
} )
155
116
}
156
117
GenericParamKind :: Const { .. } => {
157
118
// Generic consts don't impose any constraints.
158
119
//
159
120
// We still store a dummy value here to allow generic parameters
160
121
// in an arbitrary order.
161
- Some ( Set1 :: Empty )
122
+ Some ( ObjectLifetimeDefault :: Empty )
162
123
}
163
124
} ;
164
125
@@ -529,9 +490,7 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
529
490
loop {
530
491
match * scope {
531
492
Scope :: Root => break false ,
532
-
533
493
Scope :: Body => break true ,
534
-
535
494
Scope :: Binder { s, .. }
536
495
| Scope :: Static { s, .. }
537
496
| Scope :: ObjectLifetimeDefault { s, .. } => {
@@ -540,28 +499,37 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
540
499
}
541
500
}
542
501
} ;
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 => {
546
505
if in_body {
547
506
None
548
507
} else {
549
508
Some ( Region :: Static )
550
509
}
551
510
}
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 ) ,
555
516
_ => None ,
556
- } ) ;
557
- lifetimes
558
- . nth ( index as usize )
559
- . and_then ( |lifetime| self . tcx . named_region ( lifetime. hir_id ) )
517
+ } )
560
518
}
561
- Set1 :: One ( r) => Some ( r) ,
562
- Set1 :: Many => None ,
519
+ ObjectLifetimeDefault :: Ambiguous => None ,
563
520
} ;
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 ( )
565
533
} ) ;
566
534
debug ! ( ?object_lifetime_defaults) ;
567
535
0 commit comments