-
-
Notifications
You must be signed in to change notification settings - Fork 15.5k
borrowck dataflow: respect borrow shallowness when killing borrows that conflict with assignments #160902
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
Open
dianne
wants to merge
1
commit into
rust-lang:main
Choose a base branch
from
dianne:preserve-shallow
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+59
−4
Open
borrowck dataflow: respect borrow shallowness when killing borrows that conflict with assignments #160902
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
32 changes: 32 additions & 0 deletions
32
tests/ui/borrowck/dont-kill-shallow-borrows-on-child-writes.rs
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| //! Regression test for <https://github.com/rust-lang/rust/issues/160599>. On assignment statements, | ||
| //! borrowck's dataflow analysis kills borrows that it knows conflict with the assigment: a borrow | ||
| //! of a place can't be live anymore after that place is assigned over. Previously, this didn't | ||
| //! account for fake borrows being shallow: it would kill any shallow borrows that would have | ||
| //! conflicted if they were normal borrows. This made it possible to circumvent fake borrows for | ||
| //! match guards and indexing expressions. | ||
|
|
||
| fn test_match_guard() { | ||
| let mut a = (Some(&42u64), 0u8); | ||
| let mut b = (None::<&u64>, 0u8); | ||
| let mut p = &mut a; | ||
| // Writing to `(*p).1` in the match guard previously killed the fake borrow of `p` in the guard, | ||
| // making it possible to mutate `p` despite `(*p).0` being matched on. This would reach the | ||
| // `Some(r)` branch with `(*p).0` being `None`, so the `r` binding was invalid. | ||
| match p.0 { | ||
| Some(_) if { p.1 = 1; p = &mut b; false } => unreachable!(), | ||
| //~^ ERROR: cannot assign `p` in match guard | ||
| Some(r) => println!("{r}"), | ||
| None => unreachable!(), | ||
| } | ||
| } | ||
|
|
||
| fn test_indexing() { | ||
| let mut x: &mut [&mut [u32]] = &mut [&mut [0]]; | ||
| let y: &mut [&mut [u32]] = &mut []; | ||
| // Writing to `x[0][0]` previously killed the fake borrow of `x` in the index expression, making | ||
| // it possible to access `y[0]` without a bounds-check. | ||
| x[0][{ x[0][0] = 1; x = y; 0 }]; | ||
| //~^ ERROR: cannot assign `x` in indexing expression | ||
| } | ||
|
|
||
| fn main() {} |
19 changes: 19 additions & 0 deletions
19
tests/ui/borrowck/dont-kill-shallow-borrows-on-child-writes.stderr
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| error[E0510]: cannot assign `p` in match guard | ||
| --> $DIR/dont-kill-shallow-borrows-on-child-writes.rs:16:31 | ||
| | | ||
| LL | match p.0 { | ||
| | --- value is immutable in match guard | ||
| LL | Some(_) if { p.1 = 1; p = &mut b; false } => unreachable!(), | ||
| | ^^^^^^^^^^ cannot assign | ||
|
|
||
| error[E0510]: cannot assign `x` in indexing expression | ||
| --> $DIR/dont-kill-shallow-borrows-on-child-writes.rs:28:25 | ||
| | | ||
| LL | x[0][{ x[0][0] = 1; x = y; 0 }]; | ||
| | ---- ^^^^^ cannot assign | ||
| | | | ||
| | value is immutable in indexing expression | ||
|
|
||
| error: aborting due to 2 previous errors | ||
|
|
||
| For more information about this error, try `rustc --explain E0510`. |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I copied the
AccessDepth::Deepfromplaces_conflict::places_conflict, so this will have the same behavior as before in that regard, but I'm wary about it.kill_borrows_on_placeis called on assignments andStorageDeads, which are shallow accesses. Consequently,definitely_conflicting_borrowscan contain non-conflicting borrows, which are then killed. e.g.I haven't been able to coax unsoundness out of it, but it feels strange. I tried changing it to
AccessDepth::Shallow(None)to see what would happen, but it broke some tests that rely on non-conflicting borrows being killed, e.g. tests/ui/borrowck/issue-62007-assign-box.rs and tests/ui/borrowck/issue-62007-assign-field.rs. As I understand it, the borrows there go through derefs, so the assignments don't technically conflict with them (as in the above example), but they need to be killed for the loops to work.The fast path for assignments to locals also seems not to account for assignments being shallow accesses. e.g.
Maybe I'm missing something?
View changes since the review
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is definitely a part of borrowck with a confusing implementation. The check here is looking for cases where the assignment overwrites the reference to the borrowed place. The assumption is that this would be cases where there is a conflicts with
AccessDepth::Deepbut notAccessDepth::Shallow(None). The reason that the kill only checksAccessDepth::Deephere is that the check withAccessDepth::Shallow(None)invisit_after_early_statement_effectis done on the borrow state before the kill happens, so there's an error either way.