Skip to content

Commit 10c232a

Browse files
committed
[WIP] per-fn RegionMaps
1 parent 98e0881 commit 10c232a

File tree

3 files changed

+60
-57
lines changed

3 files changed

+60
-57
lines changed

src/librustc/dep_graph/dep_node.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ pub enum DepNode<D: Clone + Debug> {
5858
// Represents different phases in the compiler.
5959
CollectLanguageItems,
6060
ResolveLifetimes,
61-
RegionResolveCrate,
61+
RegionResolveFn(D),
62+
CheckLoops,
6263
PluginRegistrar,
6364
StabilityIndex,
6465
CollectItem(D),
@@ -213,7 +214,7 @@ impl<D: Clone + Debug> DepNode<D> {
213214
TypeckBodiesKrate => Some(TypeckBodiesKrate),
214215
CollectLanguageItems => Some(CollectLanguageItems),
215216
ResolveLifetimes => Some(ResolveLifetimes),
216-
RegionResolveCrate => Some(RegionResolveCrate),
217+
CheckLoops => Some(CheckLoops),
217218
PluginRegistrar => Some(PluginRegistrar),
218219
StabilityIndex => Some(StabilityIndex),
219220
Coherence => Some(Coherence),
@@ -255,6 +256,7 @@ impl<D: Clone + Debug> DepNode<D> {
255256
def_ids.map(MirShim)
256257
}
257258
BorrowCheck(ref d) => op(d).map(BorrowCheck),
259+
RegionResolveFn(ref d) => op(d).map(RegionResolveFn),
258260
RvalueCheck(ref d) => op(d).map(RvalueCheck),
259261
StabilityCheck(ref d) => op(d).map(StabilityCheck),
260262
TransCrateItem(ref d) => op(d).map(TransCrateItem),

src/librustc/middle/region.rs

Lines changed: 52 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,7 @@ use syntax::codemap::DUMMY_SP;
3232
use ty::TyCtxt;
3333
use ty::maps::Providers;
3434

35-
use hir;
36-
use hir::def_id::{CrateNum, LOCAL_CRATE};
35+
use hir; use hir::def_id::{CrateNum, DefId, LOCAL_CRATE};
3736
use hir::intravisit::{self, Visitor, FnKind, NestedVisitorMap};
3837
use hir::{Block, Item, FnDecl, Arm, Pat, PatKind, Stmt, Expr, Local};
3938

@@ -347,7 +346,11 @@ struct RegionResolutionVisitor<'hir: 'a, 'a> {
347346
/// arbitrary amounts of stack space. Terminating scopes end
348347
/// up being contained in a DestructionScope that contains the
349348
/// destructor's execution.
350-
terminating_scopes: NodeSet
349+
terminating_scopes: NodeSet,
350+
351+
target_fn_id: DefId,
352+
353+
found_target_fn: bool,
351354
}
352355

353356

@@ -1139,50 +1142,54 @@ fn resolve_fn<'a, 'tcx>(visitor: &mut RegionResolutionVisitor<'tcx, 'a>,
11391142
body_id: hir::BodyId,
11401143
sp: Span,
11411144
id: ast::NodeId) {
1142-
debug!("region::resolve_fn(id={:?}, \
1143-
span={:?}, \
1144-
body.id={:?}, \
1145-
cx.parent={:?})",
1146-
id,
1147-
visitor.sess.codemap().span_to_string(sp),
1148-
body_id,
1149-
visitor.cx.parent);
11501145

11511146
visitor.cx.parent = visitor.new_code_extent(
11521147
CodeExtentData::CallSiteScope { fn_id: id, body_id: body_id.node_id });
11531148

1154-
let fn_decl_scope = visitor.new_code_extent(
1155-
CodeExtentData::ParameterScope { fn_id: id, body_id: body_id.node_id });
1156-
1157-
if let Some(root_id) = visitor.cx.root_id {
1158-
visitor.region_maps.record_fn_parent(body_id.node_id, root_id);
1149+
if body_id == visitor.target_fn_id {
1150+
// We've found the top level `fn`. Store it and its children in the `RegionMaps`
1151+
visitor.found_target_fn = true;
11591152
}
11601153

1161-
let outer_cx = visitor.cx;
1162-
let outer_ts = mem::replace(&mut visitor.terminating_scopes, NodeSet());
1163-
visitor.terminating_scopes.insert(body_id.node_id);
1164-
1165-
// The arguments and `self` are parented to the fn.
1166-
visitor.cx = Context {
1167-
root_id: Some(body_id.node_id),
1168-
parent: ROOT_CODE_EXTENT,
1169-
var_parent: fn_decl_scope,
1170-
};
1154+
if visitor.found_target_fn {
1155+
debug!("region::resolve_fn(id={:?}, \
1156+
span={:?}, \
1157+
body.id={:?}, \
1158+
cx.parent={:?})",
1159+
id,
1160+
visitor.sess.codemap().span_to_string(sp),
1161+
body_id,
1162+
visitor.cx.parent);
1163+
1164+
let fn_decl_scope = visitor.new_code_extent(
1165+
CodeExtentData::ParameterScope { fn_id: id, body_id: body_id.node_id });
1166+
1167+
let outer_cx = visitor.cx;
1168+
let outer_ts = mem::replace(&mut visitor.terminating_scopes, NodeSet());
1169+
visitor.terminating_scopes.insert(body_id.node_id);
1170+
1171+
// The arguments and `self` are parented to the fn.
1172+
visitor.cx = Context {
1173+
root_id: Some(body_id.node_id),
1174+
parent: ROOT_CODE_EXTENT,
1175+
var_parent: fn_decl_scope,
1176+
};
11711177

1172-
intravisit::walk_fn_decl(visitor, decl);
1173-
intravisit::walk_fn_kind(visitor, kind);
1178+
intravisit::walk_fn_decl(visitor, decl);
1179+
intravisit::walk_fn_kind(visitor, kind);
11741180

1175-
// The body of the every fn is a root scope.
1176-
visitor.cx = Context {
1177-
root_id: Some(body_id.node_id),
1178-
parent: fn_decl_scope,
1179-
var_parent: fn_decl_scope
1180-
};
1181-
visitor.visit_nested_body(body_id);
1181+
// The body of the every fn is a root scope.
1182+
visitor.cx = Context {
1183+
root_id: Some(body_id.node_id),
1184+
parent: fn_decl_scope,
1185+
var_parent: fn_decl_scope
1186+
};
1187+
visitor.visit_nested_body(body_id);
11821188

1183-
// Restore context we had at the start.
1184-
visitor.cx = outer_cx;
1185-
visitor.terminating_scopes = outer_ts;
1189+
// Restore context we had at the start.
1190+
visitor.cx = outer_cx;
1191+
visitor.terminating_scopes = outer_ts;
1192+
}
11861193
}
11871194

11881195
impl<'hir, 'a> RegionResolutionVisitor<'hir, 'a> {
@@ -1264,14 +1271,12 @@ impl<'hir, 'a> Visitor<'hir> for RegionResolutionVisitor<'hir, 'a> {
12641271
}
12651272

12661273
pub fn resolve_crate<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) -> Rc<RegionMaps> {
1267-
ty::queries::region_resolve_crate::get(tcx, DUMMY_SP, LOCAL_CRATE)
1274+
ty::queries::region_resolve_fn::get(tcx, DUMMY_SP, LOCAL_CRATE)
12681275
}
12691276

1270-
fn region_resolve_crate<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, crate_num: CrateNum)
1277+
fn region_resolve_fn<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, fn_id: DefId)
12711278
-> Rc<RegionMaps>
12721279
{
1273-
debug_assert!(crate_num == LOCAL_CRATE);
1274-
12751280
let sess = &tcx.sess;
12761281
let hir_map = &tcx.hir;
12771282

@@ -1302,16 +1307,19 @@ fn region_resolve_crate<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, crate_num: CrateN
13021307
parent: ROOT_CODE_EXTENT,
13031308
var_parent: ROOT_CODE_EXTENT
13041309
},
1305-
terminating_scopes: NodeSet()
1310+
terminating_scopes: NodeSet(),
1311+
target_fn_id: fn_id,
1312+
found_target_fn: false,
13061313
};
13071314
krate.visit_all_item_likes(&mut visitor.as_deep_visitor());
1315+
debug_assert!(visitor.found_target_fn);
13081316
}
13091317
Rc::new(maps)
13101318
}
13111319

13121320
pub fn provide(providers: &mut Providers) {
13131321
*providers = Providers {
1314-
region_resolve_crate,
1322+
region_resolve_fn,
13151323
..*providers
13161324
};
13171325
}

src/librustc/ty/maps.rs

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -217,12 +217,6 @@ impl<'tcx> QueryDescription for queries::reachable_set<'tcx> {
217217
}
218218
}
219219

220-
impl<'tcx> QueryDescription for queries::region_resolve_crate<'tcx> {
221-
fn describe(_: TyCtxt, _: CrateNum) -> String {
222-
format!("resolve crate")
223-
}
224-
}
225-
226220
macro_rules! define_maps {
227221
(<$tcx:tt>
228222
$($(#[$attr:meta])*
@@ -457,7 +451,10 @@ define_maps! { <'tcx>
457451

458452
pub reachable_set: reachability_dep_node(CrateNum) -> NodeSet,
459453

460-
pub region_resolve_crate: region_resolve_crate_dep_node(CrateNum) -> Rc<RegionMaps>,
454+
/// Per-function `RegionMaps`. Regions are referenced through their top-most containing
455+
/// function, e.g. `fn outer() { fn inner() { ... } }` produces a single entry which can
456+
/// be accessed using the `DefId` of `outer`.
457+
pub region_resolve_fn: RegionResolveFn(DefId) -> Rc<RegionMaps>,
461458

462459
pub mir_shims: mir_shim(ty::InstanceDef<'tcx>) -> &'tcx RefCell<mir::Mir<'tcx>>
463460
}
@@ -474,10 +471,6 @@ fn reachability_dep_node(_: CrateNum) -> DepNode<DefId> {
474471
DepNode::Reachability
475472
}
476473

477-
fn region_resolve_crate_dep_node(_: CrateNum) -> DepNode<DefId> {
478-
DepNode::RegionResolveCrate
479-
}
480-
481474
fn mir_shim(instance: ty::InstanceDef) -> DepNode<DefId> {
482475
instance.dep_node()
483476
}

0 commit comments

Comments
 (0)