Simplify ResolveLiftimes creation.

This commit is contained in:
Camille GILLOT 2019-12-30 12:48:32 +01:00
parent 31f85d3fe2
commit 7e4345ca79
2 changed files with 32 additions and 63 deletions

View File

@ -5,7 +5,7 @@ use crate::hir::{GenericParam, ItemLocalId};
use crate::hir::{GenericParamKind, LifetimeParamKind};
use crate::ty;
use crate::util::nodemap::{FxHashMap, FxHashSet, HirIdMap, HirIdSet};
use crate::util::nodemap::{FxHashMap, FxHashSet};
use rustc_macros::HashStable;
/// The origin of a named lifetime definition.
@ -68,60 +68,19 @@ pub type ObjectLifetimeDefault = Set1<Region>;
/// Maps the id of each lifetime reference to the lifetime decl
/// that it corresponds to.
#[derive(HashStable)]
#[derive(Default, HashStable)]
pub struct ResolveLifetimes {
defs: FxHashMap<LocalDefId, FxHashMap<ItemLocalId, Region>>,
late_bound: FxHashMap<LocalDefId, FxHashSet<ItemLocalId>>,
object_lifetime_defaults:
/// Maps from every use of a named (not anonymous) lifetime to a
/// `Region` describing how that region is bound
pub defs: FxHashMap<LocalDefId, FxHashMap<ItemLocalId, Region>>,
/// Set of lifetime def ids that are late-bound; a region can
/// be late-bound if (a) it does NOT appear in a where-clause and
/// (b) it DOES appear in the arguments.
pub late_bound: FxHashMap<LocalDefId, FxHashSet<ItemLocalId>>,
/// For each type and trait definition, maps type parameters
/// to the trait object lifetime defaults computed from them.
pub object_lifetime_defaults:
FxHashMap<LocalDefId, FxHashMap<ItemLocalId, Vec<ObjectLifetimeDefault>>>,
}
impl ResolveLifetimes {
pub fn new(
defs: HirIdMap<Region>,
late_bound: HirIdSet,
object_lifetime_defaults: HirIdMap<Vec<ObjectLifetimeDefault>>,
) -> Self {
let defs = {
let mut map = FxHashMap::<_, FxHashMap<_, _>>::default();
for (hir_id, v) in defs {
let map = map.entry(hir_id.owner_local_def_id()).or_default();
map.insert(hir_id.local_id, v);
}
map
};
let late_bound = {
let mut map = FxHashMap::<_, FxHashSet<_>>::default();
for hir_id in late_bound {
let map = map.entry(hir_id.owner_local_def_id()).or_default();
map.insert(hir_id.local_id);
}
map
};
let object_lifetime_defaults = {
let mut map = FxHashMap::<_, FxHashMap<_, _>>::default();
for (hir_id, v) in object_lifetime_defaults {
let map = map.entry(hir_id.owner_local_def_id()).or_default();
map.insert(hir_id.local_id, v);
}
map
};
Self { defs, late_bound, object_lifetime_defaults }
}
pub fn named_region_map(&self, id: &LocalDefId) -> Option<&FxHashMap<ItemLocalId, Region>> {
self.defs.get(id)
}
pub fn is_late_bound_map(&self, id: &LocalDefId) -> Option<&FxHashSet<ItemLocalId>> {
self.late_bound.get(id)
}
pub fn object_lifetime_defaults_map(
&self,
id: &LocalDefId,
) -> Option<&FxHashMap<ItemLocalId, Vec<ObjectLifetimeDefault>>> {
self.object_lifetime_defaults.get(id)
}
}

View File

@ -280,17 +280,17 @@ pub fn provide(providers: &mut ty::query::Providers<'_>) {
named_region_map: |tcx, id| {
let id = LocalDefId::from_def_id(DefId::local(id)); // (*)
tcx.resolve_lifetimes(LOCAL_CRATE).named_region_map(&id)
tcx.resolve_lifetimes(LOCAL_CRATE).defs.get(&id)
},
is_late_bound_map: |tcx, id| {
let id = LocalDefId::from_def_id(DefId::local(id)); // (*)
tcx.resolve_lifetimes(LOCAL_CRATE).is_late_bound_map(&id)
tcx.resolve_lifetimes(LOCAL_CRATE).late_bound.get(&id)
},
object_lifetime_defaults_map: |tcx, id| {
let id = LocalDefId::from_def_id(DefId::local(id)); // (*)
tcx.resolve_lifetimes(LOCAL_CRATE).object_lifetime_defaults_map(&id)
tcx.resolve_lifetimes(LOCAL_CRATE).object_lifetime_defaults.get(&id)
},
..*providers
@ -308,11 +308,21 @@ fn resolve_lifetimes(tcx: TyCtxt<'_>, for_krate: CrateNum) -> &ResolveLifetimes
let named_region_map = krate(tcx);
let rl = ResolveLifetimes::new(
named_region_map.defs,
named_region_map.late_bound,
named_region_map.object_lifetime_defaults,
);
let mut rl = ResolveLifetimes::default();
for (hir_id, v) in named_region_map.defs {
let map = rl.defs.entry(hir_id.owner_local_def_id()).or_default();
map.insert(hir_id.local_id, v);
}
for hir_id in named_region_map.late_bound {
let map = rl.late_bound.entry(hir_id.owner_local_def_id()).or_default();
map.insert(hir_id.local_id);
}
for (hir_id, v) in named_region_map.object_lifetime_defaults {
let map = rl.object_lifetime_defaults.entry(hir_id.owner_local_def_id()).or_default();
map.insert(hir_id.local_id, v);
}
tcx.arena.alloc(rl)
}