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] borrows that must be valid for a free lifetime should explain why #54229

Merged
merged 12 commits into from
Sep 23, 2018

Conversation

davidtwco
Copy link
Member

Fixes #52534.

r? @nikomatsakis

@memoryruins memoryruins added the A-NLL Area: Non-lexical lifetimes (NLL) label Sep 14, 2018
@rust-highfive

This comment has been minimized.

@rust-highfive

This comment has been minimized.

@nikomatsakis
Copy link
Contributor

r? @nikomatsakis

@bors

This comment has been minimized.

@bors bors added the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label Sep 14, 2018
@davidtwco

This comment has been minimized.

@bors

This comment has been minimized.

@davidtwco

This comment has been minimized.

@davidtwco davidtwco added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Sep 18, 2018
@bors

This comment has been minimized.

@davidtwco
Copy link
Member Author

Rebased.

@davidtwco davidtwco changed the title WIP: [nll] borrows that must be valid for a free lifetime should explain why [nll] borrows that must be valid for a free lifetime should explain why Sep 18, 2018
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.

Seems good overall

src/librustc/util/ppaux.rs Outdated Show resolved Hide resolved
@@ -1,17 +1,19 @@
error[E0597]: `x` does not live long enough
--> $DIR/issue-30438-c.rs:19:5
|
LL | fn silly<'y, 'z>(_s: &'y Test<'z>) -> &'y <Test<'z> as Trait>::Out where 'z: 'static {
| ------------ ---------------------------- has type `&'y <Test<'z> as Trait>::Out`
Copy link
Contributor

Choose a reason for hiding this comment

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

question: do you think it's good to talk about 'z here? I'm torn.

(Also, man, it'd be nice if we could just underline 'y 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.

I've changed this so that it only underlines the 'y. Looking into the types to work out what the 'z is used for seems like it would be challenging.

src/librustc_mir/borrow_check/error_reporting.rs Outdated Show resolved Hide resolved
src/librustc_mir/borrow_check/error_reporting.rs Outdated Show resolved Hide resolved
//
// If there is more than one argument and this error is being reported, that
// means there must be a self parameter - as otherwise there would be an error
// from lifetime elision and not this. So we highlight the self parameter.
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't follow this logic 100% — this is true if the region in question is anonymous, but that's not known here, right?

Copy link
Contributor

Choose a reason for hiding this comment

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

And this only looks at the first lifetime?

Copy link
Member Author

Choose a reason for hiding this comment

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

I've updated the logic to handle this case correctly.

LL | fn foo<'a>(x: &'a (u32,)) -> &'a u32 {
| ---------- ------- has type `&'a u32`
| |
| has type `&'a (u32,)`
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm a bit torn about highlighting x here. I mean, it doesn't turn out to matter what type x has, right? Though maybe that is hard for us to see right now.

Copy link
Member Author

Choose a reason for hiding this comment

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

The intention with highlighting the argument is to show that through the elision rules, it is expected to be the same lifetime as the return type. This isn't useful in this case where the argument and return type have the lifetimes specified.

Copy link
Member Author

Choose a reason for hiding this comment

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

I've added a note that should clarify this intent for the cases where elision rules are relevant and a help for the named lifetime cases.

src/librustc_mir/borrow_check/error_reporting.rs Outdated Show resolved Hide resolved
pub fn has_name(&self) -> bool {
match *self {
RegionKind::ReEarlyBound(ebr) => ebr.has_name(),
RegionKind::ReLateBound(_, br) => br.is_named(),
Copy link
Contributor

Choose a reason for hiding this comment

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

Aside: The difference above is... unfortunate.

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm not sure I understand what you mean?

Copy link
Contributor

Choose a reason for hiding this comment

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

has_name vs is_named

src/test/ui/nll/issue-52534.stderr Outdated Show resolved Hide resolved
src/test/ui/nll/issue-52534.stderr Outdated Show resolved Hide resolved
Copy link
Contributor

@estebank estebank left a comment

Choose a reason for hiding this comment

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

found 'em

src/librustc_mir/borrow_check/error_reporting.rs Outdated Show resolved Hide resolved
src/librustc_mir/borrow_check/error_reporting.rs Outdated Show resolved Hide resolved
@bors

This comment has been minimized.

@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 Sep 23, 2018
Previously, explain_borrow would emit an error with the explanation of
the a borrow. Now, it returns a enum with what the explanation for the
borrow is and any relevant spans or information such that the calling
code can choose to emit the same note/suggestion as before by calling
the emit method on the new enum.
Previously, region naming would always highlight the source of the
region name it found. Now, region naming returns the name as part
of a larger structure that encodes the source of the region naming
such that a region name can be optionally added to the diagnostic.
For cases where there are references in the parameters and in the the
outputs that do not match, and where no closures are involved, this
commit introduces an improved error that mentions (or synthesizes)
a name for the regions involved to better illustrate why the borrow
does not live long enough.
Adds improved messages for closures where returned type
does not match the inferred return lifetime of the closure.
Start mentioning function name that the variable is valid within in
notes to provide context.
New test has multiple parameters in a closure with longer names in order
to clarify the issues relating to odd spans.
Fixes the off-by-one span issue where closure argument spans were
pointing to the token after the argument.
This error can only occur within a function when a borrow of data owned
within the function is returned; and when there are arguments that could
have been returned instead. Therefore, it is always applicable to add a
specific note that links to the relevant rust documentation about
dangling references.
Changed `highlight_region_with_region` function(s) to
`highlight_region_with_bound_region` to be more specific and less
ambigious.
Enhances annotation logic to properly consider named lifetimes where
lifetime elision rules that were previously implemented would not apply.

Further, adds new help and note messages to diagnostics and highlights
only lifetime when dealing with named lifetimes.
Error now correctly checks whether the borrow that does not live
long enough is being returned before annotating the error with the
arguments and return type from the signature - as this would not be
relevant if the borrow was not being returned.
@davidtwco
Copy link
Member Author

@bors r=pnkfelix

@bors
Copy link
Contributor

bors commented Sep 23, 2018

📌 Commit b342f00 has been approved by pnkfelix

@bors
Copy link
Contributor

bors commented Sep 23, 2018

⌛ Testing commit b342f00 with merge f49f6e7...

bors added a commit that referenced this pull request Sep 23, 2018
[nll] borrows that must be valid for a free lifetime should explain why

Fixes #52534.

r? @nikomatsakis
@bors
Copy link
Contributor

bors commented Sep 23, 2018

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


match reason {
Liveness { local, location, in_loop } => {
match find_use::find(mir, regioncx, tcx, region_sub, context.loc) {
Copy link
Member

@pnkfelix pnkfelix Sep 28, 2018

Choose a reason for hiding this comment

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

pnkfelix's brain, five days after code lands: "Hey, this isn't indented right... hmm and maybe I might have chosen a name besides emit for that method over there... who reviewed this, anyway !?!?! Oh. Oh. It was me. I reviewed it..." 😆

@davidtwco davidtwco deleted the issue-52534 branch September 28, 2018 09:53
topecongiro added a commit to topecongiro/rustfmt that referenced this pull request Oct 6, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-NLL Area: Non-lexical lifetimes (NLL) 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.

8 participants