Skip to content

Commit

Permalink
On-demandify region mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
cramertj authored and nikomatsakis committed Apr 30, 2017
1 parent 06fb4d2 commit eff39b7
Show file tree
Hide file tree
Showing 32 changed files with 146 additions and 105 deletions.
8 changes: 4 additions & 4 deletions src/librustc/cfg/construct.rs
Expand Up @@ -583,11 +583,11 @@ impl<'a, 'tcx> CFGBuilder<'a, 'tcx> {
scope_id: ast::NodeId,
to_index: CFGIndex) {
let mut data = CFGEdgeData { exiting_scopes: vec![] };
let mut scope = self.tcx.region_maps.node_extent(from_expr.id);
let target_scope = self.tcx.region_maps.node_extent(scope_id);
let mut scope = self.tcx.region_maps().node_extent(from_expr.id);
let target_scope = self.tcx.region_maps().node_extent(scope_id);
while scope != target_scope {
data.exiting_scopes.push(scope.node_id(&self.tcx.region_maps));
scope = self.tcx.region_maps.encl_scope(scope);
data.exiting_scopes.push(scope.node_id(&self.tcx.region_maps()));
scope = self.tcx.region_maps().encl_scope(scope);
}
self.graph.add_edge(from_index, to_index, data);
}
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/ich/impls_ty.rs
Expand Up @@ -438,7 +438,7 @@ impl<'a, 'tcx> HashStable<StableHashingContext<'a, 'tcx>> for ::middle::region::
hcx: &mut StableHashingContext<'a, 'tcx>,
hasher: &mut StableHasher<W>) {
hcx.with_node_id_hashing_mode(NodeIdHashingMode::HashDefPath, |hcx| {
hcx.tcx().region_maps.code_extent_data(*self).hash_stable(hcx, hasher);
hcx.tcx().region_maps().code_extent_data(*self).hash_stable(hcx, hasher);
});
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/librustc/infer/error_reporting/mod.rs
Expand Up @@ -123,14 +123,14 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
format!("{}unknown scope: {:?}{}. Please report a bug.",
prefix, scope, suffix)
};
let span = match scope.span(&self.region_maps, &self.hir) {
let span = match scope.span(&self.region_maps(), &self.hir) {
Some(s) => s,
None => {
err.note(&unknown_scope());
return;
}
};
let tag = match self.hir.find(scope.node_id(&self.region_maps)) {
let tag = match self.hir.find(scope.node_id(&self.region_maps())) {
Some(hir_map::NodeBlock(_)) => "block",
Some(hir_map::NodeExpr(expr)) => match expr.node {
hir::ExprCall(..) => "call",
Expand All @@ -150,7 +150,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
return;
}
};
let scope_decorated_tag = match self.region_maps.code_extent_data(scope) {
let scope_decorated_tag = match self.region_maps().code_extent_data(scope) {
region::CodeExtentData::Misc(_) => tag,
region::CodeExtentData::CallSiteScope { .. } => {
"scope of call-site for function"
Expand Down Expand Up @@ -183,7 +183,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
}
};

let node = fr.scope.node_id(&self.region_maps);
let node = fr.scope.node_id(&self.region_maps());
let unknown;
let tag = match self.hir.find(node) {
Some(hir_map::NodeBlock(_)) |
Expand Down
4 changes: 2 additions & 2 deletions src/librustc/infer/region_inference/graphviz.rs
Expand Up @@ -159,7 +159,7 @@ impl<'a, 'gcx, 'tcx> ConstraintGraph<'a, 'gcx, 'tcx> {
add_node(n2);
}

tcx.region_maps.each_encl_scope(|sub, sup| {
tcx.region_maps().each_encl_scope(|sub, sup| {
add_node(Node::Region(ty::ReScope(*sub)));
add_node(Node::Region(ty::ReScope(*sup)));
});
Expand Down Expand Up @@ -245,7 +245,7 @@ impl<'a, 'gcx, 'tcx> dot::GraphWalk<'a> for ConstraintGraph<'a, 'gcx, 'tcx> {
fn edges(&self) -> dot::Edges<Edge<'tcx>> {
debug!("constraint graph has {} edges", self.map.len());
let mut v: Vec<_> = self.map.keys().map(|e| Edge::Constraint(*e)).collect();
self.tcx.region_maps.each_encl_scope(|sub, sup| v.push(Edge::EnclScope(*sub, *sup)));
self.tcx.region_maps().each_encl_scope(|sub, sup| v.push(Edge::EnclScope(*sub, *sup)));
debug!("region graph has {} edges", v.len());
Cow::Owned(v)
}
Expand Down
4 changes: 2 additions & 2 deletions src/librustc/infer/region_inference/mod.rs
Expand Up @@ -938,7 +938,7 @@ impl<'a, 'gcx, 'tcx> RegionVarBindings<'a, 'gcx, 'tcx> {
// A "free" region can be interpreted as "some region
// at least as big as the block fr.scope_id". So, we can
// reasonably compare free regions and scopes:
let r_id = self.tcx.region_maps.nearest_common_ancestor(fr.scope, s_id);
let r_id = self.tcx.region_maps().nearest_common_ancestor(fr.scope, s_id);

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

(&ReFree(a_fr), &ReFree(b_fr)) => {
Expand Down
4 changes: 2 additions & 2 deletions src/librustc/middle/free_region.rs
Expand Up @@ -136,10 +136,10 @@ impl FreeRegionMap {
true,

(&ty::ReScope(sub_scope), &ty::ReScope(super_scope)) =>
tcx.region_maps.is_subscope_of(sub_scope, super_scope),
tcx.region_maps().is_subscope_of(sub_scope, super_scope),

(&ty::ReScope(sub_scope), &ty::ReFree(fr)) =>
tcx.region_maps.is_subscope_of(sub_scope, fr.scope) ||
tcx.region_maps().is_subscope_of(sub_scope, fr.scope) ||
self.is_static(fr),

(&ty::ReFree(sub_fr), &ty::ReFree(super_fr)) =>
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/middle/liveness.rs
Expand Up @@ -1441,7 +1441,7 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
// and must outlive the *call-site* of the function.
let fn_ret =
self.ir.tcx.liberate_late_bound_regions(
self.ir.tcx.region_maps.call_site_extent(id, body.value.id),
self.ir.tcx.region_maps().call_site_extent(id, body.value.id),
&fn_ret);

if !fn_ret.is_never() && self.live_on_entry(entry_ln, self.s.no_ret_var).is_some() {
Expand Down
4 changes: 2 additions & 2 deletions src/librustc/middle/mem_categorization.rs
Expand Up @@ -796,7 +796,7 @@ impl<'a, 'gcx, 'tcx> MemCategorizationContext<'a, 'gcx, 'tcx> {
// The environment of a closure is guaranteed to
// outlive any bindings introduced in the body of the
// closure itself.
scope: self.tcx().region_maps.item_extent(fn_body_id),
scope: self.tcx().region_maps().item_extent(fn_body_id),
bound_region: ty::BrEnv
}));

Expand Down Expand Up @@ -845,7 +845,7 @@ impl<'a, 'gcx, 'tcx> MemCategorizationContext<'a, 'gcx, 'tcx> {
pub fn temporary_scope(&self, id: ast::NodeId) -> (&'tcx ty::Region, &'tcx ty::Region)
{
let (scope, old_scope) =
self.tcx().region_maps.old_and_new_temporary_scope(id);
self.tcx().region_maps().old_and_new_temporary_scope(id);
(self.tcx().mk_region(match scope {
Some(scope) => ty::ReScope(scope),
None => ty::ReStatic
Expand Down
41 changes: 33 additions & 8 deletions src/librustc/middle/region.rs
Expand Up @@ -16,7 +16,6 @@
//! Most of the documentation on regions can be found in
//! `middle/infer/region_inference/README.md`

use dep_graph::DepNode;
use hir::map as hir_map;
use session::Session;
use util::nodemap::{FxHashMap, NodeMap, NodeSet};
Expand All @@ -26,11 +25,15 @@ use std::cell::RefCell;
use std::collections::hash_map::Entry;
use std::fmt;
use std::mem;
use std::rc::Rc;
use syntax::codemap;
use syntax::ast::{self, NodeId};
use syntax_pos::Span;
use ty::TyCtxt;
use ty::maps::Providers;

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

Expand All @@ -44,8 +47,13 @@ impl fmt::Debug for CodeExtent {

ty::tls::with_opt(|opt_tcx| {
if let Some(tcx) = opt_tcx {
if let Some(data) = tcx.region_maps.code_extents.borrow().get(self.0 as usize) {
write!(f, "/{:?}", data)?;
let region_maps = tcx.region_maps();
{
let code_extents = &region_maps.code_extents;
if let Some(data) = code_extents.borrow().get(self.0 as usize) {
write!(f, "/{:?}", data)?;
}
mem::drop(code_extents); // FIXME why is this necessary?
}
}
Ok(())
Expand Down Expand Up @@ -1256,9 +1264,19 @@ impl<'hir, 'a> Visitor<'hir> for RegionResolutionVisitor<'hir, 'a> {
}
}

pub fn resolve_crate(sess: &Session, map: &hir_map::Map) -> RegionMaps {
let _task = map.dep_graph.in_task(DepNode::RegionResolveCrate);
let krate = map.krate();
pub fn resolve_crate<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) -> Rc<RegionMaps> {
tcx.region_resolve_crate(LOCAL_CRATE)
}

fn region_resolve_crate<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, crate_num: CrateNum)
-> Rc<RegionMaps>
{
debug_assert!(crate_num == LOCAL_CRATE);

let sess = &tcx.sess;
let hir_map = &tcx.hir;

let krate = hir_map.krate();

let maps = RegionMaps {
code_extents: RefCell::new(vec![]),
Expand All @@ -1279,7 +1297,7 @@ pub fn resolve_crate(sess: &Session, map: &hir_map::Map) -> RegionMaps {
let mut visitor = RegionResolutionVisitor {
sess: sess,
region_maps: &maps,
map: map,
map: hir_map,
cx: Context {
root_id: None,
parent: ROOT_CODE_EXTENT,
Expand All @@ -1289,5 +1307,12 @@ pub fn resolve_crate(sess: &Session, map: &hir_map::Map) -> RegionMaps {
};
krate.visit_all_item_likes(&mut visitor.as_deep_visitor());
}
return maps;
Rc::new(maps)
}

pub fn provide(providers: &mut Providers) {
*providers = Providers {
region_resolve_crate,
..*providers
};
}
2 changes: 1 addition & 1 deletion src/librustc/traits/object_safety.rs
Expand Up @@ -207,7 +207,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {

// Search for a predicate like `Self : Sized` amongst the trait bounds.
let free_substs = self.construct_free_substs(def_id,
self.region_maps.node_extent(ast::DUMMY_NODE_ID));
self.region_maps().node_extent(ast::DUMMY_NODE_ID));
let predicates = self.predicates_of(def_id);
let predicates = predicates.instantiate(self, free_substs).predicates;
elaborate_predicates(self, predicates)
Expand Down
9 changes: 5 additions & 4 deletions src/librustc/ty/context.rs
Expand Up @@ -51,6 +51,7 @@ use std::mem;
use std::ops::Deref;
use std::iter;
use std::cmp::Ordering;
use std::rc::Rc;
use syntax::abi;
use syntax::ast::{self, Name, NodeId};
use syntax::attr;
Expand Down Expand Up @@ -439,8 +440,6 @@ pub struct GlobalCtxt<'tcx> {

pub named_region_map: resolve_lifetime::NamedRegionMap,

pub region_maps: RegionMaps,

pub hir: hir_map::Map<'tcx>,
pub maps: maps::Maps<'tcx>,

Expand Down Expand Up @@ -678,6 +677,10 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
local as usize == global as usize
}

pub fn region_maps(self) -> Rc<RegionMaps> {
self.region_resolve_crate(LOCAL_CRATE)
}

/// Create a type context and call the closure with a `TyCtxt` reference
/// to the context. The closure enforces that the type context and any interned
/// value (types, substs, etc.) can only be used while `ty::tls` has a valid
Expand All @@ -690,7 +693,6 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
resolutions: ty::Resolutions,
named_region_map: resolve_lifetime::NamedRegionMap,
hir: hir_map::Map<'tcx>,
region_maps: RegionMaps,
lang_items: middle::lang_items::LanguageItems,
stability: stability::Index<'tcx>,
crate_name: &str,
Expand All @@ -714,7 +716,6 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
dep_graph: dep_graph.clone(),
types: common_types,
named_region_map: named_region_map,
region_maps: region_maps,
variance_computed: Cell::new(false),
trait_map: resolutions.trait_map,
export_map: resolutions.export_map,
Expand Down
15 changes: 14 additions & 1 deletion src/librustc/ty/maps.rs
Expand Up @@ -14,6 +14,7 @@ use hir::def::Def;
use hir;
use middle::const_val;
use middle::privacy::AccessLevels;
use middle::region::RegionMaps;
use mir;
use session::CompileResult;
use ty::{self, CrateInherentImpls, Ty, TyCtxt};
Expand Down Expand Up @@ -291,6 +292,12 @@ impl<'tcx> QueryDescription for queries::def_span<'tcx> {
}
}

impl<'tcx> QueryDescription for queries::region_resolve_crate<'tcx> {
fn describe(_: TyCtxt, _: CrateNum) -> String {
format!("resolve crate")
}
}

macro_rules! define_maps {
(<$tcx:tt>
$($(#[$attr:meta])*
Expand Down Expand Up @@ -571,6 +578,8 @@ define_maps! { <'tcx>

[] reachable_set: reachability_dep_node(CrateNum) -> Rc<NodeSet>,

[] region_resolve_crate: region_resolve_crate_dep_node(CrateNum) -> Rc<RegionMaps>,

[] mir_shims: mir_shim_dep_node(ty::InstanceDef<'tcx>) -> &'tcx RefCell<mir::Mir<'tcx>>,

[] def_symbol_name: SymbolName(DefId) -> ty::SymbolName,
Expand All @@ -592,6 +601,10 @@ fn reachability_dep_node(_: CrateNum) -> DepNode<DefId> {
DepNode::Reachability
}

fn region_resolve_crate_dep_node(_: CrateNum) -> DepNode<DefId> {
DepNode::RegionResolveCrate
}

fn mir_shim_dep_node(instance: ty::InstanceDef) -> DepNode<DefId> {
instance.dep_node()
}
Expand All @@ -608,4 +621,4 @@ fn typeck_item_bodies_dep_node(_: CrateNum) -> DepNode<DefId> {

fn const_eval_dep_node((def_id, _): (DefId, &Substs)) -> DepNode<DefId> {
DepNode::ConstEval(def_id)
}
}

0 comments on commit eff39b7

Please sign in to comment.