@@ -2,84 +2,33 @@ use crate::AstConv;
22use rustc_data_structures:: fx:: FxHashMap ;
33use rustc_hir as hir;
44use rustc_hir:: def:: { DefKind , Res } ;
5- use rustc_hir:: def_id:: LocalDefId ;
5+ use rustc_hir:: def_id:: { DefId , LocalDefId } ;
66use rustc_hir:: hir_id:: ItemLocalId ;
77use rustc_hir:: intravisit:: { self , Visitor } ;
88use rustc_hir:: { GenericArg , GenericParamKind , LifetimeName , Node } ;
99use rustc_middle:: bug;
1010use rustc_middle:: hir:: nested_filter;
1111use rustc_middle:: middle:: resolve_lifetime:: * ;
1212use rustc_middle:: ty:: { self , DefIdTree , GenericParamDefKind , TyCtxt } ;
13- use rustc_span:: symbol:: sym;
14- use std:: borrow:: Cow ;
1513
1614use tracing:: debug;
1715
18- pub ( super ) fn object_lifetime_defaults (
16+ pub ( super ) fn object_lifetime_default (
1917 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) ?;
6023
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.
6627
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) ?;
8130
82- let process_param = | param : & hir :: GenericParam < ' _ > | match param. kind {
31+ match param. kind {
8332 GenericParamKind :: Lifetime { .. } => None ,
8433 GenericParamKind :: Type { .. } => {
8534 let mut set = Set1 :: Empty ;
@@ -101,7 +50,11 @@ fn object_lifetime_defaults_for_item<'tcx>(
10150 } ;
10251
10352 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+ }
10558 }
10659 }
10760
@@ -121,9 +74,7 @@ fn object_lifetime_defaults_for_item<'tcx>(
12174 // in an arbitrary order.
12275 Some ( ObjectLifetimeDefault :: Empty )
12376 }
124- } ;
125-
126- tcx. arena . alloc_from_iter ( generics. params . iter ( ) . filter_map ( process_param) )
77+ }
12778}
12879
12980pub ( super ) fn object_lifetime_map (
@@ -466,7 +417,7 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
466417 // Therefore, we would compute `object_lifetime_defaults` to a
467418 // vector like `['x, 'static]`. Note that the vector only
468419 // includes type parameters.
469- let generics = self . tcx . generics_of ( type_def_id) ;
420+ let generics = tcx. generics_of ( type_def_id) ;
470421
471422 let in_body = {
472423 let mut scope = self . scope ;
@@ -485,9 +436,7 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
485436 let object_lifetime_default = |i : usize | {
486437 let param = generics. params . get ( i) ?;
487438 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 ) ,
491440 GenericParamDefKind :: Const { .. } => Some ( ObjectLifetimeDefault :: Empty ) ,
492441 GenericParamDefKind :: Lifetime => return None ,
493442 }
0 commit comments