Skip to content

Commit dc8fe63

Browse files
authored
Rollup merge of #101217 - eholk:drop-tracking-73137, r=jyn514
[drop tracking] Use parent expression for scope, not parent node Previously we were just using the parent node as the scope for a temporary value, but it turns out this is too narrow. For example, in an expression like Foo { b: &42, a: async { 0 }.await, } the scope for the &42 was set to the ExprField node for `b: &42`, when we actually want to use the Foo struct expression. We fix this by recursively searching through parent nodes until we find a Node::Expr. It may be that we don't find one, and if so that's okay, we will just fall back on the enclosing temporary scope which is always sufficient. Helps with #97331 r? ``@jyn514``
2 parents a005679 + f921f56 commit dc8fe63

File tree

4 files changed

+17
-4
lines changed

4 files changed

+17
-4
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

+9-2
Original file line numberDiff line numberDiff line change
@@ -408,8 +408,15 @@ impl<'a, 'tcx> Visitor<'tcx> for InteriorVisitor<'a, 'tcx> {
408408
}) {
409409
self.rvalue_scopes.temporary_scope(self.region_scope_tree, expr.hir_id.local_id)
410410
} else {
411-
debug!("parent_node: {:?}", self.fcx.tcx.hir().find_parent_node(expr.hir_id));
412-
match self.fcx.tcx.hir().find_parent_node(expr.hir_id) {
411+
let parent_expr = self
412+
.fcx
413+
.tcx
414+
.hir()
415+
.parent_iter(expr.hir_id)
416+
.find(|(_, node)| matches!(node, hir::Node::Expr(_)))
417+
.map(|(id, _)| id);
418+
debug!("parent_expr: {:?}", parent_expr);
419+
match parent_expr {
413420
Some(parent) => Some(Scope { id: parent.local_id, data: ScopeData::Node }),
414421
None => {
415422
self.rvalue_scopes.temporary_scope(self.region_scope_tree, expr.hir_id.local_id)

compiler/rustc_typeck/src/check/generator_interior/drop_ranges/record_consumed_borrow.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,8 @@ impl<'tcx> expr_use_visitor::Delegate<'tcx> for ExprUseDelegate<'tcx> {
159159
bk: rustc_middle::ty::BorrowKind,
160160
) {
161161
debug!(
162-
"borrow: place_with_id = {place_with_id:?}, diag_expr_id={diag_expr_id:?}, \
163-
borrow_kind={bk:?}"
162+
"borrow: place_with_id = {place_with_id:#?}, diag_expr_id={diag_expr_id:#?}, \
163+
borrow_kind={bk:#?}"
164164
);
165165

166166
self.borrow_place(place_with_id);

src/test/ui/async-await/issue-73137.rs

+3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
// run-pass
44
// edition:2018
5+
// revisions: normal drop-tracking
6+
// [normal]compile-flags: -Zdrop-tracking=no
7+
// [drop-tracking]compile-flags: -Zdrop-tracking
58

69
#![allow(dead_code)]
710
use std::future::Future;

0 commit comments

Comments
 (0)