@@ -2,84 +2,33 @@ use crate::AstConv;
2
2
use rustc_data_structures:: fx:: FxHashMap ;
3
3
use rustc_hir as hir;
4
4
use rustc_hir:: def:: { DefKind , Res } ;
5
- use rustc_hir:: def_id:: LocalDefId ;
5
+ use rustc_hir:: def_id:: { DefId , LocalDefId } ;
6
6
use rustc_hir:: hir_id:: ItemLocalId ;
7
7
use rustc_hir:: intravisit:: { self , Visitor } ;
8
8
use rustc_hir:: { GenericArg , GenericParamKind , LifetimeName , Node } ;
9
9
use rustc_middle:: bug;
10
10
use rustc_middle:: hir:: nested_filter;
11
11
use rustc_middle:: middle:: resolve_lifetime:: * ;
12
12
use rustc_middle:: ty:: { self , DefIdTree , GenericParamDefKind , TyCtxt } ;
13
- use rustc_span:: symbol:: sym;
14
- use std:: borrow:: Cow ;
15
13
16
14
use tracing:: debug;
17
15
18
- pub ( super ) fn object_lifetime_defaults (
16
+ pub ( super ) fn object_lifetime_default (
19
17
tcx : TyCtxt < ' _ > ,
20
- def_id : LocalDefId ,
21
- ) -> Option < & [ ObjectLifetimeDefault ] > {
22
- let Node :: Item ( item) = tcx. hir ( ) . get_by_def_id ( def_id) else { return None } ;
23
- match item. kind {
24
- hir:: ItemKind :: Struct ( _, ref generics)
25
- | hir:: ItemKind :: Union ( _, ref generics)
26
- | hir:: ItemKind :: Enum ( _, ref generics)
27
- | hir:: ItemKind :: OpaqueTy ( hir:: OpaqueTy {
28
- ref generics,
29
- origin : hir:: OpaqueTyOrigin :: TyAlias ,
30
- ..
31
- } )
32
- | hir:: ItemKind :: TyAlias ( _, ref generics)
33
- | hir:: ItemKind :: Trait ( _, _, ref generics, ..) => {
34
- let result = object_lifetime_defaults_for_item ( tcx, generics) ;
35
- debug ! ( ?result) ;
36
-
37
- // Debugging aid.
38
- let attrs = tcx. hir ( ) . attrs ( item. hir_id ( ) ) ;
39
- if tcx. sess . contains_name ( attrs, sym:: rustc_object_lifetime_default) {
40
- let object_lifetime_default_reprs: String = result
41
- . iter ( )
42
- . map ( |set| match * set {
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 ( ) ,
55
- } )
56
- . collect :: < Vec < Cow < ' static , str > > > ( )
57
- . join ( "," ) ;
58
- tcx. sess . span_err ( item. span , & object_lifetime_default_reprs) ;
59
- }
18
+ param_def_id : DefId ,
19
+ ) -> Option < ObjectLifetimeDefault > {
20
+ let param_def_id = param_def_id. expect_local ( ) ;
21
+ let parent_item_id = tcx. local_parent ( param_def_id) ;
22
+ let generics = tcx. hir ( ) . get_generics ( parent_item_id) ?;
60
23
61
- Some ( result)
62
- }
63
- _ => None ,
64
- }
65
- }
24
+ // Scan the bounds and where-clauses on parameters to extract bounds
25
+ // of the form `T:'a` so as to determine the `ObjectLifetimeDefault`
26
+ // for each type parameter.
66
27
67
- /// Scan the bounds and where-clauses on parameters to extract bounds
68
- /// of the form `T:'a` so as to determine the `ObjectLifetimeDefault`
69
- /// for each type parameter.
70
- fn object_lifetime_defaults_for_item < ' tcx > (
71
- tcx : TyCtxt < ' tcx > ,
72
- generics : & hir:: Generics < ' _ > ,
73
- ) -> & ' tcx [ ObjectLifetimeDefault ] {
74
- fn add_bounds ( set : & mut Set1 < hir:: LifetimeName > , bounds : & [ hir:: GenericBound < ' _ > ] ) {
75
- for bound in bounds {
76
- if let hir:: GenericBound :: Outlives ( ref lifetime) = * bound {
77
- set. insert ( lifetime. name . normalize_to_macros_2_0 ( ) ) ;
78
- }
79
- }
80
- }
28
+ let param_hir_id = tcx. hir ( ) . local_def_id_to_hir_id ( param_def_id) ;
29
+ let param = generics. params . iter ( ) . find ( |p| p. hir_id == param_hir_id) ?;
81
30
82
- let process_param = | param : & hir :: GenericParam < ' _ > | match param. kind {
31
+ match param. kind {
83
32
GenericParamKind :: Lifetime { .. } => None ,
84
33
GenericParamKind :: Type { .. } => {
85
34
let mut set = Set1 :: Empty ;
@@ -101,7 +50,11 @@ fn object_lifetime_defaults_for_item<'tcx>(
101
50
} ;
102
51
103
52
if res == Res :: Def ( DefKind :: TyParam , param_def_id. to_def_id ( ) ) {
104
- add_bounds ( & mut set, & data. bounds ) ;
53
+ for bound in data. bounds {
54
+ if let hir:: GenericBound :: Outlives ( ref lifetime) = * bound {
55
+ set. insert ( lifetime. name . normalize_to_macros_2_0 ( ) ) ;
56
+ }
57
+ }
105
58
}
106
59
}
107
60
@@ -121,9 +74,7 @@ fn object_lifetime_defaults_for_item<'tcx>(
121
74
// in an arbitrary order.
122
75
Some ( ObjectLifetimeDefault :: Empty )
123
76
}
124
- } ;
125
-
126
- tcx. arena . alloc_from_iter ( generics. params . iter ( ) . filter_map ( process_param) )
77
+ }
127
78
}
128
79
129
80
pub ( super ) fn object_lifetime_map (
@@ -466,7 +417,7 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
466
417
// Therefore, we would compute `object_lifetime_defaults` to a
467
418
// vector like `['x, 'static]`. Note that the vector only
468
419
// includes type parameters.
469
- let generics = self . tcx . generics_of ( type_def_id) ;
420
+ let generics = tcx. generics_of ( type_def_id) ;
470
421
471
422
let in_body = {
472
423
let mut scope = self . scope ;
@@ -485,9 +436,7 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
485
436
let object_lifetime_default = |i : usize | {
486
437
let param = generics. params . get ( i) ?;
487
438
match param. kind {
488
- GenericParamDefKind :: Type { object_lifetime_default, .. } => {
489
- Some ( object_lifetime_default)
490
- }
439
+ GenericParamDefKind :: Type { .. } => tcx. object_lifetime_default ( param. def_id ) ,
491
440
GenericParamDefKind :: Const { .. } => Some ( ObjectLifetimeDefault :: Empty ) ,
492
441
GenericParamDefKind :: Lifetime => return None ,
493
442
}
0 commit comments