1
Fork 0

Use parent_iter instead of a find_parent_node loop

This commit is contained in:
Eric Holk 2022-08-31 11:10:19 -07:00
parent b9d3f65412
commit f921f5626d
2 changed files with 10 additions and 13 deletions

View file

@ -291,6 +291,9 @@ impl<'hir> Map<'hir> {
Some(def_kind) 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> { pub fn find_parent_node(self, id: HirId) -> Option<HirId> {
if id.local_id == ItemLocalId::from_u32(0) { if id.local_id == ItemLocalId::from_u32(0) {
Some(self.tcx.hir_owner_parent(id.owner)) Some(self.tcx.hir_owner_parent(id.owner))

View file

@ -387,18 +387,6 @@ impl<'a, 'tcx> Visitor<'tcx> for InteriorVisitor<'a, 'tcx> {
ty.needs_drop(self.fcx.tcx, self.fcx.param_env) 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, // 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 // 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 // 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> {
}) { }) {
self.rvalue_scopes.temporary_scope(self.region_scope_tree, expr.hir_id.local_id) self.rvalue_scopes.temporary_scope(self.region_scope_tree, expr.hir_id.local_id)
} else { } 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); debug!("parent_expr: {:?}", parent_expr);
match parent_expr { match parent_expr {
Some(parent) => Some(Scope { id: parent.local_id, data: ScopeData::Node }), Some(parent) => Some(Scope { id: parent.local_id, data: ScopeData::Node }),