-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Two phase borrows rewrite #48770
Two phase borrows rewrite #48770
Conversation
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @pnkfelix (or someone else) soon. If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes. Please see the contribution instructions for more information. |
|
||
// The following revisions are disabled due to missing support from two-phase beyond autorefs |
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'm not super happy about this, but I wanted to get reviews kicked off before I wandered off in to the weeds trying to get 2-phase beyond autoref working.
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 actually don't think you/we should worry about trying to support 2-phase borrow beyond autoref.
I had originally thought the general idea could make sense, but at this point I think there are enough cases where it does not work that it would not be a good use of time to try to hack it together. Or at least, I'd want to see a more theoretically proof of soundness for the idea before we implemented anything.
@@ -51,7 +53,6 @@ fn not_ok() { | |||
*y += 1; | |||
//[lxl_beyond]~^ ERROR cannot borrow `x` as mutable because it is also borrowed as immutable | |||
//[nll_beyond]~^^ ERROR cannot borrow `x` as mutable because it is also borrowed as immutable | |||
//[nll_target]~^^^ ERROR cannot borrow `x` as mutable because it is also borrowed as immutable |
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.
It seems like this error was spurious? While it's technically correct, we're already reporting one borrow error a few lines above involving the same variables so I don't /think/ this error being missing is an indication of unsoundness. I would really appreciate another pair of eyes on this though.
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 wouldn't be surprised if these were indeed instances of the spurious errors due to treating a series of uses in the MIR as distinct activations, as described in #48418)
//[nll]~| ERROR cannot borrow `*f` as mutable more than once at a time | ||
//[g2p]~^^^^^ ERROR cannot borrow `*f` as mutable more than once at a time | ||
//[nll]~^^ ERROR cannot borrow `*f` as mutable more than once at a time | ||
//[g2p]~^^^ ERROR cannot borrow `*f` as mutable more than once at a time |
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.
Here again it seemed like we were reporting duplicate errors; but again I'm not convinced.
@@ -12,7 +12,7 @@ | |||
// in the type of `p` includes the points after `&v[0]` up to (but not | |||
// including) the call to `use_x`. The `else` branch is not included. | |||
|
|||
// compile-flags:-Zborrowck=compare -Znll | |||
// compile-flags:-Zborrowck=compare -Znll -Ztwo-phase-borrows |
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.
Without two-phase borrows this test outright breaks. This is probably the thing I'm least happy with, as it really indicates that some form of unsoundness is introduced with my changes here. The specific symptom is that MIR borrowck no longer reports E0502 on line 41 without two-phase-borrows
enabled.
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.
Yes I think you are right that this is worrisome, possibly a symptom of some latent bug in the PR. I'll try to take some time on Monday to dig into it if you don't resolve it yourself today or over the weekend.
a0dcce4
to
42d4890
Compare
@bobtwinkles thanks for the PR, I plan to review on Friday |
☔ The latest upstream changes (presumably #48849) made this pull request unmergeable. Please resolve the merge conflicts. |
/// No usage seen | ||
None, | ||
/// Has been seen as the argument to a StorageDead statement. This is required to | ||
/// gracefully handle cases where user code has an unneeded |
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.
"where user code has an unneeded ..." sentence needs an ending.
|
||
/// Computes the activation location of a borrow. | ||
/// The general idea is to start at the beginning of the region and perform a DFS | ||
/// until we exit the region, either via an explicit EndRegion or because NLL tells |
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.
oh wow, you got this working without NLL, and just lexical regions via EndRegion
? How cool!
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.
(or maybe you don't have EndRegion
support completely baked yet? I saw evidence of some XXX-marked code that came and went...)
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.
It does work without NLL; since we're walking the MIR statement by statement and only considering one region at a time we can just look for the explicit EndRegion
instead of asking the NLL region inference engine where the region ends. The code that came and went was me playing with the idea of handling all kills for borrows going out of scope in kill_borrows_out_of_scope_at_location
but it ended up being cleaner to leave the existing implementation (where EndRegion
gets handled in the big match
in statement_effect
) IMO.
// Right now this is sufficient though since there should only be exactly | ||
// one borrow-activating use of the borrow. | ||
assert!(found_use.is_none(), "Found secondary use of place"); | ||
found_use = Some(curr_loc); |
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.
Hmm, I would have expected you to continue
to the top of the while let
here, rather than pushing the points in the control-flow that succeed the use that you have found. I don't mind pushing those points (traversing them represents a small bit of extra effort, I suspect), I just found it a little surprising.
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 agree, that should be sufficient. I wrote it this way in case we wanted to support general two-phase but if that's not of interest (as indicated by some comments above) I'll use the continue instead.
is_activations: bool) { | ||
location: Location) { | ||
/* | ||
XXX: bob_twinkles reintroduce this |
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.
ah, maybe you don't yet have EndRegion
-based support working after all? (or maybe I'll see somewhere else in the PR that they get supported in some manner... I'm going through the PR commit-by-commit at the moment.)
@@ -525,161 +762,6 @@ impl<'a, 'gcx, 'tcx> Borrows<'a, 'gcx, 'tcx> { | |||
mir::TerminatorKind::Unreachable => {} | |||
} | |||
} | |||
} | |||
|
|||
impl<'a, 'gcx, 'tcx> ActiveBorrows<'a, 'gcx, 'tcx> { |
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.
hooray, goodbye to impl ActiveBorrows
src/test/ui/issue-45157.stderr
Outdated
@@ -7,15 +7,6 @@ LL | let mref = &mut u.s.a; | |||
LL | let nref = &u.z.c; | |||
| ^^^^^^ immutable borrow occurs here | |||
|
|||
error[E0502]: cannot borrow `u.s.a` as mutable because it is also borrowed as immutable |
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.
Hmm I think this is okay that this error went away, but I'll want to double check
@bobtwinkles eek, please do not merge master branch into PRs. Use |
@bobtwinkles I'm pretty happy with much of this. I think the main thing we want to do is get to the bottom of that test you noted where the error message under |
in preparation for rewritting two phase borrow support
The new output format is perhaps a little more readable. As a bonus, we get labels on the outgoing edges to more easily corroborate the dataflow with the plain MIR graphviz output.
See rust-lang#48431 for discussion as to why this was necessary and what we hoped to accomplish. A brief summary: - the first implementation of 2-phase borrows was hard to limit in the way we wanted. That is, it was too good at accepting all 2-phase borrows rather than just autorefs =) - Numerous diagnostic regressions were introduced by 2-phase borrow support which were difficult to fix
Fix a small compilation issue after I missed a critical change after rebasing yesterday (ref c933440)
a6c42ec
to
2ed0f51
Compare
Rebased properly this time; I thought the Github UI would be smarter ;(. Sorry about that. I'll work through your other comments shortly. Thanks for the review ❤️. |
It seems whatever was causing problems has been fixed.
Left over from prior experimentation, no longer required.
Huh, looks like I outplayed myself with the |
@bors r+ |
📌 Commit 9a5d61a has been approved by |
Two phase borrows rewrite This definitely needs a careful review. Both @pnkfelix and @nikomatsakis were involved with the design of this so they're natural choices here. I'm r?'ing @pnkfelix since they wrote the original two-phase borrow implementation. Also ping @KiChjang who expressed interest in working on this. I'm going to leave a few comments below pointing out some of the more dangerous changes I made (i.e. what I would like reviewers to pay special attention too.) r? @pnkfelix
☀️ Test successful - status-appveyor, status-travis |
Resolves rust-lang#48070. The bug itself was fixed by rust-lang#48770, but that PR didn't add a test for it.
…atsakis Add a test for rust-lang#48070 Resolves rust-lang#48070. The bug itself was fixed by rust-lang#48770, but that PR didn't add a test for it. r? @nikomatsakis
This definitely needs a careful review. Both @pnkfelix and @nikomatsakis were involved with the design of this so they're natural choices here. I'm r?'ing @pnkfelix since they wrote the original two-phase borrow implementation. Also ping @KiChjang who expressed interest in working on this. I'm going to leave a few comments below pointing out some of the more dangerous changes I made (i.e. what I would like reviewers to pay special attention too.)
r? @pnkfelix