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

Two phase borrows rewrite #48770

Merged
merged 9 commits into from Mar 12, 2018

Conversation

sapphire-arches
Copy link
Contributor

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

@rust-highfive
Copy link
Collaborator

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.

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Mar 6, 2018

// The following revisions are disabled due to missing support from two-phase beyond autorefs
Copy link
Contributor Author

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.

Copy link
Member

@pnkfelix pnkfelix Mar 9, 2018

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
Copy link
Contributor Author

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.

Copy link
Member

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
Copy link
Contributor Author

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
Copy link
Contributor Author

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.

Copy link
Member

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.

@pnkfelix
Copy link
Member

pnkfelix commented Mar 8, 2018

@bobtwinkles thanks for the PR, I plan to review on Friday

@bors
Copy link
Contributor

bors commented Mar 9, 2018

☔ 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
Copy link
Member

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
Copy link
Member

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!

Copy link
Member

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...)

Copy link
Contributor Author

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);
Copy link
Member

@pnkfelix pnkfelix Mar 9, 2018

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.

Copy link
Contributor Author

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
Copy link
Member

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> {
Copy link
Member

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

@@ -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
Copy link
Member

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

@pnkfelix
Copy link
Member

pnkfelix commented Mar 9, 2018

@bobtwinkles eek, please do not merge master branch into PRs. Use git rebase instead to reroot your PR atop the latest master. Thanks!

@pnkfelix
Copy link
Member

pnkfelix commented Mar 9, 2018

@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 -Z nll suddenly went away except for when you started passing -Z two-phase-borrows as well.

@pietroalbini pietroalbini 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 Mar 9, 2018
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)
@sapphire-arches
Copy link
Contributor Author

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 ❤️.

@nikomatsakis nikomatsakis removed their assignment Mar 9, 2018
It seems whatever was causing problems has been fixed.
Left over from prior experimentation, no longer required.
@sapphire-arches
Copy link
Contributor Author

Huh, looks like I outplayed myself with the region-ends-after-if-condition test. That change must have been left over from when I was playing with further cleaning up the code in borrow_check/mod.rs....

@pnkfelix
Copy link
Member

@bors r+

@bors
Copy link
Contributor

bors commented Mar 12, 2018

📌 Commit 9a5d61a has been approved by pnkfelix

@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 Mar 12, 2018
@bors
Copy link
Contributor

bors commented Mar 12, 2018

⌛ Testing commit 9a5d61a with merge 883e746...

bors added a commit that referenced this pull request Mar 12, 2018
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
@bors
Copy link
Contributor

bors commented Mar 12, 2018

☀️ Test successful - status-appveyor, status-travis
Approved by: pnkfelix
Pushing 883e746 to master...

@bors bors merged commit 9a5d61a into rust-lang:master Mar 12, 2018
sapphire-arches added a commit to sapphire-arches/rust that referenced this pull request Mar 13, 2018
Resolves rust-lang#48070.
The bug itself was fixed by rust-lang#48770, but that PR didn't add a test for it.
kennytm added a commit to kennytm/rust that referenced this pull request Mar 14, 2018
…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
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.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants