-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
Conversation
This comment has been minimized.
This comment has been minimized.
8a615ec
to
f72b8a4
Compare
src/test/ui/issue-40510-1.nll.stderr
Outdated
| -- | ||
| | | ||
| lifetime `'1` represents this closure's body | ||
| lifetime `'2` appears in return type |
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 it would be nice to say "in return type of this closure" -- this confused me for quite a bit
{ | ||
span | ||
} else { | ||
mir.span |
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.
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`
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 the code to extract the type name:
rust/src/librustc_mir/borrow_check/nll/region_infer/error_reporting/region_name.rs
Lines 278 to 280 in 4b8089d
let type_name = with_highlight_region(needle_fr, *counter, || { | |
infcx.extract_type_name(&argument_ty) | |
}); |
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.
To get the final character you can use the end_point
method
Comments addressed |
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 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 |
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.
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 |
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.
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<()>] |
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.
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.
@bors r+ |
📌 Commit b13e3f8 has been approved by |
… 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
…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
☀️ Test successful - status-appveyor, status-travis |
[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.
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