Skip to content

Commit

Permalink
Simplify ResolveLiftimes creation.
Browse files Browse the repository at this point in the history
  • Loading branch information
cjgillot committed Dec 31, 2019
1 parent 31f85d3 commit 7e4345c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 62 deletions.
67 changes: 13 additions & 54 deletions src/librustc/middle/resolve_lifetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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:
FxHashMap<LocalDefId, FxHashMap<ItemLocalId, Vec<ObjectLifetimeDefault>>>,
}
/// 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>>,

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 }
}
/// 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>>,

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)
}
/// 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>>>,
}
26 changes: 18 additions & 8 deletions src/librustc_resolve/lifetimes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
}

Expand Down

0 comments on commit 7e4345c

Please sign in to comment.