Skip to content

Commit fd61fa5

Browse files
committed
rustc: Hide the named_region_map behind queries
This commit makes the `named_region_map` field of `GlobalCtxt` private by encapsulating the fields behind new queries, and the new queries are also targeted at particular `HirId` nodes instead of accessing the entire map.
1 parent 64a7034 commit fd61fa5

File tree

7 files changed

+95
-41
lines changed

7 files changed

+95
-41
lines changed

src/librustc/dep_graph/dep_node.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,10 @@ define_dep_nodes!( <'tcx>
550550
[] IsStaticallyIncludedForeignItem(DefId),
551551
[] NativeLibraryKind(DefId),
552552
[] LinkArgs,
553+
554+
[] NamedRegion(HirId),
555+
[] IsLateBound(HirId),
556+
[] ObjectLifetimeDefaults(HirId),
553557
);
554558

555559
trait DepNodeParams<'a, 'gcx: 'tcx + 'a, 'tcx: 'a> : fmt::Debug {

src/librustc/infer/error_reporting/anon_anon_conflict.rs

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -209,18 +209,19 @@ impl<'a, 'gcx, 'tcx> Visitor<'gcx> for FindNestedTypeVisitor<'a, 'gcx, 'tcx> {
209209

210210
match arg.node {
211211
hir::TyRptr(ref lifetime, _) => {
212-
match self.infcx.tcx.named_region_map.defs.get(&lifetime.id) {
212+
let hir_id = self.infcx.tcx.hir.node_to_hir_id(lifetime.id);
213+
match self.infcx.tcx.named_region(hir_id) {
213214
// the lifetime of the TyRptr
214-
Some(&rl::Region::LateBoundAnon(debruijn_index, anon_index)) => {
215+
Some(rl::Region::LateBoundAnon(debruijn_index, anon_index)) => {
215216
if debruijn_index.depth == 1 && anon_index == br_index {
216217
self.found_type = Some(arg);
217218
return; // we can stop visiting now
218219
}
219220
}
220-
Some(&rl::Region::Static) |
221-
Some(&rl::Region::EarlyBound(_, _)) |
222-
Some(&rl::Region::LateBound(_, _)) |
223-
Some(&rl::Region::Free(_, _)) |
221+
Some(rl::Region::Static) |
222+
Some(rl::Region::EarlyBound(_, _)) |
223+
Some(rl::Region::LateBound(_, _)) |
224+
Some(rl::Region::Free(_, _)) |
224225
None => {
225226
debug!("no arg found");
226227
}
@@ -272,17 +273,18 @@ impl<'a, 'gcx, 'tcx> Visitor<'gcx> for TyPathVisitor<'a, 'gcx, 'tcx> {
272273
_ => return,
273274
};
274275

275-
match self.infcx.tcx.named_region_map.defs.get(&lifetime.id) {
276+
let hir_id = self.infcx.tcx.hir.node_to_hir_id(lifetime.id);
277+
match self.infcx.tcx.named_region(hir_id) {
276278
// the lifetime of the TyPath!
277-
Some(&rl::Region::LateBoundAnon(debruijn_index, anon_index)) => {
279+
Some(rl::Region::LateBoundAnon(debruijn_index, anon_index)) => {
278280
if debruijn_index.depth == 1 && anon_index == br_index {
279281
self.found_it = true;
280282
}
281283
}
282-
Some(&rl::Region::Static) |
283-
Some(&rl::Region::EarlyBound(_, _)) |
284-
Some(&rl::Region::LateBound(_, _)) |
285-
Some(&rl::Region::Free(_, _)) |
284+
Some(rl::Region::Static) |
285+
Some(rl::Region::EarlyBound(_, _)) |
286+
Some(rl::Region::LateBound(_, _)) |
287+
Some(rl::Region::Free(_, _)) |
286288
None => {
287289
debug!("no arg found");
288290
}

src/librustc/ty/context.rs

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use lint::{self, Lint};
2323
use ich::{self, StableHashingContext, NodeIdHashingMode};
2424
use middle::free_region::FreeRegionMap;
2525
use middle::lang_items;
26-
use middle::resolve_lifetime;
26+
use middle::resolve_lifetime::{self, ObjectLifetimeDefault};
2727
use middle::stability;
2828
use mir::Mir;
2929
use mir::transform::Passes;
@@ -822,7 +822,7 @@ pub struct GlobalCtxt<'tcx> {
822822
/// Export map produced by name resolution.
823823
export_map: FxHashMap<HirId, Rc<Vec<Export>>>,
824824

825-
pub named_region_map: resolve_lifetime::NamedRegionMap,
825+
named_region_map: NamedRegionMap,
826826

827827
pub hir: hir_map::Map<'tcx>,
828828

@@ -1054,7 +1054,23 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
10541054
global_interners: interners,
10551055
dep_graph: dep_graph.clone(),
10561056
types: common_types,
1057-
named_region_map,
1057+
named_region_map: NamedRegionMap {
1058+
defs:
1059+
named_region_map.defs
1060+
.into_iter()
1061+
.map(|(k, v)| (hir.node_to_hir_id(k), v))
1062+
.collect(),
1063+
late_bound:
1064+
named_region_map.late_bound
1065+
.into_iter()
1066+
.map(|k| hir.node_to_hir_id(k))
1067+
.collect(),
1068+
object_lifetime_defaults:
1069+
named_region_map.object_lifetime_defaults
1070+
.into_iter()
1071+
.map(|(k, v)| (hir.node_to_hir_id(k), Rc::new(v)))
1072+
.collect(),
1073+
},
10581074
trait_map: resolutions.trait_map.into_iter().map(|(k, v)| {
10591075
(hir.node_to_hir_id(k), Rc::new(v))
10601076
}).collect(),
@@ -1978,19 +1994,18 @@ impl<T, R, E> InternIteratorElement<T, R> for Result<T, E> {
19781994
}
19791995
}
19801996

1981-
fn in_scope_traits<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, id: HirId)
1982-
-> Option<Rc<Vec<TraitCandidate>>>
1983-
{
1984-
tcx.gcx.trait_map.get(&id).cloned()
1985-
}
1986-
1987-
fn module_exports<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, id: HirId)
1988-
-> Option<Rc<Vec<Export>>>
1989-
{
1990-
tcx.gcx.export_map.get(&id).cloned()
1997+
struct NamedRegionMap {
1998+
defs: FxHashMap<HirId, resolve_lifetime::Region>,
1999+
late_bound: FxHashSet<HirId>,
2000+
object_lifetime_defaults: FxHashMap<HirId, Rc<Vec<ObjectLifetimeDefault>>>,
19912001
}
19922002

19932003
pub fn provide(providers: &mut ty::maps::Providers) {
1994-
providers.in_scope_traits = in_scope_traits;
1995-
providers.module_exports = module_exports;
2004+
providers.in_scope_traits = |tcx, id| tcx.gcx.trait_map.get(&id).cloned();
2005+
providers.module_exports = |tcx, id| tcx.gcx.export_map.get(&id).cloned();
2006+
providers.named_region = |tcx, id| tcx.gcx.named_region_map.defs.get(&id).cloned();
2007+
providers.is_late_bound = |tcx, id| tcx.gcx.named_region_map.late_bound.contains(&id);
2008+
providers.object_lifetime_defaults = |tcx, id| {
2009+
tcx.gcx.named_region_map.object_lifetime_defaults.get(&id).cloned()
2010+
};
19962011
}

src/librustc/ty/maps.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ use middle::cstore::{ExternCrate, LinkagePreference, NativeLibrary};
2020
use middle::cstore::NativeLibraryKind;
2121
use middle::privacy::AccessLevels;
2222
use middle::region;
23+
use middle::region::RegionMaps;
24+
use middle::resolve_lifetime::{Region, ObjectLifetimeDefault};
2325
use mir;
2426
use mir::transform::{MirSuite, MirPassIndex};
2527
use session::CompileResult;
@@ -649,6 +651,24 @@ impl<'tcx> QueryDescription for queries::link_args<'tcx> {
649651
}
650652
}
651653

654+
impl<'tcx> QueryDescription for queries::named_region<'tcx> {
655+
fn describe(_tcx: TyCtxt, _: HirId) -> String {
656+
format!("fetching info about a named region")
657+
}
658+
}
659+
660+
impl<'tcx> QueryDescription for queries::is_late_bound<'tcx> {
661+
fn describe(_tcx: TyCtxt, _: HirId) -> String {
662+
format!("testing whether a lifetime is late bound")
663+
}
664+
}
665+
666+
impl<'tcx> QueryDescription for queries::object_lifetime_defaults<'tcx> {
667+
fn describe(_tcx: TyCtxt, _: HirId) -> String {
668+
format!("fetching a list of ObjectLifetimeDefault for a lifetime")
669+
}
670+
}
671+
652672
// If enabled, send a message to the profile-queries thread
653673
macro_rules! profq_msg {
654674
($tcx:expr, $msg:expr) => {
@@ -1243,6 +1263,11 @@ define_maps! { <'tcx>
12431263
[] native_library_kind: NativeLibraryKind(DefId)
12441264
-> Option<NativeLibraryKind>,
12451265
[] link_args: link_args_node(CrateNum) -> Rc<Vec<String>>,
1266+
1267+
[] named_region: NamedRegion(HirId) -> Option<Region>,
1268+
[] is_late_bound: IsLateBound(HirId) -> bool,
1269+
[] object_lifetime_defaults: ObjectLifetimeDefaults(HirId)
1270+
-> Option<Rc<Vec<ObjectLifetimeDefault>>>,
12461271
}
12471272

12481273
fn type_param_predicates<'tcx>((item_id, param_id): (DefId, DefId)) -> DepConstructor<'tcx> {

src/librustc_typeck/astconv.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -96,22 +96,23 @@ impl<'o, 'gcx: 'tcx, 'tcx> AstConv<'gcx, 'tcx>+'o {
9696
-> ty::Region<'tcx>
9797
{
9898
let tcx = self.tcx();
99-
let r = match tcx.named_region_map.defs.get(&lifetime.id) {
100-
Some(&rl::Region::Static) => {
99+
let hir_id = tcx.hir.node_to_hir_id(lifetime.id);
100+
let r = match tcx.named_region(hir_id) {
101+
Some(rl::Region::Static) => {
101102
tcx.types.re_static
102103
}
103104

104-
Some(&rl::Region::LateBound(debruijn, id)) => {
105+
Some(rl::Region::LateBound(debruijn, id)) => {
105106
let name = tcx.hir.name(id);
106107
tcx.mk_region(ty::ReLateBound(debruijn,
107108
ty::BrNamed(tcx.hir.local_def_id(id), name)))
108109
}
109110

110-
Some(&rl::Region::LateBoundAnon(debruijn, index)) => {
111+
Some(rl::Region::LateBoundAnon(debruijn, index)) => {
111112
tcx.mk_region(ty::ReLateBound(debruijn, ty::BrAnon(index)))
112113
}
113114

114-
Some(&rl::Region::EarlyBound(index, id)) => {
115+
Some(rl::Region::EarlyBound(index, id)) => {
115116
let name = tcx.hir.name(id);
116117
tcx.mk_region(ty::ReEarlyBound(ty::EarlyBoundRegion {
117118
def_id: tcx.hir.local_def_id(id),
@@ -120,7 +121,7 @@ impl<'o, 'gcx: 'tcx, 'tcx> AstConv<'gcx, 'tcx>+'o {
120121
}))
121122
}
122123

123-
Some(&rl::Region::Free(scope, id)) => {
124+
Some(rl::Region::Free(scope, id)) => {
124125
let name = tcx.hir.name(id);
125126
tcx.mk_region(ty::ReFree(ty::FreeRegion {
126127
scope,
@@ -627,7 +628,8 @@ impl<'o, 'gcx: 'tcx, 'tcx> AstConv<'gcx, 'tcx>+'o {
627628
self.ast_region_to_region(lifetime, None)
628629
} else {
629630
self.compute_object_lifetime_bound(span, existential_predicates).unwrap_or_else(|| {
630-
if tcx.named_region_map.defs.contains_key(&lifetime.id) {
631+
let hir_id = tcx.hir.node_to_hir_id(lifetime.id);
632+
if tcx.named_region(hir_id).is_some() {
631633
self.ast_region_to_region(lifetime, None)
632634
} else {
633635
self.re_infer(span, None).unwrap_or_else(|| {

src/librustc_typeck/collect.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -812,7 +812,8 @@ fn has_late_bound_regions<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
812812
fn visit_lifetime(&mut self, lt: &'tcx hir::Lifetime) {
813813
if self.has_late_bound_regions.is_some() { return }
814814

815-
match self.tcx.named_region_map.defs.get(&lt.id).cloned() {
815+
let hir_id = self.tcx.hir.node_to_hir_id(lt.id);
816+
match self.tcx.named_region(hir_id) {
816817
Some(rl::Region::Static) | Some(rl::Region::EarlyBound(..)) => {}
817818
Some(rl::Region::LateBound(debruijn, _)) |
818819
Some(rl::Region::LateBoundAnon(debruijn, _))
@@ -830,7 +831,8 @@ fn has_late_bound_regions<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
830831
tcx, binder_depth: 1, has_late_bound_regions: None
831832
};
832833
for lifetime in &generics.lifetimes {
833-
if tcx.named_region_map.late_bound.contains(&lifetime.lifetime.id) {
834+
let hir_id = tcx.hir.node_to_hir_id(lifetime.lifetime.id);
835+
if tcx.is_late_bound(hir_id) {
834836
return Some(lifetime.lifetime.span);
835837
}
836838
}
@@ -987,8 +989,8 @@ fn generics_of<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
987989
}
988990
}).collect::<Vec<_>>();
989991

990-
let object_lifetime_defaults =
991-
tcx.named_region_map.object_lifetime_defaults.get(&node_id);
992+
let hir_id = tcx.hir.node_to_hir_id(node_id);
993+
let object_lifetime_defaults = tcx.object_lifetime_defaults(hir_id);
992994

993995
// Now create the real type parameters.
994996
let type_start = own_start + regions.len() as u32;
@@ -1014,7 +1016,7 @@ fn generics_of<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
10141016
def_id: tcx.hir.local_def_id(p.id),
10151017
has_default: p.default.is_some(),
10161018
object_lifetime_default:
1017-
object_lifetime_defaults.map_or(rl::Set1::Empty, |o| o[i]),
1019+
object_lifetime_defaults.as_ref().map_or(rl::Set1::Empty, |o| o[i]),
10181020
pure_wrt_drop: p.pure_wrt_drop,
10191021
}
10201022
});
@@ -1343,7 +1345,10 @@ fn early_bound_lifetimes_from_generics<'a, 'tcx>(
13431345
ast_generics
13441346
.lifetimes
13451347
.iter()
1346-
.filter(move |l| !tcx.named_region_map.late_bound.contains(&l.lifetime.id))
1348+
.filter(move |l| {
1349+
let hir_id = tcx.hir.node_to_hir_id(l.lifetime.id);
1350+
!tcx.is_late_bound(hir_id)
1351+
})
13471352
}
13481353

13491354
fn predicates_of<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,

src/librustdoc/clean/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -830,7 +830,8 @@ impl Lifetime {
830830

831831
impl Clean<Lifetime> for hir::Lifetime {
832832
fn clean(&self, cx: &DocContext) -> Lifetime {
833-
let def = cx.tcx.named_region_map.defs.get(&self.id).cloned();
833+
let hir_id = cx.tcx.hir.node_to_hir_id(self.id);
834+
let def = cx.tcx.named_region(hir_id);
834835
match def {
835836
Some(rl::Region::EarlyBound(_, node_id)) |
836837
Some(rl::Region::LateBound(_, node_id)) |

0 commit comments

Comments
 (0)