@@ -18,11 +18,10 @@ use rustc_middle::bug;
18
18
use rustc_middle:: hir:: map:: Map ;
19
19
use rustc_middle:: hir:: nested_filter;
20
20
use rustc_middle:: middle:: resolve_lifetime:: * ;
21
- use rustc_middle:: ty:: { self , GenericParamDefKind , TyCtxt } ;
21
+ use rustc_middle:: ty:: { self , DefIdTree , TyCtxt } ;
22
22
use rustc_span:: def_id:: DefId ;
23
23
use rustc_span:: symbol:: { sym, Ident } ;
24
24
use rustc_span:: Span ;
25
- use std:: borrow:: Cow ;
26
25
use std:: fmt;
27
26
28
27
trait RegionExt {
@@ -290,7 +289,7 @@ pub fn provide(providers: &mut ty::query::Providers) {
290
289
291
290
named_region_map : |tcx, id| resolve_lifetimes_for ( tcx, id) . defs . get ( & id) ,
292
291
is_late_bound_map,
293
- object_lifetime_defaults ,
292
+ object_lifetime_default ,
294
293
late_bound_vars_map : |tcx, id| resolve_lifetimes_for ( tcx, id) . late_bound_vars . get ( & id) ,
295
294
296
295
..* providers
@@ -1264,87 +1263,36 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
1264
1263
}
1265
1264
}
1266
1265
1267
- fn object_lifetime_defaults < ' tcx > (
1266
+ fn object_lifetime_default < ' tcx > (
1268
1267
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 {
1326
1280
GenericParamKind :: Lifetime { .. } => None ,
1327
1281
GenericParamKind :: Type { .. } => {
1328
1282
let mut set = Set1 :: Empty ;
1329
1283
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) {
1335
1286
// Ignore `for<'a> type: ...` as they can change what
1336
1287
// lifetimes mean (although we could "just" handle it).
1337
- if !data . bound_generic_params . is_empty ( ) {
1288
+ if !bound . bound_generic_params . is_empty ( ) {
1338
1289
continue ;
1339
1290
}
1340
1291
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
+ }
1348
1296
}
1349
1297
}
1350
1298
@@ -1364,9 +1312,7 @@ fn object_lifetime_defaults_for_item<'tcx>(
1364
1312
// in an arbitrary order.
1365
1313
Some ( ObjectLifetimeDefault :: Empty )
1366
1314
}
1367
- } ;
1368
-
1369
- tcx. arena . alloc_from_iter ( generics. params . iter ( ) . filter_map ( process_param) )
1315
+ }
1370
1316
}
1371
1317
1372
1318
impl < ' a , ' tcx > LifetimeContext < ' a , ' tcx > {
@@ -1744,13 +1690,7 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
1744
1690
generics
1745
1691
. params
1746
1692
. 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 ) )
1754
1694
. map ( set_to_region)
1755
1695
. collect ( )
1756
1696
} ) ;
0 commit comments