Skip to content

Commit ec23e3f

Browse files
committed
Convert fn_id to outermost in region_maps
1 parent 10c232a commit ec23e3f

File tree

3 files changed

+46
-8
lines changed

3 files changed

+46
-8
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 & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ struct RegionResolutionVisitor<'hir: 'a, 'a> {
348348
/// destructor's execution.
349349
terminating_scopes: NodeSet,
350350

351-
target_fn_id: DefId,
351+
target_fn_node_id: NodeId,
352352

353353
found_target_fn: bool,
354354
}
@@ -1142,11 +1142,10 @@ fn resolve_fn<'a, 'tcx>(visitor: &mut RegionResolutionVisitor<'tcx, 'a>,
11421142
body_id: hir::BodyId,
11431143
sp: Span,
11441144
id: ast::NodeId) {
1145-
11461145
visitor.cx.parent = visitor.new_code_extent(
11471146
CodeExtentData::CallSiteScope { fn_id: id, body_id: body_id.node_id });
11481147

1149-
if body_id == visitor.target_fn_id {
1148+
if id == visitor.target_fn_node_id {
11501149
// We've found the top level `fn`. Store it and its children in the `RegionMaps`
11511150
visitor.found_target_fn = true;
11521151
}
@@ -1270,8 +1269,23 @@ impl<'hir, 'a> Visitor<'hir> for RegionResolutionVisitor<'hir, 'a> {
12701269
}
12711270
}
12721271

1273-
pub fn resolve_crate<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) -> Rc<RegionMaps> {
1274-
ty::queries::region_resolve_fn::get(tcx, DUMMY_SP, LOCAL_CRATE)
1272+
pub fn resolve_crate<'a, 'tcx: 'a>(tcx: TyCtxt<'a, 'tcx, 'tcx>) {
1273+
1274+
struct CrateResolutionVisitor<'a, 'tcx: 'a>(TyCtxt<'a, 'tcx, 'tcx>);
1275+
1276+
impl<'a, 'hir: 'a> Visitor<'hir> for CrateResolutionVisitor<'a, 'hir> {
1277+
fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'hir> {
1278+
NestedVisitorMap::OnlyBodies(&self.0.hir)
1279+
}
1280+
fn visit_fn(&mut self, _fk: FnKind<'hir>, _fd: &'hir FnDecl,
1281+
_b: hir::BodyId, _s: Span, fn_id: NodeId)
1282+
{
1283+
let fn_def_id = self.0.hir.local_def_id(fn_id);
1284+
ty::queries::region_resolve_fn::get(self.0, DUMMY_SP, fn_def_id);
1285+
}
1286+
}
1287+
1288+
tcx.hir.krate().visit_all_item_likes(&mut CrateResolutionVisitor(tcx).as_deep_visitor());
12751289
}
12761290

12771291
fn region_resolve_fn<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, fn_id: DefId)
@@ -1308,7 +1322,8 @@ fn region_resolve_fn<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, fn_id: DefId)
13081322
var_parent: ROOT_CODE_EXTENT
13091323
},
13101324
terminating_scopes: NodeSet(),
1311-
target_fn_id: fn_id,
1325+
target_fn_node_id: hir_map.as_local_node_id(fn_id)
1326+
.expect("fn DefId should be for LOCAL_CRATE"),
13121327
found_target_fn: false,
13131328
};
13141329
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
@@ -663,8 +663,19 @@ 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)
666+
pub fn region_maps(self, fn_id: NodeId) -> Rc<RegionMaps> {
667+
// Find the `NodeId` of the outermost function that wraps the function pointed to by fn_id
668+
let mut outermost_fn_id = fn_id;
669+
let mut outermost_id = fn_id;
670+
loop {
671+
let next_id = self.hir.get_parent(outermost_id);
672+
if outermost_id == next_id { break; }
673+
if self.hir.is_fn(outermost_id) {
674+
outermost_fn_id = outermost_id;
675+
}
676+
}
677+
678+
ty::queries::region_resolve_fn::get(self, DUMMY_SP, self.hir.local_def_id(outermost_fn_id))
668679
}
669680

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

0 commit comments

Comments
 (0)