Skip to content

Commit f921f56

Browse files
committed
Use parent_iter instead of a find_parent_node loop
1 parent b9d3f65 commit f921f56

File tree

2 files changed

+10
-13
lines changed

2 files changed

+10
-13
lines changed

compiler/rustc_middle/src/hir/map/mod.rs

+3
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,9 @@ impl<'hir> Map<'hir> {
291291
Some(def_kind)
292292
}
293293

294+
/// Finds the id of the parent node to this one.
295+
///
296+
/// If calling repeatedly and iterating over parents, prefer [`Map::parent_iter`].
294297
pub fn find_parent_node(self, id: HirId) -> Option<HirId> {
295298
if id.local_id == ItemLocalId::from_u32(0) {
296299
Some(self.tcx.hir_owner_parent(id.owner))

compiler/rustc_typeck/src/check/generator_interior.rs

+7-13
Original file line numberDiff line numberDiff line change
@@ -387,18 +387,6 @@ impl<'a, 'tcx> Visitor<'tcx> for InteriorVisitor<'a, 'tcx> {
387387
ty.needs_drop(self.fcx.tcx, self.fcx.param_env)
388388
};
389389

390-
let find_parent_expr = |mut hir_id| {
391-
let hir = self.fcx.tcx.hir();
392-
hir_id = hir.find_parent_node(hir_id)?;
393-
loop {
394-
if let hir::Node::Expr(_) = self.fcx.tcx.hir().find(hir_id)? {
395-
return Some(hir_id);
396-
} else {
397-
hir_id = hir.find_parent_node(hir_id)?;
398-
}
399-
}
400-
};
401-
402390
// Typically, the value produced by an expression is consumed by its parent in some way,
403391
// so we only have to check if the parent contains a yield (note that the parent may, for
404392
// example, store the value into a local variable, but then we already consider local
@@ -421,7 +409,13 @@ impl<'a, 'tcx> Visitor<'tcx> for InteriorVisitor<'a, 'tcx> {
421409
}) {
422410
self.rvalue_scopes.temporary_scope(self.region_scope_tree, expr.hir_id.local_id)
423411
} else {
424-
let parent_expr = find_parent_expr(expr.hir_id);
412+
let parent_expr = self
413+
.fcx
414+
.tcx
415+
.hir()
416+
.parent_iter(expr.hir_id)
417+
.find(|(_, node)| matches!(node, hir::Node::Expr(_)))
418+
.map(|(id, _)| id);
425419
debug!("parent_expr: {:?}", parent_expr);
426420
match parent_expr {
427421
Some(parent) => Some(Scope { id: parent.local_id, data: ScopeData::Node }),

0 commit comments

Comments
 (0)