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

[NLL] Make causal tracking lazy #48682

Merged
merged 4 commits into from
Mar 9, 2018
Merged

Conversation

spastorino
Copy link
Member

@spastorino spastorino commented Mar 2, 2018

@rust-highfive
Copy link
Collaborator

r? @estebank

(rust_highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Mar 2, 2018
@spastorino
Copy link
Member Author

r? @nikomatsakis

@spastorino spastorino changed the title [NLL] [WIP] Remove nll-dump-cause flag and allow label_diagnostic to run if -Zverbose [NLL] [WIP] Make causal tracking lazy Mar 2, 2018
} else {
None
},
causes: Some(CauseMap::default()),
Copy link
Contributor

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

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?

Copy link
Member Author

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

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

Copy link
Member Author

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

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@bors
Copy link
Contributor

bors commented Mar 4, 2018

☔ The latest upstream changes (presumably #48592) made this pull request unmergeable. Please resolve the merge conflicts.

@bors bors 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 4, 2018
@spastorino spastorino changed the title [NLL] [WIP] Make causal tracking lazy [NLL] Make causal tracking lazy Mar 5, 2018
R: ToRegionVid,
{
let inferred_values = &self.nonlexical_cause_info.as_ref()
.expect("region values not yet inferred")
Copy link
Contributor

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

Copy link
Contributor

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

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

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

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.

Copy link
Contributor

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

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

@spastorino spastorino force-pushed the make_causal_lazy branch 4 times, most recently from cf4d7fb to fd81cfd Compare March 5, 2018 19:21
self.nonlexical_cause_info = Some(regioncx.compute_causal_info(mir));
}

if let Some(cause_info) = &self.nonlexical_cause_info {
Copy link
Contributor

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

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

Copy link
Contributor

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

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

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

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

@bors
Copy link
Contributor

bors commented Mar 5, 2018

☔ The latest upstream changes (presumably #48208) made this pull request unmergeable. Please resolve the merge conflicts.

@spastorino spastorino force-pushed the make_causal_lazy branch 6 times, most recently from 8c7cffd to e97d862 Compare March 6, 2018 06:03
Copy link
Contributor

@nikomatsakis nikomatsakis left a 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),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@nikomatsakis
Copy link
Contributor

@bors r+

@bors
Copy link
Contributor

bors commented Mar 7, 2018

📌 Commit 52a47d4 has been approved by nikomatsakis

@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 7, 2018
Manishearth added a commit to Manishearth/rust that referenced this pull request Mar 8, 2018
bors added a commit that referenced this pull request Mar 8, 2018
Rollup of 7 pull requests

- Successful merges: #48292, #48682, #48699, #48738, #48752, #48789, #48808
- Failed merges:
@bors bors merged commit 52a47d4 into rust-lang:master Mar 9, 2018
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.

5 participants