@@ -18,11 +18,10 @@ use rustc_middle::bug;
1818use rustc_middle:: hir:: map:: Map ;
1919use rustc_middle:: hir:: nested_filter;
2020use rustc_middle:: middle:: resolve_lifetime:: * ;
21- use rustc_middle:: ty:: { self , GenericParamDefKind , TyCtxt } ;
21+ use rustc_middle:: ty:: { self , DefIdTree , TyCtxt } ;
2222use rustc_span:: def_id:: DefId ;
2323use rustc_span:: symbol:: { sym, Ident } ;
2424use rustc_span:: Span ;
25- use std:: borrow:: Cow ;
2625use std:: fmt;
2726
2827trait RegionExt {
@@ -290,7 +289,7 @@ pub fn provide(providers: &mut ty::query::Providers) {
290289
291290 named_region_map : |tcx, id| resolve_lifetimes_for ( tcx, id) . defs . get ( & id) ,
292291 is_late_bound_map,
293- object_lifetime_defaults ,
292+ object_lifetime_default ,
294293 late_bound_vars_map : |tcx, id| resolve_lifetimes_for ( tcx, id) . late_bound_vars . get ( & id) ,
295294
296295 ..* providers
@@ -1264,87 +1263,36 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
12641263 }
12651264}
12661265
1267- fn object_lifetime_defaults < ' tcx > (
1266+ fn object_lifetime_default < ' tcx > (
12681267 tcx : TyCtxt < ' tcx > ,
1269- def_id : LocalDefId ,
1270- ) -> Option < & ' tcx [ ObjectLifetimeDefault ] > {
1271- let hir:: Node :: Item ( item) = tcx. hir ( ) . get_by_def_id ( def_id) else { return None ; } ;
1272- match item. kind {
1273- hir:: ItemKind :: Struct ( _, ref generics)
1274- | hir:: ItemKind :: Union ( _, ref generics)
1275- | hir:: ItemKind :: Enum ( _, ref generics)
1276- | hir:: ItemKind :: OpaqueTy ( hir:: OpaqueTy {
1277- ref generics,
1278- origin : hir:: OpaqueTyOrigin :: TyAlias ,
1279- ..
1280- } )
1281- | hir:: ItemKind :: TyAlias ( _, ref generics)
1282- | hir:: ItemKind :: Trait ( _, _, ref generics, ..) => {
1283- let result = object_lifetime_defaults_for_item ( tcx, generics) ;
1284-
1285- // Debugging aid.
1286- let attrs = tcx. hir ( ) . attrs ( item. hir_id ( ) ) ;
1287- if tcx. sess . contains_name ( attrs, sym:: rustc_object_lifetime_default) {
1288- let object_lifetime_default_reprs: String = result
1289- . iter ( )
1290- . map ( |set| match * set {
1291- ObjectLifetimeDefault :: Empty => "BaseDefault" . into ( ) ,
1292- ObjectLifetimeDefault :: Static => "'static" . into ( ) ,
1293- ObjectLifetimeDefault :: Param ( def_id) => {
1294- let def_id = def_id. expect_local ( ) ;
1295- tcx. hir ( ) . ty_param_name ( def_id) . to_string ( ) . into ( )
1296- }
1297- ObjectLifetimeDefault :: Ambiguous => "Ambiguous" . into ( ) ,
1298- } )
1299- . collect :: < Vec < Cow < ' static , str > > > ( )
1300- . join ( "," ) ;
1301- tcx. sess . span_err ( item. span , & object_lifetime_default_reprs) ;
1302- }
1303-
1304- Some ( result)
1305- }
1306- _ => None ,
1307- }
1308- }
1309-
1310- /// Scan the bounds and where-clauses on parameters to extract bounds
1311- /// of the form `T:'a` so as to determine the `ObjectLifetimeDefault`
1312- /// for each type parameter.
1313- fn object_lifetime_defaults_for_item < ' tcx > (
1314- tcx : TyCtxt < ' tcx > ,
1315- generics : & hir:: Generics < ' _ > ,
1316- ) -> & ' tcx [ ObjectLifetimeDefault ] {
1317- fn add_bounds ( set : & mut Set1 < hir:: LifetimeName > , bounds : & [ hir:: GenericBound < ' _ > ] ) {
1318- for bound in bounds {
1319- if let hir:: GenericBound :: Outlives ( ref lifetime) = * bound {
1320- set. insert ( lifetime. name . normalize_to_macros_2_0 ( ) ) ;
1321- }
1322- }
1323- }
1324-
1325- let process_param = |param : & hir:: GenericParam < ' _ > | match param. kind {
1268+ param_def_id : DefId ,
1269+ ) -> Option < ObjectLifetimeDefault > {
1270+ let param_def_id = param_def_id. expect_local ( ) ;
1271+ let parent_def_id = tcx. local_parent ( param_def_id) ;
1272+ let generics = tcx. hir ( ) . get_generics ( parent_def_id) ?;
1273+ let param_hir_id = tcx. local_def_id_to_hir_id ( param_def_id) ;
1274+ let param = generics. params . iter ( ) . find ( |p| p. hir_id == param_hir_id) ?;
1275+
1276+ // Scan the bounds and where-clauses on parameters to extract bounds
1277+ // of the form `T:'a` so as to determine the `ObjectLifetimeDefault`
1278+ // for each type parameter.
1279+ match param. kind {
13261280 GenericParamKind :: Lifetime { .. } => None ,
13271281 GenericParamKind :: Type { .. } => {
13281282 let mut set = Set1 :: Empty ;
13291283
1330- let param_def_id = tcx. hir ( ) . local_def_id ( param. hir_id ) ;
1331- for predicate in generics. predicates {
1332- // Look for `type: ...` where clauses.
1333- let hir:: WherePredicate :: BoundPredicate ( ref data) = * predicate else { continue } ;
1334-
1284+ // Look for `type: ...` where clauses.
1285+ for bound in generics. bounds_for_param ( param_def_id) {
13351286 // Ignore `for<'a> type: ...` as they can change what
13361287 // lifetimes mean (although we could "just" handle it).
1337- if !data . bound_generic_params . is_empty ( ) {
1288+ if !bound . bound_generic_params . is_empty ( ) {
13381289 continue ;
13391290 }
13401291
1341- let res = match data. bounded_ty . kind {
1342- hir:: TyKind :: Path ( hir:: QPath :: Resolved ( None , ref path) ) => path. res ,
1343- _ => continue ,
1344- } ;
1345-
1346- if res == Res :: Def ( DefKind :: TyParam , param_def_id. to_def_id ( ) ) {
1347- add_bounds ( & mut set, & data. bounds ) ;
1292+ for bound in bound. bounds {
1293+ if let hir:: GenericBound :: Outlives ( ref lifetime) = * bound {
1294+ set. insert ( lifetime. name . normalize_to_macros_2_0 ( ) ) ;
1295+ }
13481296 }
13491297 }
13501298
@@ -1364,9 +1312,7 @@ fn object_lifetime_defaults_for_item<'tcx>(
13641312 // in an arbitrary order.
13651313 Some ( ObjectLifetimeDefault :: Empty )
13661314 }
1367- } ;
1368-
1369- tcx. arena . alloc_from_iter ( generics. params . iter ( ) . filter_map ( process_param) )
1315+ }
13701316}
13711317
13721318impl < ' a , ' tcx > LifetimeContext < ' a , ' tcx > {
@@ -1744,13 +1690,7 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
17441690 generics
17451691 . params
17461692 . iter ( )
1747- . filter_map ( |param| match param. kind {
1748- GenericParamDefKind :: Type { object_lifetime_default, .. } => {
1749- Some ( object_lifetime_default)
1750- }
1751- GenericParamDefKind :: Const { .. } => Some ( ObjectLifetimeDefault :: Empty ) ,
1752- GenericParamDefKind :: Lifetime => None ,
1753- } )
1693+ . filter_map ( |param| self . tcx . object_lifetime_default ( param. def_id ) )
17541694 . map ( set_to_region)
17551695 . collect ( )
17561696 } ) ;
0 commit comments