Skip to content

Commit f242329

Browse files
cramertjnikomatsakis
authored andcommitted
Convert fn_id to outermost in region_maps
1 parent 6b03fb4 commit f242329

File tree

3 files changed

+46
-7
lines changed

3 files changed

+46
-7
lines changed

src/librustc/hir/map/mod.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -572,6 +572,18 @@ impl<'hir> Map<'hir> {
572572
}
573573
}
574574

575+
/// Check if the node is a non-closure function item
576+
pub fn is_fn(&self, id: NodeId) -> bool {
577+
let entry = if let Some(id) = self.find_entry(id) { id } else { return false };
578+
579+
match entry {
580+
EntryItem(_, &Item { node: ItemFn(..), .. }) |
581+
EntryTraitItem(_, &TraitItem { node: TraitItemKind::Method(..), .. }) |
582+
EntryImplItem(_, &ImplItem { node: ImplItemKind::Method(..), .. }) => true,
583+
_ => false,
584+
}
585+
}
586+
575587
/// If there is some error when walking the parents (e.g., a node does not
576588
/// have a parent in the map or a node can't be found), then we return the
577589
/// last good node id we found. Note that reaching the crate root (id == 0),

src/librustc/middle/region.rs

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ struct RegionResolutionVisitor<'a, 'tcx: 'a> {
303303
/// destructor's execution.
304304
terminating_scopes: NodeSet,
305305

306-
target_fn_id: DefId,
306+
target_fn_node_id: NodeId,
307307

308308
found_target_fn: bool,
309309
}
@@ -1037,7 +1037,7 @@ fn resolve_fn<'a, 'tcx>(visitor: &mut RegionResolutionVisitor<'a, 'tcx>,
10371037
visitor.cx.parent = Some(visitor.new_code_extent(
10381038
CodeExtentData::CallSiteScope { fn_id: id, body_id: body_id.node_id }));
10391039

1040-
if body_id == visitor.target_fn_id {
1040+
if id == visitor.target_fn_node_id {
10411041
// We've found the top level `fn`. Store it and its children in the `RegionMaps`
10421042
visitor.found_target_fn = true;
10431043
}
@@ -1170,8 +1170,23 @@ impl<'a, 'tcx> Visitor<'tcx> for RegionResolutionVisitor<'a, 'tcx> {
11701170
}
11711171
}
11721172

1173-
pub fn resolve_crate<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) -> Rc<RegionMaps<'tcx>> {
1174-
ty::queries::region_resolve_fn::get(tcx, DUMMY_SP, LOCAL_CRATE)
1173+
pub fn resolve_crate<'a, 'tcx: 'a>(tcx: TyCtxt<'a, 'tcx, 'tcx>) {
1174+
1175+
struct CrateResolutionVisitor<'a, 'tcx: 'a>(TyCtxt<'a, 'tcx, 'tcx>);
1176+
1177+
impl<'a, 'hir: 'a> Visitor<'hir> for CrateResolutionVisitor<'a, 'hir> {
1178+
fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'hir> {
1179+
NestedVisitorMap::OnlyBodies(&self.0.hir)
1180+
}
1181+
fn visit_fn(&mut self, _fk: FnKind<'hir>, _fd: &'hir FnDecl,
1182+
_b: hir::BodyId, _s: Span, fn_id: NodeId)
1183+
{
1184+
let fn_def_id = self.0.hir.local_def_id(fn_id);
1185+
ty::queries::region_resolve_fn::get(self.0, DUMMY_SP, fn_def_id);
1186+
}
1187+
}
1188+
1189+
tcx.hir.krate().visit_all_item_likes(&mut CrateResolutionVisitor(tcx).as_deep_visitor());
11751190
}
11761191

11771192
fn region_resolve_fn<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, fn_id: DefId)
@@ -1202,7 +1217,8 @@ fn region_resolve_fn<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, fn_id: DefId)
12021217
var_parent: None,
12031218
},
12041219
terminating_scopes: NodeSet(),
1205-
target_fn_id: fn_id,
1220+
target_fn_node_id: hir_map.as_local_node_id(fn_id)
1221+
.expect("fn DefId should be for LOCAL_CRATE"),
12061222
found_target_fn: false,
12071223
};
12081224
krate.visit_all_item_likes(&mut visitor.as_deep_visitor());

src/librustc/ty/context.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -698,8 +698,19 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
698698
local as usize == global as usize
699699
}
700700

701-
pub fn region_maps(self) -> Rc<RegionMaps<'tcx>> {
702-
ty::queries::region_resolve_crate::get(self, DUMMY_SP, LOCAL_CRATE)
701+
pub fn region_maps(self, fn_id: NodeId) -> Rc<RegionMaps<'tcx>> {
702+
// Find the `NodeId` of the outermost function that wraps the function pointed to by fn_id
703+
let mut outermost_fn_id = fn_id;
704+
let mut outermost_id = fn_id;
705+
loop {
706+
let next_id = self.hir.get_parent(outermost_id);
707+
if outermost_id == next_id { break; }
708+
if self.hir.is_fn(outermost_id) {
709+
outermost_fn_id = outermost_id;
710+
}
711+
}
712+
713+
ty::queries::region_resolve_fn::get(self, DUMMY_SP, self.hir.local_def_id(outermost_fn_id))
703714
}
704715

705716
/// Create a type context and call the closure with a `TyCtxt` reference

0 commit comments

Comments
 (0)