forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of rust-lang#99612 - yanchen4791:issue-95079-fix, r=compil…
…er-errors Fix rust-lang#95079 unhelpful error when missing move in nested closure Fix rust-lang#95079 by adding help for missing move in nested closure
- Loading branch information
Showing
7 changed files
with
104 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
src/test/ui/borrowck/issue-95079-missing-move-in-nested-closure.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
fn foo1(s: &str) -> impl Iterator<Item = String> + '_ { | ||
None.into_iter() | ||
.flat_map(move |()| s.chars().map(|c| format!("{}{}", c, s))) | ||
//~^ ERROR captured variable cannot escape `FnMut` closure body | ||
//~| HELP consider adding 'move' keyword before the nested closure | ||
} | ||
|
||
fn foo2(s: &str) -> impl Sized + '_ { | ||
move |()| s.chars().map(|c| format!("{}{}", c, s)) | ||
//~^ ERROR lifetime may not live long enough | ||
//~| HELP consider adding 'move' keyword before the nested closure | ||
} | ||
|
||
fn main() {} |
37 changes: 37 additions & 0 deletions
37
src/test/ui/borrowck/issue-95079-missing-move-in-nested-closure.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
error: captured variable cannot escape `FnMut` closure body | ||
--> $DIR/issue-95079-missing-move-in-nested-closure.rs:3:29 | ||
| | ||
LL | fn foo1(s: &str) -> impl Iterator<Item = String> + '_ { | ||
| - variable defined here | ||
LL | None.into_iter() | ||
LL | .flat_map(move |()| s.chars().map(|c| format!("{}{}", c, s))) | ||
| - -^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | | | ||
| | returns a reference to a captured variable which escapes the closure body | ||
| | variable captured here | ||
| inferred to be a `FnMut` 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 | ||
help: consider adding 'move' keyword before the nested closure | ||
| | ||
LL | .flat_map(move |()| s.chars().map(move |c| format!("{}{}", c, s))) | ||
| ++++ | ||
|
||
error: lifetime may not live long enough | ||
--> $DIR/issue-95079-missing-move-in-nested-closure.rs:9:15 | ||
| | ||
LL | move |()| s.chars().map(|c| format!("{}{}", c, s)) | ||
| --------- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ returning this value requires that `'1` must outlive `'2` | ||
| | | | ||
| | return type of closure `Map<Chars<'_>, [closure@$DIR/issue-95079-missing-move-in-nested-closure.rs:9:29: 9:32]>` contains a lifetime `'2` | ||
| lifetime `'1` represents this closure's body | ||
| | ||
= note: closure implements `Fn`, so references to captured variables can't escape the closure | ||
help: consider adding 'move' keyword before the nested closure | ||
| | ||
LL | move |()| s.chars().map(move |c| format!("{}{}", c, s)) | ||
| ++++ | ||
|
||
error: aborting due to 2 previous errors | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters