-
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
[NLL] Make causal tracking lazy #48682
Conversation
r? @estebank (rust_highfive has picked a reviewer for you, use r? to override) |
} else { | ||
None | ||
}, | ||
causes: Some(CauseMap::default()), |
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 seems ok but mildly surprising -- that is, I think we should adjust some comments. I guess I would like a comment on new
like this:
Creates a new set of "region values" that tracks causal information. Each of the regions in num_region_variables
will be initialized with an empty set of points and no causal information.
Then I would expect to remove the Clone
impl and add a new method like:
// This is what we use at first...
fn clone_not_tracking_causes(&self) -> Self { .. }
// ...this is what `clone` is today
fn clone_tracking_causes(&self) -> Self { .. }
because of an outlives relation created at `bb0[1]` | ||
because of an outlives relation created at `bb0[1]` | ||
because of an outlives relation created at `bb0[0]` | ||
because `'_#3r` is universally quantified |
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.
didn't we remove this output? I guess this PR is independent from that one?
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, the other PR is not in master yet.
| | - | ||
| | | | ||
| |_temporary value only lives until here | ||
| borrow later used here |
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 very odd looking.
It seems to me that 1) there's a bug in the graphing code for a multiline span ending in the same char as a single line span, and 2) we should probably only show one of these spans, as they do not add more information.
Personally, I would prefer the output to look more like this:
error[E0597]: borrowed value does not live long enough
--> $DIR/return-ref-mut-issue-46557.rs:17:21
|
LL | fn gimme_static_mut() -> &'static mut u32 {
| ---------------- the returned value needs to live for the duration of the `'static` lifetime
LL | let ref mut x = 1234543;
| ^^^^^^^ value does not live long enough
LL | x
| - value is being returned here
LL | }
| - value only lives until here
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.
@estebank @nikomatsakis I thought exactly the same thing. Waht about opening a different issue to avoid tackling a lot of different things here?.
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.
👍
6f59d5f
to
ca64989
Compare
☔ The latest upstream changes (presumably #48592) made this pull request unmergeable. Please resolve the merge conflicts. |
cba6cf3
to
a8fbb11
Compare
src/librustc_mir/borrow_check/mod.rs
Outdated
R: ToRegionVid, | ||
{ | ||
let inferred_values = &self.nonlexical_cause_info.as_ref() | ||
.expect("region values not yet inferred") |
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.
Nit: this should say "region cause information not yet populated" or something like that
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 think I had expected to put this method onto the RegionCausalInfo
, precisely so that the "unwrap/expect" question is pushed up to the caller. This way is OK too, but I mildly prefer it to be moved.
@@ -125,6 +123,10 @@ pub(crate) enum Cause { | |||
}, | |||
} | |||
|
|||
pub(crate) struct RegionCausalInfo { | |||
pub(crate) inferred_values: RegionValues, |
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 field should not be public, I think -- RegionValues
were meant to be encapsulated within the region_infer
module. If we move the why_borrow_contains_point
method onto the RegionCausalInfo
struct, that would work out fine.
self.inferred_values = Some(causal_info.inferred_values); | ||
} | ||
|
||
pub(crate) fn compute_causal_info2(&self, mir: &Mir<'tcx>, track_causes: bool) -> RegionCausalInfo { |
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 shouldn't be to be pub(crate)
, right?
let mut constraints: Vec<_> = self.constraints.iter().collect(); | ||
constraints.sort(); | ||
constraints | ||
}); | ||
|
||
// The initial values for each region are derived from the liveness | ||
// constraints we have accumulated. | ||
let mut inferred_values = self.liveness_constraints.clone(); | ||
let mut inferred_values = |
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.
Rather than pass a flag, I personally would have this take as argument the initial RegionValues
, so that the caller clones either with causes or without. I just hate boolean flags. If we are going to have a flag, I'd prefer it to be struct TrackCauses(bool)
, so that when we see a call to it, it looks like self.compute_causal_info(..., TrackCauses(true))
and hence we know what that true/false
means.
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.
Had a thought. First off, I see an argument for keeping the flag (though I prefer the struct), because -- as I wrote on gitter -- it shows that it only makes sense for compute_causal_info
to start with a fresh clone of the liveness data versus some caller that might clone the liveness data, manipulate it in some way, and then invoke compute_causal_info
. But if we are going to have the flag, then let's remove clone_tracking_causal_info
thing and pass the flag to RegionValues
:
fn clone(&self, track_causes: TrackCauses) -> RegionValues { ... }
Maybe pick another name than clone
(duplicate
?)
|
||
/// Maps between the various kinds of elements of a region value to | ||
/// the internal indices that w use. | ||
pub(super) struct RegionValueElements { | ||
pub(crate) struct RegionValueElements { |
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'd rather that this is kept private to region_infer
cf4d7fb
to
fd81cfd
Compare
self.nonlexical_cause_info = Some(regioncx.compute_causal_info(mir)); | ||
} | ||
|
||
if let Some(cause_info) = &self.nonlexical_cause_info { |
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.
can you rewrite this as:
let cause_info = self.nonlexical_cause_info.as_ref().unwrap();
after all, it had better be Some
, since we just assigned it up there!
self.inferred_values = Some(causal_info.inferred_values); | ||
} | ||
|
||
fn compute_causal_info2(&self, mir: &Mir<'tcx>, track_causes: TrackCauses) -> RegionCausalInfo { |
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 think this should return a RegionValues
; we should build the struct in compute_causal_info()
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.
Let's rename this to compute_region_values
or .. anything but something matching the regex .*2$
=)
@@ -218,6 +220,18 @@ impl RegionValues { | |||
} | |||
} | |||
|
|||
pub(super) fn duplicate(&self, track_causes: TrackCauses) -> Self { |
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 think a comment would be nice:
Duplicates the region values. If track_causes
is false, then the resulting value will not track causal information (and any existing causal information is dropped). Otherwise, the causal information is preserved and maintained. Tracking the causal information makes region propagation significantly slower, so we prefer not to do it until an error is reported.
@@ -444,21 +439,30 @@ impl<'tcx> RegionInferenceContext<'tcx> { | |||
} | |||
} | |||
|
|||
pub(crate) fn compute_causal_info(&self, mir: &Mir<'tcx>) -> RegionCausalInfo { |
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 think a comment would be nice:
Re-execute the region inference, this time tracking causal information. This is significantly slower, so it is done only when an error is being reported.
@@ -19,69 +19,75 @@ use util::liveness::{self, DefUse, LivenessMode}; | |||
|
|||
impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> { | |||
pub(in borrow_check) fn explain_why_borrow_contains_point( |
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.
Pre-existing, but needs a comment:
Adds annotations to `err` explaining *why* the borrow contains the point from `context`. This is key for the "3-point errors" [described in the NLL RFC][d].
[d]: https://rust-lang.github.io/rfcs/2094-nll.html#leveraging-intuition-framing-errors-in-terms-of-points
4fe7890
to
61c223c
Compare
☔ The latest upstream changes (presumably #48208) made this pull request unmergeable. Please resolve the merge conflicts. |
8c7cffd
to
e97d862
Compare
e97d862
to
6c2dec0
Compare
7067d1f
to
746f1b3
Compare
746f1b3
to
52a47d4
Compare
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.
lgtm!
ty::ReEarlyBound(_) | ty::ReFree(_) => { | ||
self.msg_span_from_early_bound_and_free_regions(region) | ||
}, | ||
ty::ReStatic => ("the static lifetime".to_owned(), None), |
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.
👍
@bors r+ |
📌 Commit 52a47d4 has been approved by |
…matsakis [NLL] Make causal tracking lazy Close rust-lang#46590 cc @nikomatsakis
Close #46590
cc @nikomatsakis