Skip to content

Commit 236ccce

Browse files
committed
Create a specific ObjectLifetimeDefault enum.
1 parent 39bc74e commit 236ccce

File tree

6 files changed

+58
-83
lines changed

6 files changed

+58
-83
lines changed

compiler/rustc_middle/src/hir/map/mod.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -499,7 +499,9 @@ impl<'hir> Map<'hir> {
499499
let def_kind = self.tcx.def_kind(def_id);
500500
match def_kind {
501501
DefKind::Trait | DefKind::TraitAlias => def_id,
502-
DefKind::TyParam | DefKind::ConstParam => self.tcx.local_parent(def_id),
502+
DefKind::LifetimeParam | DefKind::TyParam | DefKind::ConstParam => {
503+
self.tcx.local_parent(def_id)
504+
}
503505
_ => bug!("ty_param_owner: {:?} is a {:?} not a type parameter", def_id, def_kind),
504506
}
505507
}
@@ -508,7 +510,9 @@ impl<'hir> Map<'hir> {
508510
let def_kind = self.tcx.def_kind(def_id);
509511
match def_kind {
510512
DefKind::Trait | DefKind::TraitAlias => kw::SelfUpper,
511-
DefKind::TyParam | DefKind::ConstParam => self.tcx.item_name(def_id.to_def_id()),
513+
DefKind::LifetimeParam | DefKind::TyParam | DefKind::ConstParam => {
514+
self.tcx.item_name(def_id.to_def_id())
515+
}
512516
_ => bug!("ty_param_name: {:?} is a {:?} not a type parameter", def_id, def_kind),
513517
}
514518
}

compiler/rustc_middle/src/middle/resolve_lifetime.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,13 @@ impl<T: PartialEq> Set1<T> {
3535
}
3636
}
3737

38-
pub type ObjectLifetimeDefault = Set1<Region>;
38+
#[derive(Copy, Clone, Debug, HashStable, Encodable, Decodable)]
39+
pub enum ObjectLifetimeDefault {
40+
Empty,
41+
Static,
42+
Ambiguous,
43+
Param(DefId),
44+
}
3945

4046
/// Maps the id of each lifetime reference to the lifetime decl
4147
/// that it corresponds to.

compiler/rustc_middle/src/query/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1579,7 +1579,7 @@ rustc_queries! {
15791579
/// for each parameter if a trait object were to be passed for that parameter.
15801580
/// For example, for `struct Foo<'a, T, U>`, this would be `['static, 'static]`.
15811581
/// For `struct Foo<'a, T: 'a, U>`, this would instead be `['a, 'static]`.
1582-
query object_lifetime_defaults(_: DefId) -> Option<&'tcx [ObjectLifetimeDefault]> {
1582+
query object_lifetime_defaults(_: LocalDefId) -> Option<&'tcx [ObjectLifetimeDefault]> {
15831583
desc { "looking up lifetime defaults for a region on an item" }
15841584
}
15851585
query late_bound_vars_map(_: LocalDefId)

compiler/rustc_resolve/src/late/lifetimes.rs

Lines changed: 39 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -290,24 +290,7 @@ pub fn provide(providers: &mut ty::query::Providers) {
290290

291291
named_region_map: |tcx, id| resolve_lifetimes_for(tcx, id).defs.get(&id),
292292
is_late_bound_map,
293-
object_lifetime_defaults: |tcx, def_id| {
294-
if let Some(def_id) = def_id.as_local() {
295-
match tcx.hir().get_by_def_id(def_id) {
296-
Node::Item(item) => compute_object_lifetime_defaults(tcx, item),
297-
_ => None,
298-
}
299-
} else {
300-
Some(tcx.arena.alloc_from_iter(tcx.generics_of(def_id).params.iter().filter_map(
301-
|param| match param.kind {
302-
GenericParamDefKind::Type { object_lifetime_default, .. } => {
303-
Some(object_lifetime_default)
304-
}
305-
GenericParamDefKind::Const { .. } => Some(Set1::Empty),
306-
GenericParamDefKind::Lifetime => None,
307-
},
308-
)))
309-
}
310-
},
293+
object_lifetime_defaults,
311294
late_bound_vars_map: |tcx, id| resolve_lifetimes_for(tcx, id).late_bound_vars.get(&id),
312295

313296
..*providers
@@ -1281,10 +1264,11 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
12811264
}
12821265
}
12831266

1284-
fn compute_object_lifetime_defaults<'tcx>(
1267+
fn object_lifetime_defaults<'tcx>(
12851268
tcx: TyCtxt<'tcx>,
1286-
item: &hir::Item<'_>,
1269+
def_id: LocalDefId,
12871270
) -> Option<&'tcx [ObjectLifetimeDefault]> {
1271+
let hir::Node::Item(item) = tcx.hir().get_by_def_id(def_id) else { return None; };
12881272
match item.kind {
12891273
hir::ItemKind::Struct(_, ref generics)
12901274
| hir::ItemKind::Union(_, ref generics)
@@ -1304,24 +1288,13 @@ fn compute_object_lifetime_defaults<'tcx>(
13041288
let object_lifetime_default_reprs: String = result
13051289
.iter()
13061290
.map(|set| match *set {
1307-
Set1::Empty => "BaseDefault".into(),
1308-
Set1::One(Region::Static) => "'static".into(),
1309-
Set1::One(Region::EarlyBound(mut i, _)) => generics
1310-
.params
1311-
.iter()
1312-
.find_map(|param| match param.kind {
1313-
GenericParamKind::Lifetime { .. } => {
1314-
if i == 0 {
1315-
return Some(param.name.ident().to_string().into());
1316-
}
1317-
i -= 1;
1318-
None
1319-
}
1320-
_ => None,
1321-
})
1322-
.unwrap(),
1323-
Set1::One(_) => bug!(),
1324-
Set1::Many => "Ambiguous".into(),
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(),
13251298
})
13261299
.collect::<Vec<Cow<'static, str>>>()
13271300
.join(",");
@@ -1376,40 +1349,20 @@ fn object_lifetime_defaults_for_item<'tcx>(
13761349
}
13771350

13781351
Some(match set {
1379-
Set1::Empty => Set1::Empty,
1380-
Set1::One(name) => {
1381-
if name == hir::LifetimeName::Static {
1382-
Set1::One(Region::Static)
1383-
} else {
1384-
generics
1385-
.params
1386-
.iter()
1387-
.filter_map(|param| match param.kind {
1388-
GenericParamKind::Lifetime { .. } => {
1389-
let param_def_id = tcx.hir().local_def_id(param.hir_id);
1390-
Some((
1391-
param_def_id,
1392-
hir::LifetimeName::Param(param_def_id, param.name),
1393-
))
1394-
}
1395-
_ => None,
1396-
})
1397-
.enumerate()
1398-
.find(|&(_, (_, lt_name))| lt_name == name)
1399-
.map_or(Set1::Many, |(i, (def_id, _))| {
1400-
Set1::One(Region::EarlyBound(i as u32, def_id.to_def_id()))
1401-
})
1402-
}
1352+
Set1::Empty => ObjectLifetimeDefault::Empty,
1353+
Set1::One(hir::LifetimeName::Static) => ObjectLifetimeDefault::Static,
1354+
Set1::One(hir::LifetimeName::Param(param_def_id, _)) => {
1355+
ObjectLifetimeDefault::Param(param_def_id.to_def_id())
14031356
}
1404-
Set1::Many => Set1::Many,
1357+
_ => ObjectLifetimeDefault::Ambiguous,
14051358
})
14061359
}
14071360
GenericParamKind::Const { .. } => {
14081361
// Generic consts don't impose any constraints.
14091362
//
14101363
// We still store a dummy value here to allow generic parameters
14111364
// in an arbitrary order.
1412-
Some(Set1::Empty)
1365+
Some(ObjectLifetimeDefault::Empty)
14131366
}
14141367
};
14151368

@@ -1769,24 +1722,37 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
17691722
};
17701723

17711724
let map = &self.map;
1772-
let set_to_region = |set: &ObjectLifetimeDefault| match *set {
1773-
Set1::Empty => {
1725+
let generics = self.tcx.generics_of(def_id);
1726+
let set_to_region = |set: ObjectLifetimeDefault| match set {
1727+
ObjectLifetimeDefault::Empty => {
17741728
if in_body {
17751729
None
17761730
} else {
17771731
Some(Region::Static)
17781732
}
17791733
}
1780-
Set1::One(r) => {
1781-
let lifetimes = generic_args.args.iter().filter_map(|arg| match arg {
1782-
GenericArg::Lifetime(lt) => Some(lt),
1734+
ObjectLifetimeDefault::Static => Some(Region::Static),
1735+
ObjectLifetimeDefault::Param(param_def_id) => {
1736+
let index = generics.param_def_id_to_index[&param_def_id];
1737+
generic_args.args.get(index as usize).and_then(|arg| match arg {
1738+
GenericArg::Lifetime(lt) => map.defs.get(&lt.hir_id).copied(),
17831739
_ => None,
1784-
});
1785-
r.subst(lifetimes, map)
1740+
})
17861741
}
1787-
Set1::Many => None,
1742+
ObjectLifetimeDefault::Ambiguous => None,
17881743
};
1789-
self.tcx.object_lifetime_defaults(def_id).unwrap().iter().map(set_to_region).collect()
1744+
generics
1745+
.params
1746+
.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+
})
1754+
.map(set_to_region)
1755+
.collect()
17901756
});
17911757

17921758
debug!("visit_segment_args: object_lifetime_defaults={:?}", object_lifetime_defaults);

compiler/rustc_typeck/src/astconv/mod.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -252,9 +252,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
252252
})
253253
}
254254
};
255-
256255
debug!("ast_region_to_region(lifetime={:?}) yields {:?}", lifetime, r);
257-
258256
r
259257
}
260258

compiler/rustc_typeck/src/collect.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ use rustc_hir::weak_lang_items;
3434
use rustc_hir::{GenericParamKind, HirId, Node};
3535
use rustc_middle::hir::nested_filter;
3636
use rustc_middle::middle::codegen_fn_attrs::{CodegenFnAttrFlags, CodegenFnAttrs};
37+
use rustc_middle::middle::resolve_lifetime::ObjectLifetimeDefault;
3738
use rustc_middle::mir::mono::Linkage;
3839
use rustc_middle::ty::query::Providers;
3940
use rustc_middle::ty::subst::InternalSubsts;
@@ -1597,7 +1598,7 @@ fn generics_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::Generics {
15971598
pure_wrt_drop: false,
15981599
kind: ty::GenericParamDefKind::Type {
15991600
has_default: false,
1600-
object_lifetime_default: rl::Set1::Empty,
1601+
object_lifetime_default: ObjectLifetimeDefault::Empty,
16011602
synthetic: false,
16021603
},
16031604
});
@@ -1671,7 +1672,7 @@ fn generics_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::Generics {
16711672
has_default: default.is_some(),
16721673
object_lifetime_default: object_lifetime_defaults
16731674
.as_ref()
1674-
.map_or(rl::Set1::Empty, |o| o[i]),
1675+
.map_or(ObjectLifetimeDefault::Empty, |o| o[i]),
16751676
synthetic,
16761677
};
16771678

@@ -1727,7 +1728,7 @@ fn generics_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::Generics {
17271728
pure_wrt_drop: false,
17281729
kind: ty::GenericParamDefKind::Type {
17291730
has_default: false,
1730-
object_lifetime_default: rl::Set1::Empty,
1731+
object_lifetime_default: ObjectLifetimeDefault::Empty,
17311732
synthetic: false,
17321733
},
17331734
}));
@@ -1744,7 +1745,7 @@ fn generics_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::Generics {
17441745
pure_wrt_drop: false,
17451746
kind: ty::GenericParamDefKind::Type {
17461747
has_default: false,
1747-
object_lifetime_default: rl::Set1::Empty,
1748+
object_lifetime_default: ObjectLifetimeDefault::Empty,
17481749
synthetic: false,
17491750
},
17501751
});

0 commit comments

Comments
 (0)