Skip to content

Commit 7e4345c

Browse files
committed
Simplify ResolveLiftimes creation.
1 parent 31f85d3 commit 7e4345c

File tree

2 files changed

+31
-62
lines changed

2 files changed

+31
-62
lines changed

src/librustc/middle/resolve_lifetime.rs

+13-54
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::hir::{GenericParam, ItemLocalId};
55
use crate::hir::{GenericParamKind, LifetimeParamKind};
66
use crate::ty;
77

8-
use crate::util::nodemap::{FxHashMap, FxHashSet, HirIdMap, HirIdSet};
8+
use crate::util::nodemap::{FxHashMap, FxHashSet};
99
use rustc_macros::HashStable;
1010

1111
/// The origin of a named lifetime definition.
@@ -68,60 +68,19 @@ pub type ObjectLifetimeDefault = Set1<Region>;
6868

6969
/// Maps the id of each lifetime reference to the lifetime decl
7070
/// that it corresponds to.
71-
#[derive(HashStable)]
71+
#[derive(Default, HashStable)]
7272
pub struct ResolveLifetimes {
73-
defs: FxHashMap<LocalDefId, FxHashMap<ItemLocalId, Region>>,
74-
late_bound: FxHashMap<LocalDefId, FxHashSet<ItemLocalId>>,
75-
object_lifetime_defaults:
76-
FxHashMap<LocalDefId, FxHashMap<ItemLocalId, Vec<ObjectLifetimeDefault>>>,
77-
}
73+
/// Maps from every use of a named (not anonymous) lifetime to a
74+
/// `Region` describing how that region is bound
75+
pub defs: FxHashMap<LocalDefId, FxHashMap<ItemLocalId, Region>>,
7876

79-
impl ResolveLifetimes {
80-
pub fn new(
81-
defs: HirIdMap<Region>,
82-
late_bound: HirIdSet,
83-
object_lifetime_defaults: HirIdMap<Vec<ObjectLifetimeDefault>>,
84-
) -> Self {
85-
let defs = {
86-
let mut map = FxHashMap::<_, FxHashMap<_, _>>::default();
87-
for (hir_id, v) in defs {
88-
let map = map.entry(hir_id.owner_local_def_id()).or_default();
89-
map.insert(hir_id.local_id, v);
90-
}
91-
map
92-
};
93-
let late_bound = {
94-
let mut map = FxHashMap::<_, FxHashSet<_>>::default();
95-
for hir_id in late_bound {
96-
let map = map.entry(hir_id.owner_local_def_id()).or_default();
97-
map.insert(hir_id.local_id);
98-
}
99-
map
100-
};
101-
let object_lifetime_defaults = {
102-
let mut map = FxHashMap::<_, FxHashMap<_, _>>::default();
103-
for (hir_id, v) in object_lifetime_defaults {
104-
let map = map.entry(hir_id.owner_local_def_id()).or_default();
105-
map.insert(hir_id.local_id, v);
106-
}
107-
map
108-
};
109-
110-
Self { defs, late_bound, object_lifetime_defaults }
111-
}
77+
/// Set of lifetime def ids that are late-bound; a region can
78+
/// be late-bound if (a) it does NOT appear in a where-clause and
79+
/// (b) it DOES appear in the arguments.
80+
pub late_bound: FxHashMap<LocalDefId, FxHashSet<ItemLocalId>>,
11281

113-
pub fn named_region_map(&self, id: &LocalDefId) -> Option<&FxHashMap<ItemLocalId, Region>> {
114-
self.defs.get(id)
115-
}
116-
117-
pub fn is_late_bound_map(&self, id: &LocalDefId) -> Option<&FxHashSet<ItemLocalId>> {
118-
self.late_bound.get(id)
119-
}
120-
121-
pub fn object_lifetime_defaults_map(
122-
&self,
123-
id: &LocalDefId,
124-
) -> Option<&FxHashMap<ItemLocalId, Vec<ObjectLifetimeDefault>>> {
125-
self.object_lifetime_defaults.get(id)
126-
}
82+
/// For each type and trait definition, maps type parameters
83+
/// to the trait object lifetime defaults computed from them.
84+
pub object_lifetime_defaults:
85+
FxHashMap<LocalDefId, FxHashMap<ItemLocalId, Vec<ObjectLifetimeDefault>>>,
12786
}

src/librustc_resolve/lifetimes.rs

+18-8
Original file line numberDiff line numberDiff line change
@@ -280,17 +280,17 @@ pub fn provide(providers: &mut ty::query::Providers<'_>) {
280280

281281
named_region_map: |tcx, id| {
282282
let id = LocalDefId::from_def_id(DefId::local(id)); // (*)
283-
tcx.resolve_lifetimes(LOCAL_CRATE).named_region_map(&id)
283+
tcx.resolve_lifetimes(LOCAL_CRATE).defs.get(&id)
284284
},
285285

286286
is_late_bound_map: |tcx, id| {
287287
let id = LocalDefId::from_def_id(DefId::local(id)); // (*)
288-
tcx.resolve_lifetimes(LOCAL_CRATE).is_late_bound_map(&id)
288+
tcx.resolve_lifetimes(LOCAL_CRATE).late_bound.get(&id)
289289
},
290290

291291
object_lifetime_defaults_map: |tcx, id| {
292292
let id = LocalDefId::from_def_id(DefId::local(id)); // (*)
293-
tcx.resolve_lifetimes(LOCAL_CRATE).object_lifetime_defaults_map(&id)
293+
tcx.resolve_lifetimes(LOCAL_CRATE).object_lifetime_defaults.get(&id)
294294
},
295295

296296
..*providers
@@ -308,11 +308,21 @@ fn resolve_lifetimes(tcx: TyCtxt<'_>, for_krate: CrateNum) -> &ResolveLifetimes
308308

309309
let named_region_map = krate(tcx);
310310

311-
let rl = ResolveLifetimes::new(
312-
named_region_map.defs,
313-
named_region_map.late_bound,
314-
named_region_map.object_lifetime_defaults,
315-
);
311+
let mut rl = ResolveLifetimes::default();
312+
313+
for (hir_id, v) in named_region_map.defs {
314+
let map = rl.defs.entry(hir_id.owner_local_def_id()).or_default();
315+
map.insert(hir_id.local_id, v);
316+
}
317+
for hir_id in named_region_map.late_bound {
318+
let map = rl.late_bound.entry(hir_id.owner_local_def_id()).or_default();
319+
map.insert(hir_id.local_id);
320+
}
321+
for (hir_id, v) in named_region_map.object_lifetime_defaults {
322+
let map = rl.object_lifetime_defaults.entry(hir_id.owner_local_def_id()).or_default();
323+
map.insert(hir_id.local_id, v);
324+
}
325+
316326
tcx.arena.alloc(rl)
317327
}
318328

0 commit comments

Comments
 (0)