Skip to content

Commit

Permalink
Use parent_iter instead of a find_parent_node loop
Browse files Browse the repository at this point in the history
  • Loading branch information
eholk committed Aug 31, 2022
1 parent b9d3f65 commit f921f56
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 13 deletions.
3 changes: 3 additions & 0 deletions compiler/rustc_middle/src/hir/map/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,9 @@ impl<'hir> Map<'hir> {
Some(def_kind)
}

/// Finds the id of the parent node to this one.
///
/// If calling repeatedly and iterating over parents, prefer [`Map::parent_iter`].
pub fn find_parent_node(self, id: HirId) -> Option<HirId> {
if id.local_id == ItemLocalId::from_u32(0) {
Some(self.tcx.hir_owner_parent(id.owner))
Expand Down
20 changes: 7 additions & 13 deletions compiler/rustc_typeck/src/check/generator_interior.rs
Original file line number Diff line number Diff line change
Expand Up @@ -387,18 +387,6 @@ impl<'a, 'tcx> Visitor<'tcx> for InteriorVisitor<'a, 'tcx> {
ty.needs_drop(self.fcx.tcx, self.fcx.param_env)
};

let find_parent_expr = |mut hir_id| {
let hir = self.fcx.tcx.hir();
hir_id = hir.find_parent_node(hir_id)?;
loop {
if let hir::Node::Expr(_) = self.fcx.tcx.hir().find(hir_id)? {
return Some(hir_id);
} else {
hir_id = hir.find_parent_node(hir_id)?;
}
}
};

// Typically, the value produced by an expression is consumed by its parent in some way,
// so we only have to check if the parent contains a yield (note that the parent may, for
// example, store the value into a local variable, but then we already consider local
Expand All @@ -421,7 +409,13 @@ impl<'a, 'tcx> Visitor<'tcx> for InteriorVisitor<'a, 'tcx> {
}) {
self.rvalue_scopes.temporary_scope(self.region_scope_tree, expr.hir_id.local_id)
} else {
let parent_expr = find_parent_expr(expr.hir_id);
let parent_expr = self
.fcx
.tcx
.hir()
.parent_iter(expr.hir_id)
.find(|(_, node)| matches!(node, hir::Node::Expr(_)))
.map(|(id, _)| id);
debug!("parent_expr: {:?}", parent_expr);
match parent_expr {
Some(parent) => Some(Scope { id: parent.local_id, data: ScopeData::Node }),
Expand Down

0 comments on commit f921f56

Please sign in to comment.