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] Use span of the closure args in free region errors #53088

Merged
merged 2 commits into from
Aug 8, 2018

Conversation

matthewjasper
Copy link
Contributor

Also adds a note when one of the free regions is BrEnv.
In a future PR I'll change these messages to say "return requires", which should improve them a bit more.

r? @nikomatsakis

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

This comment has been minimized.

| --
| |
| lifetime `'1` represents this closure's body
| lifetime `'2` appears in return type
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 it would be nice to say "in return type of this closure" -- this confused me for quite a bit

{
span
} else {
mir.span
Copy link
Contributor

Choose a reason for hiding this comment

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

We can do better here, too. mir.span is pretty horrific in all cases. Maybe we leave that for another issue though. We would want to extract (from the HIR) the span of the return type, I suppose. Really, we should do the same "return type is &'1 u32" sort of thing we do for arguments, right?

I wonder if, for closures, we can highlight just the final |?

So it would look like:

error: unsatisfied lifetime constraints
  --> $DIR/E0621-does-not-trigger-for-closures.rs:25:26
   |
LL |     invoke(&x, |a, b| if a > b { a } else { b }); //~ ERROR E0495
   |                    --    ^^^^^ requires that `'1` must outlive `'2`
   |                    ||
   |                    |return type of closure is `&'2 i32`
   |                    has type `&'1 i32`

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 the code to extract the type name:

let type_name = with_highlight_region(needle_fr, *counter, || {
infcx.extract_type_name(&argument_ty)
});

Copy link
Contributor

Choose a reason for hiding this comment

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

To get the final character you can use the end_point method

@matthewjasper
Copy link
Contributor Author

Comments addressed

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.

I have some ideas for future improvement, but they should not block this PR I think. I wonder where the best place to note them is.

| lifetime `'2` appears in return type
| -- ^^^^^ requires that `'1` must outlive `'2`
| ||
| |return type of closure is &'2 i32
Copy link
Contributor

Choose a reason for hiding this comment

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

so nice

LL | &mut x
| ^^^^^^ return requires that `'1` must outlive `'2`
|
= note: closure implements `FnMut`, so references to captured variables can't escape the closure
Copy link
Contributor

Choose a reason for hiding this comment

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

also so nice — I do wonder if we can "re-orient" the message around this note though. Something like this:

error: captured variable cannot escape a `FnMut` closure
  --> $DIR/issue-40510-1.rs:18:9
   |
LL |     || {
   |     -- closure implements `FnMut`, so references to captured variables can't escape the closure
LL |         &mut x
   |         ^^^^^^ returns a reference that is local to the closure

error: aborting due to previous error

maybe we should shelve this for a later improvement, though -- in particular, I would really like to do a bit more analysis and connect the message to the variable x. Something like this:

error: captured variable cannot escape a `FnMut` closure
  --> $DIR/issue-40510-1.rs:18:9
   |
LL |     || {
   |     -- closure implements `FnMut`, so references to captured variables can't escape the closure
LL |         &mut x
   |              ^ references to `x` cannot escape the closure body

error: aborting due to previous error

LL | || {
| --
| ||
| |return type of closure is [closure@$DIR/issue-40510-3.rs:18:9: 20:10 x:&'2 mut std::vec::Vec<()>]
Copy link
Contributor

Choose a reason for hiding this comment

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

another place we should take a note for future improvement: rather than printing the return type here, I'd rather we found some way to describe the role that '2 plays (i.e., the reference to x). But this is a start.

@nikomatsakis
Copy link
Contributor

@bors r+

@bors
Copy link
Contributor

bors commented Aug 7, 2018

📌 Commit b13e3f8 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-review Status: Awaiting review from the assignee but also interested parties. labels Aug 7, 2018
cramertj added a commit to cramertj/rust that referenced this pull request Aug 8, 2018
… r=nikomatsakis

[NLL] Use span of the closure args in free region errors

Also adds a note when one of the free regions is BrEnv.
In a future PR I'll change these messages to say "return requires", which should improve them a bit more.

r? @nikomatsakis
@bors
Copy link
Contributor

bors commented Aug 8, 2018

⌛ Testing commit b13e3f8 with merge 63fe441...

bors added a commit that referenced this pull request Aug 8, 2018
…akis

[NLL] Use span of the closure args in free region errors

Also adds a note when one of the free regions is BrEnv.
In a future PR I'll change these messages to say "return requires", which should improve them a bit more.

r? @nikomatsakis
@bors
Copy link
Contributor

bors commented Aug 8, 2018

☀️ Test successful - status-appveyor, status-travis
Approved by: nikomatsakis
Pushing 63fe441 to master...

@bors bors merged commit b13e3f8 into rust-lang:master Aug 8, 2018
bors added a commit that referenced this pull request Aug 18, 2018
[NLL] Returns are interesting for free regions

Based on #53088 - creating now to get feedback.

Closes #51175

* Make assigning to the return type interesting.
* Use "returning this value" instead of "return" in error messages.
* Prefer one of the explanations that we have a name for to a generic interesting cause in some cases.
* Treat causes that involve the destination of a call like assignments.
@matthewjasper matthewjasper deleted the closure-region-spans branch September 3, 2018 21:27
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

4 participants