-
Notifications
You must be signed in to change notification settings - Fork 13.3k
[nll] better error message when returning refs to upvars #54802
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,13 @@ | ||
error: unsatisfied lifetime constraints | ||
error: captured variable cannot escape `FnMut` closure body | ||
--> $DIR/issue-40510-1.rs:18:9 | ||
| | ||
LL | || { | ||
| -- | ||
| || | ||
| |return type of closure is &'2 mut std::boxed::Box<()> | ||
| lifetime `'1` represents this closure's body | ||
| - inferred to be a `FnMut` closure | ||
LL | &mut x | ||
| ^^^^^^ returning this value requires that `'1` must outlive `'2` | ||
| ^^^^^^ returns a reference to a captured variable which escapes the closure body | ||
| | ||
= note: closure implements `FnMut`, so references to captured variables can't escape the closure | ||
= note: `FnMut` closures only have access to their captured variables while they are executing... | ||
= note: ...therefore, they cannot allow references to captured variables to escape | ||
|
||
error: aborting due to previous error | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,15 @@ | ||
error: unsatisfied lifetime constraints | ||
error: captured variable cannot escape `FnMut` closure body | ||
--> $DIR/issue-40510-3.rs:18:9 | ||
| | ||
LL | || { | ||
| -- | ||
| || | ||
| |return type of closure is [closure@$DIR/issue-40510-3.rs:18:9: 20:10 x:&'2 mut std::vec::Vec<()>] | ||
| lifetime `'1` represents this closure's body | ||
| - inferred to be a `FnMut` closure | ||
LL | / || { | ||
LL | | x.push(()) | ||
LL | | } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here I suspect this will be a bit confusing because two closures are in play. We actually can probably tell that — if the statement is an assignment with a closure rvalue, then there is an "inner" and an "outer" closure, and maybe we can adjust the message to say something like:
that wording doesn't feel 💯, though, and I don't know if it's easy to find the name Maybe something like this?
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Pushed a change with this. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note that @nikomatsakis was suggesting in particular the phrases "inner closure" and "outer closure", in that there are two closures in play and he wanted some way that the diagnostic would distinguish between the two when discussing them. I don't see any use of the word "inner" in the diff, so I don't think you quite encoded exactly what @nikomatsakis was asking for. (But I also do think that what you've done is a vast improvement over what we had before...) |
||
| |_________^ returning this value requires that `'1` must outlive `'2` | ||
| |_________^ returns a closure that contains a reference to a captured variable, which then escapes the closure body | ||
| | ||
= note: closure implements `FnMut`, so references to captured variables can't escape the closure | ||
= note: `FnMut` closures only have access to their captured variables while they are executing... | ||
= note: ...therefore, they cannot allow references to captured variables to escape | ||
|
||
error: aborting due to previous error | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,15 @@ | ||
error: unsatisfied lifetime constraints | ||
error: captured variable cannot escape `FnMut` closure body | ||
--> $DIR/issue-49824.rs:22:9 | ||
| | ||
LL | || { | ||
| -- | ||
| || | ||
| |return type of closure is [closure@$DIR/issue-49824.rs:22:9: 24:10 x:&'2 mut i32] | ||
| lifetime `'1` represents this closure's body | ||
| - inferred to be a `FnMut` closure | ||
LL | / || { | ||
LL | | let _y = &mut x; | ||
LL | | } | ||
davidtwco marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| |_________^ returning this value requires that `'1` must outlive `'2` | ||
| |_________^ returns a closure that contains a reference to a captured variable, which then escapes the closure body | ||
| | ||
= note: closure implements `FnMut`, so references to captured variables can't escape the closure | ||
= note: `FnMut` closures only have access to their captured variables while they are executing... | ||
= note: ...therefore, they cannot allow references to captured variables to escape | ||
|
||
error: aborting due to previous error | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT | ||
// file at the top-level directory of this distribution and at | ||
// http://rust-lang.org/COPYRIGHT. | ||
// | ||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | ||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | ||
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
||
#![feature(nll)] | ||
|
||
fn main() { | ||
let mut v: Vec<()> = Vec::new(); | ||
|| &mut v; | ||
} |
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.
note to self: are we literally saying
'_
when we mean it as a placeholder for a concrete lifetime like'a
? Or is it just supposed to be a replacement for the default of'static
?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.
note to self and to @davidtwco : Wait, so now I don't get a "Submit review" prompt? what the hey...?
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 comment annotates a function that was added in a previous PR - I just added it in this PR because I noticed it was missing.
IIRC from that PR, we suggest a named lifetime if we have one and
'_
otherwise.