[generator_interior] Be more precise with scopes of borrowed places#94309
Merged
bors merged 8 commits intorust-lang:masterfrom Mar 18, 2022
Merged
[generator_interior] Be more precise with scopes of borrowed places#94309bors merged 8 commits intorust-lang:masterfrom
bors merged 8 commits intorust-lang:masterfrom
Conversation
This comment has been minimized.
This comment has been minimized.
Collaborator
|
☔ The latest upstream changes (presumably #94350) made this pull request unmergeable. Please resolve the merge conflicts. |
nikomatsakis
requested changes
Feb 28, 2022
compiler/rustc_typeck/src/check/generator_interior/drop_ranges/record_consumed_borrow.rs
Outdated
Show resolved
Hide resolved
Collaborator
|
☔ The latest upstream changes (presumably #94634) made this pull request unmergeable. Please resolve the merge conflicts. |
Co-authored-by: Eric Holk <eric@theincredibleholk.org>
This is all almost certainly wrong
Also includes a lengthy comment arguing the correctness. Co-authored-by: Niko Matsakis <niko@alum.mit.edu>
Member
|
The change makes sense to me and is well documented. Thanks! @bors r+ |
Collaborator
|
📌 Commit 2fcd542 has been approved by |
Collaborator
|
🌲 The tree is currently closed for pull requests below priority 100. This pull request will be tested once the tree is reopened. |
bors
added a commit
to rust-lang-ci/rust
that referenced
this pull request
Mar 18, 2022
Rollup of 10 pull requests Successful merges: - rust-lang#91133 (Improve `unsafe` diagnostic) - rust-lang#93222 (Make ErrorReported impossible to construct outside `rustc_errors`) - rust-lang#93745 (Stabilize ADX target feature) - rust-lang#94309 ([generator_interior] Be more precise with scopes of borrowed places) - rust-lang#94698 (Remove redundant code from copy-suggestions) - rust-lang#94731 (Suggest adding `{ .. }` around a const function call with arguments) - rust-lang#94960 (Fix many spelling mistakes) - rust-lang#94982 (Add deprecated_safe feature gate and attribute, cc rust-lang#94978) - rust-lang#94997 (debuginfo: Fix ICE when generating name for type that produces a layout error.) - rust-lang#95000 (Fixed wrong type name in comment) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
flip1995
pushed a commit
to flip1995/rust
that referenced
this pull request
Mar 24, 2022
[generator_interior] Be more precise with scopes of borrowed places
Previously the generator interior type checking analysis would use the nearest temporary scope as the scope of a borrowed value. This ends up being overly broad for cases such as:
```rust
fn status(_client_status: &Client) -> i16 {
200
}
fn main() {
let client = Client;
let g = move || match status(&client) {
_status => yield,
};
assert_send(g);
}
```
In this case, the borrow `&client` could be considered in scope for the entirety of the `match` expression, meaning it would be viewed as live across the `yield`, therefore making the generator not `Send`.
In most cases, we want to use the enclosing expression as the scope for a borrowed value which will be less than or equal to the nearest temporary scope. This PR changes the analysis to use the enclosing expression as the scope for most borrows, with the exception of borrowed RValues which are true temporary values that should have the temporary scope. There's one further exception where borrows of a copy such as happens in autoref cases also should be ignored despite being RValues.
Joint work with `@nikomatsakis`
Fixes rust-lang#57017
r? `@tmandry`
bors
added a commit
to rust-lang-ci/rust
that referenced
this pull request
May 21, 2022
Drop Tracking: Implement `fake_read` callback This PR updates drop tracking's use of `ExprUseVisitor` so that we treat `fake_read` events as borrows. Without doing this, we were not handling match expressions correctly, which showed up as a breakage in the `addassign-yield.rs` test. We did not previously notice this because we still had rather large temporary scopes that we held borrows for, which changed in rust-lang#94309. This PR also includes a variant of the `addassign-yield.rs` test case to make sure we continue to have correct behavior here with drop tracking. r? `@nikomatsakis`
flip1995
pushed a commit
to flip1995/rust
that referenced
this pull request
Jun 4, 2022
Drop Tracking: Implement `fake_read` callback This PR updates drop tracking's use of `ExprUseVisitor` so that we treat `fake_read` events as borrows. Without doing this, we were not handling match expressions correctly, which showed up as a breakage in the `addassign-yield.rs` test. We did not previously notice this because we still had rather large temporary scopes that we held borrows for, which changed in rust-lang#94309. This PR also includes a variant of the `addassign-yield.rs` test case to make sure we continue to have correct behavior here with drop tracking. r? `@nikomatsakis`
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Previously the generator interior type checking analysis would use the nearest temporary scope as the scope of a borrowed value. This ends up being overly broad for cases such as:
In this case, the borrow
&clientcould be considered in scope for the entirety of thematchexpression, meaning it would be viewed as live across theyield, therefore making the generator notSend.In most cases, we want to use the enclosing expression as the scope for a borrowed value which will be less than or equal to the nearest temporary scope. This PR changes the analysis to use the enclosing expression as the scope for most borrows, with the exception of borrowed RValues which are true temporary values that should have the temporary scope. There's one further exception where borrows of a copy such as happens in autoref cases also should be ignored despite being RValues.
Joint work with @nikomatsakis
Fixes #57017
r? @tmandry