Skip to content

Commit ed93010

Browse files
committed
On-demandify region mapping
1 parent d5cf1cb commit ed93010

File tree

31 files changed

+147
-105
lines changed

31 files changed

+147
-105
lines changed

src/librustc/cfg/construct.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -583,11 +583,11 @@ impl<'a, 'tcx> CFGBuilder<'a, 'tcx> {
583583
scope_id: ast::NodeId,
584584
to_index: CFGIndex) {
585585
let mut data = CFGEdgeData { exiting_scopes: vec![] };
586-
let mut scope = self.tcx.region_maps.node_extent(from_expr.id);
587-
let target_scope = self.tcx.region_maps.node_extent(scope_id);
586+
let mut scope = self.tcx.region_maps().node_extent(from_expr.id);
587+
let target_scope = self.tcx.region_maps().node_extent(scope_id);
588588
while scope != target_scope {
589-
data.exiting_scopes.push(scope.node_id(&self.tcx.region_maps));
590-
scope = self.tcx.region_maps.encl_scope(scope);
589+
data.exiting_scopes.push(scope.node_id(&self.tcx.region_maps()));
590+
scope = self.tcx.region_maps().encl_scope(scope);
591591
}
592592
self.graph.add_edge(from_index, to_index, data);
593593
}

src/librustc/infer/error_reporting/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,14 +123,14 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
123123
format!("{}unknown scope: {:?}{}. Please report a bug.",
124124
prefix, scope, suffix)
125125
};
126-
let span = match scope.span(&self.region_maps, &self.hir) {
126+
let span = match scope.span(&self.region_maps(), &self.hir) {
127127
Some(s) => s,
128128
None => {
129129
err.note(&unknown_scope());
130130
return;
131131
}
132132
};
133-
let tag = match self.hir.find(scope.node_id(&self.region_maps)) {
133+
let tag = match self.hir.find(scope.node_id(&self.region_maps())) {
134134
Some(hir_map::NodeBlock(_)) => "block",
135135
Some(hir_map::NodeExpr(expr)) => match expr.node {
136136
hir::ExprCall(..) => "call",
@@ -150,7 +150,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
150150
return;
151151
}
152152
};
153-
let scope_decorated_tag = match self.region_maps.code_extent_data(scope) {
153+
let scope_decorated_tag = match self.region_maps().code_extent_data(scope) {
154154
region::CodeExtentData::Misc(_) => tag,
155155
region::CodeExtentData::CallSiteScope { .. } => {
156156
"scope of call-site for function"
@@ -183,7 +183,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
183183
}
184184
};
185185

186-
let node = fr.scope.node_id(&self.region_maps);
186+
let node = fr.scope.node_id(&self.region_maps());
187187
let unknown;
188188
let tag = match self.hir.find(node) {
189189
Some(hir_map::NodeBlock(_)) |

src/librustc/infer/region_inference/graphviz.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ impl<'a, 'gcx, 'tcx> ConstraintGraph<'a, 'gcx, 'tcx> {
159159
add_node(n2);
160160
}
161161

162-
tcx.region_maps.each_encl_scope(|sub, sup| {
162+
tcx.region_maps().each_encl_scope(|sub, sup| {
163163
add_node(Node::Region(ty::ReScope(*sub)));
164164
add_node(Node::Region(ty::ReScope(*sup)));
165165
});
@@ -245,7 +245,7 @@ impl<'a, 'gcx, 'tcx> dot::GraphWalk<'a> for ConstraintGraph<'a, 'gcx, 'tcx> {
245245
fn edges(&self) -> dot::Edges<Edge<'tcx>> {
246246
debug!("constraint graph has {} edges", self.map.len());
247247
let mut v: Vec<_> = self.map.keys().map(|e| Edge::Constraint(*e)).collect();
248-
self.tcx.region_maps.each_encl_scope(|sub, sup| v.push(Edge::EnclScope(*sub, *sup)));
248+
self.tcx.region_maps().each_encl_scope(|sub, sup| v.push(Edge::EnclScope(*sub, *sup)));
249249
debug!("region graph has {} edges", v.len());
250250
Cow::Owned(v)
251251
}

src/librustc/infer/region_inference/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -938,7 +938,7 @@ impl<'a, 'gcx, 'tcx> RegionVarBindings<'a, 'gcx, 'tcx> {
938938
// A "free" region can be interpreted as "some region
939939
// at least as big as the block fr.scope_id". So, we can
940940
// reasonably compare free regions and scopes:
941-
let r_id = self.tcx.region_maps.nearest_common_ancestor(fr.scope, s_id);
941+
let r_id = self.tcx.region_maps().nearest_common_ancestor(fr.scope, s_id);
942942

943943
if r_id == fr.scope {
944944
// if the free region's scope `fr.scope_id` is bigger than
@@ -957,7 +957,7 @@ impl<'a, 'gcx, 'tcx> RegionVarBindings<'a, 'gcx, 'tcx> {
957957
// subtype of the region corresponding to an inner
958958
// block.
959959
self.tcx.mk_region(ReScope(
960-
self.tcx.region_maps.nearest_common_ancestor(a_id, b_id)))
960+
self.tcx.region_maps().nearest_common_ancestor(a_id, b_id)))
961961
}
962962

963963
(&ReFree(a_fr), &ReFree(b_fr)) => {

src/librustc/middle/free_region.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,10 +136,10 @@ impl FreeRegionMap {
136136
true,
137137

138138
(&ty::ReScope(sub_scope), &ty::ReScope(super_scope)) =>
139-
tcx.region_maps.is_subscope_of(sub_scope, super_scope),
139+
tcx.region_maps().is_subscope_of(sub_scope, super_scope),
140140

141141
(&ty::ReScope(sub_scope), &ty::ReFree(fr)) =>
142-
tcx.region_maps.is_subscope_of(sub_scope, fr.scope) ||
142+
tcx.region_maps().is_subscope_of(sub_scope, fr.scope) ||
143143
self.is_static(fr),
144144

145145
(&ty::ReFree(sub_fr), &ty::ReFree(super_fr)) =>

src/librustc/middle/liveness.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1441,7 +1441,7 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
14411441
// and must outlive the *call-site* of the function.
14421442
let fn_ret =
14431443
self.ir.tcx.liberate_late_bound_regions(
1444-
self.ir.tcx.region_maps.call_site_extent(id, body.value.id),
1444+
self.ir.tcx.region_maps().call_site_extent(id, body.value.id),
14451445
&fn_ret);
14461446

14471447
if !fn_ret.is_never() && self.live_on_entry(entry_ln, self.s.no_ret_var).is_some() {

src/librustc/middle/mem_categorization.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -793,7 +793,7 @@ impl<'a, 'gcx, 'tcx> MemCategorizationContext<'a, 'gcx, 'tcx> {
793793
// The environment of a closure is guaranteed to
794794
// outlive any bindings introduced in the body of the
795795
// closure itself.
796-
scope: self.tcx().region_maps.item_extent(fn_body_id),
796+
scope: self.tcx().region_maps().item_extent(fn_body_id),
797797
bound_region: ty::BrEnv
798798
}));
799799

@@ -842,7 +842,7 @@ impl<'a, 'gcx, 'tcx> MemCategorizationContext<'a, 'gcx, 'tcx> {
842842
pub fn temporary_scope(&self, id: ast::NodeId) -> (&'tcx ty::Region, &'tcx ty::Region)
843843
{
844844
let (scope, old_scope) =
845-
self.tcx().region_maps.old_and_new_temporary_scope(id);
845+
self.tcx().region_maps().old_and_new_temporary_scope(id);
846846
(self.tcx().mk_region(match scope {
847847
Some(scope) => ty::ReScope(scope),
848848
None => ty::ReStatic

src/librustc/middle/region.rs

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
//! Most of the documentation on regions can be found in
1717
//! `middle/infer/region_inference/README.md`
1818
19-
use dep_graph::DepNode;
2019
use hir::map as hir_map;
2120
use session::Session;
2221
use util::nodemap::{FxHashMap, NodeMap, NodeSet};
@@ -26,11 +25,16 @@ use std::cell::RefCell;
2625
use std::collections::hash_map::Entry;
2726
use std::fmt;
2827
use std::mem;
28+
use std::rc::Rc;
2929
use syntax::codemap;
3030
use syntax::ast::{self, NodeId};
3131
use syntax_pos::Span;
32+
use syntax::codemap::DUMMY_SP;
33+
use ty::TyCtxt;
34+
use ty::maps::Providers;
3235

3336
use hir;
37+
use hir::def_id::{CrateNum, LOCAL_CRATE};
3438
use hir::intravisit::{self, Visitor, FnKind, NestedVisitorMap};
3539
use hir::{Block, Item, FnDecl, Arm, Pat, PatKind, Stmt, Expr, Local};
3640

@@ -44,8 +48,13 @@ impl fmt::Debug for CodeExtent {
4448

4549
ty::tls::with_opt(|opt_tcx| {
4650
if let Some(tcx) = opt_tcx {
47-
if let Some(data) = tcx.region_maps.code_extents.borrow().get(self.0 as usize) {
48-
write!(f, "/{:?}", data)?;
51+
let region_maps = tcx.region_maps();
52+
{
53+
let code_extents = &region_maps.code_extents;
54+
if let Some(data) = code_extents.borrow().get(self.0 as usize) {
55+
write!(f, "/{:?}", data)?;
56+
}
57+
mem::drop(code_extents); // FIXME why is this necessary?
4958
}
5059
}
5160
Ok(())
@@ -1256,9 +1265,19 @@ impl<'hir, 'a> Visitor<'hir> for RegionResolutionVisitor<'hir, 'a> {
12561265
}
12571266
}
12581267

1259-
pub fn resolve_crate(sess: &Session, map: &hir_map::Map) -> RegionMaps {
1260-
let _task = map.dep_graph.in_task(DepNode::RegionResolveCrate);
1261-
let krate = map.krate();
1268+
pub fn resolve_crate<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) -> Rc<RegionMaps> {
1269+
ty::queries::region_resolve_crate::get(tcx, DUMMY_SP, LOCAL_CRATE)
1270+
}
1271+
1272+
fn region_resolve_crate<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, crate_num: CrateNum)
1273+
-> Rc<RegionMaps>
1274+
{
1275+
debug_assert!(crate_num == LOCAL_CRATE);
1276+
1277+
let sess = &tcx.sess;
1278+
let hir_map = &tcx.hir;
1279+
1280+
let krate = hir_map.krate();
12621281

12631282
let maps = RegionMaps {
12641283
code_extents: RefCell::new(vec![]),
@@ -1279,7 +1298,7 @@ pub fn resolve_crate(sess: &Session, map: &hir_map::Map) -> RegionMaps {
12791298
let mut visitor = RegionResolutionVisitor {
12801299
sess: sess,
12811300
region_maps: &maps,
1282-
map: map,
1301+
map: hir_map,
12831302
cx: Context {
12841303
root_id: None,
12851304
parent: ROOT_CODE_EXTENT,
@@ -1289,5 +1308,12 @@ pub fn resolve_crate(sess: &Session, map: &hir_map::Map) -> RegionMaps {
12891308
};
12901309
krate.visit_all_item_likes(&mut visitor.as_deep_visitor());
12911310
}
1292-
return maps;
1311+
Rc::new(maps)
1312+
}
1313+
1314+
pub fn provide(providers: &mut Providers) {
1315+
*providers = Providers {
1316+
region_resolve_crate,
1317+
..*providers
1318+
};
12931319
}

src/librustc/traits/object_safety.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
198198

199199
// Search for a predicate like `Self : Sized` amongst the trait bounds.
200200
let free_substs = self.construct_free_substs(def_id,
201-
self.region_maps.node_extent(ast::DUMMY_NODE_ID));
201+
self.region_maps().node_extent(ast::DUMMY_NODE_ID));
202202
let predicates = self.item_predicates(def_id);
203203
let predicates = predicates.instantiate(self, free_substs).predicates;
204204
elaborate_predicates(self, predicates)

src/librustc/ty/context.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,11 @@ use std::mem;
5151
use std::ops::Deref;
5252
use std::iter;
5353
use std::cmp::Ordering;
54+
use std::rc::Rc;
5455
use syntax::abi;
5556
use syntax::ast::{self, Name, NodeId};
5657
use syntax::attr;
58+
use syntax::codemap::DUMMY_SP;
5759
use syntax::symbol::{Symbol, keywords};
5860

5961
use hir;
@@ -421,8 +423,6 @@ pub struct GlobalCtxt<'tcx> {
421423

422424
pub named_region_map: resolve_lifetime::NamedRegionMap,
423425

424-
pub region_maps: RegionMaps,
425-
426426
pub hir: hir_map::Map<'tcx>,
427427
pub maps: maps::Maps<'tcx>,
428428

@@ -663,6 +663,10 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
663663
local as usize == global as usize
664664
}
665665

666+
pub fn region_maps(self) -> Rc<RegionMaps> {
667+
ty::queries::region_resolve_crate::get(self, DUMMY_SP, LOCAL_CRATE)
668+
}
669+
666670
/// Create a type context and call the closure with a `TyCtxt` reference
667671
/// to the context. The closure enforces that the type context and any interned
668672
/// value (types, substs, etc.) can only be used while `ty::tls` has a valid
@@ -675,7 +679,6 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
675679
resolutions: ty::Resolutions,
676680
named_region_map: resolve_lifetime::NamedRegionMap,
677681
hir: hir_map::Map<'tcx>,
678-
region_maps: RegionMaps,
679682
lang_items: middle::lang_items::LanguageItems,
680683
stability: stability::Index<'tcx>,
681684
crate_name: &str,
@@ -698,7 +701,6 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
698701
dep_graph: dep_graph.clone(),
699702
types: common_types,
700703
named_region_map: named_region_map,
701-
region_maps: region_maps,
702704
variance_computed: Cell::new(false),
703705
trait_map: resolutions.trait_map,
704706
export_map: resolutions.export_map,

src/librustc/ty/maps.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use dep_graph::{DepGraph, DepNode, DepTrackingMap, DepTrackingMapConfig};
1212
use hir::def_id::{CrateNum, DefId, LOCAL_CRATE};
1313
use middle::const_val::ConstVal;
1414
use middle::privacy::AccessLevels;
15+
use middle::region::RegionMaps;
1516
use mir;
1617
use session::CompileResult;
1718
use ty::{self, CrateInherentImpls, Ty, TyCtxt};
@@ -216,6 +217,12 @@ impl<'tcx> QueryDescription for queries::reachable_set<'tcx> {
216217
}
217218
}
218219

220+
impl<'tcx> QueryDescription for queries::region_resolve_crate<'tcx> {
221+
fn describe(_: TyCtxt, _: CrateNum) -> String {
222+
format!("resolve crate")
223+
}
224+
}
225+
219226
macro_rules! define_maps {
220227
(<$tcx:tt>
221228
$($(#[$attr:meta])*
@@ -450,6 +457,8 @@ define_maps! { <'tcx>
450457

451458
pub reachable_set: reachability_dep_node(CrateNum) -> NodeSet,
452459

460+
pub region_resolve_crate: region_resolve_crate_dep_node(CrateNum) -> Rc<RegionMaps>,
461+
453462
pub mir_shims: mir_shim(ty::InstanceDef<'tcx>) -> &'tcx RefCell<mir::Mir<'tcx>>
454463
}
455464

@@ -465,6 +474,10 @@ fn reachability_dep_node(_: CrateNum) -> DepNode<DefId> {
465474
DepNode::Reachability
466475
}
467476

477+
fn region_resolve_crate_dep_node(_: CrateNum) -> DepNode<DefId> {
478+
DepNode::RegionResolveCrate
479+
}
480+
468481
fn mir_shim(instance: ty::InstanceDef) -> DepNode<DefId> {
469482
instance.dep_node()
470483
}

0 commit comments

Comments
 (0)