Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[drop tracking] Use parent expression for scope, not parent node #101217

Merged
merged 2 commits into from
Sep 3, 2022

Conversation

eholk
Copy link
Contributor

@eholk eholk commented Aug 30, 2022

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

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.
@rustbot rustbot added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Aug 30, 2022
@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Aug 30, 2022
@jyn514 jyn514 changed the title [drop tracking] Use parent expression for scope [drop tracking] Use parent expression for scope, not parent node Aug 30, 2022
Copy link
Member

@jyn514 jyn514 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are there any cases where this can have false positives? i.e. code that used to compile with drop-tracking no longer compiles?

compiler/rustc_typeck/src/check/generator_interior.rs Outdated Show resolved Hide resolved
compiler/rustc_typeck/src/check/generator_interior.rs Outdated Show resolved Hide resolved
let find_parent_expr = |mut hir_id| {
let hir = self.fcx.tcx.hir();
hir_id = hir.find_parent_node(hir_id)?;
loop {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, thanks! I didn't know about that method. I think that's way cleaner, so I rewrote my code to use that. I also added a doc comment to find_parent_node to point people towards parent_iter when more appropriate.

@jyn514 jyn514 added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Aug 31, 2022
@eholk
Copy link
Contributor Author

eholk commented Aug 31, 2022

Are there any cases where this can have false positives? i.e. code that used to compile with drop-tracking no longer compiles?

I don't think so. This is only observable if an await point is in the new wider scope but wasn't in the old narrower scope. In that case, MIR would have found the type and we should have gotten an ICE saying the types didn't match. At any rate, the old behavior was incorrect (as shown by how issue-73137.rs broke with drop tracking), so it seems better to correct that.

@jyn514
Copy link
Member

jyn514 commented Aug 31, 2022

@bors r+

@bors
Copy link
Contributor

bors commented Aug 31, 2022

📌 Commit f921f56 has been approved by jyn514

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Aug 31, 2022
Dylan-DPC added a commit to Dylan-DPC/rust that referenced this pull request Sep 1, 2022
[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 rust-lang#97331

r? `@jyn514`
Dylan-DPC added a commit to Dylan-DPC/rust that referenced this pull request Sep 2, 2022
[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 rust-lang#97331

r? `@jyn514`
bors added a commit to rust-lang-ci/rust that referenced this pull request Sep 3, 2022
Rollup of 7 pull requests

Successful merges:

 - rust-lang#99736 (Partially stabilize `bound_as_ref` by stabilizing `Bound::as_ref`)
 - rust-lang#100928 (Migrate rustc_metadata to SessionDiagnostics)
 - rust-lang#101217 ([drop tracking] Use parent expression for scope, not parent node )
 - rust-lang#101325 (Windows RNG: Use `BCRYPT_RNG_ALG_HANDLE` by default)
 - rust-lang#101330 (Fix `std::collections::HashSet::drain` documentation)
 - rust-lang#101338 (Fix unsupported syntax in .manifest file)
 - rust-lang#101348 (Cleanup css theme)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit dc8fe63 into rust-lang:master Sep 3, 2022
@rustbot rustbot added this to the 1.65.0 milestone Sep 3, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants