-
Notifications
You must be signed in to change notification settings - Fork 13k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix spurious error when a
Drop
local has an assignment in a loop
`DropAndReplace` terminators are special - unlike all other terminators, they perform two distinct actions (a drop and a store) to the same `Place`. This complicates various analysis passes that we do, which expect at most one of 'Drop'/'Def'/'Use' for a local at a given MIR location. Previously, we categorized a `DropAndReplace` terminator's `Local` access as `MutatingUseContext::Drop`. As a result, livness computation would not consider the `DropAndReplace` as a store ('Def') of a local. The "use live" set would end up being too large (a use would be seen to extend back to an earlier store, rather than the closure `DropAndReplace`). We now explicitly propagate information about `DropAndReplace` terminators via new enum variants `MutatingUseContext:DropAndReplace` and `DefUse::DropAndReplace`. We use this information in liveness computation to record *both* a Drop and a Def. This prevents creating an extra-large "use live" set, while ensuring that te local is still considered "drop live" at the required locations.
- Loading branch information
Showing
10 changed files
with
104 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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,24 @@ | ||
// A version of `issue-70919-drop-in-loop`, but without | ||
// the necessary `drop` call. | ||
// | ||
// This should fail to compile, since the `Drop` impl | ||
// for `WrapperWithDrop` could observe the changed | ||
// `base` value. | ||
|
||
struct WrapperWithDrop<'a>(&'a mut bool); | ||
impl<'a> Drop for WrapperWithDrop<'a> { | ||
fn drop(&mut self) { | ||
} | ||
} | ||
|
||
fn drop_in_loop() { | ||
let mut base = true; | ||
let mut wrapper = WrapperWithDrop(&mut base); | ||
loop { | ||
base = false; //~ ERROR: cannot assign to `base` | ||
wrapper = WrapperWithDrop(&mut base); | ||
} | ||
} | ||
|
||
fn main() { | ||
} |
This file contains 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,14 @@ | ||
error[E0506]: cannot assign to `base` because it is borrowed | ||
--> $DIR/drop-in-loop.rs:18:9 | ||
| | ||
LL | let mut wrapper = WrapperWithDrop(&mut base); | ||
| --------- borrow of `base` occurs here | ||
LL | loop { | ||
LL | base = false; | ||
| ^^^^^^^^^^^^ assignment to borrowed `base` occurs here | ||
LL | wrapper = WrapperWithDrop(&mut base); | ||
| ------- borrow might be used here, when `wrapper` is dropped and runs the `Drop` code for type `WrapperWithDrop` | ||
|
||
error: aborting due to previous error | ||
|
||
For more information about this error, try `rustc --explain E0506`. |
This file contains 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,25 @@ | ||
// Regression test for issue #70919 | ||
// Tests that we don't emit a spurious "borrow might be used" error | ||
// when we have an explicit `drop` in a loop | ||
|
||
// check-pass | ||
|
||
struct WrapperWithDrop<'a>(&'a mut bool); | ||
impl<'a> Drop for WrapperWithDrop<'a> { | ||
fn drop(&mut self) { | ||
} | ||
} | ||
|
||
fn drop_in_loop() { | ||
let mut base = true; | ||
let mut wrapper = WrapperWithDrop(&mut base); | ||
loop { | ||
drop(wrapper); | ||
|
||
base = false; | ||
wrapper = WrapperWithDrop(&mut base); | ||
} | ||
} | ||
|
||
fn main() { | ||
} |