1
1
error: captured variable cannot escape `FnMut` closure body
2
- --> $DIR/issue-95079-missing-move-in-nested-closure.rs:3 :29
2
+ --> $DIR/issue-95079-missing-move-in-nested-closure.rs:5 :29
3
3
|
4
4
LL | fn foo1(s: &str) -> impl Iterator<Item = String> + '_ {
5
5
| - variable defined here
6
6
LL | None.into_iter()
7
7
LL | .flat_map(move |()| s.chars().map(|c| format!("{}{}", c, s)))
8
8
| - -^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
9
9
| | |
10
- | | returns a reference to a captured variable which escapes the closure body
10
+ | | returns a closure that contains a reference to a captured variable, which then escapes the closure body
11
11
| | variable captured here
12
12
| inferred to be a `FnMut` closure
13
13
|
@@ -19,12 +19,12 @@ LL | .flat_map(move |()| s.chars().map(move |c| format!("{}{}", c, s)))
19
19
| ++++
20
20
21
21
error: lifetime may not live long enough
22
- --> $DIR/issue-95079-missing-move-in-nested-closure.rs:9 :15
22
+ --> $DIR/issue-95079-missing-move-in-nested-closure.rs:11 :15
23
23
|
24
24
LL | move |()| s.chars().map(|c| format!("{}{}", c, s))
25
25
| --------- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ returning this value requires that `'1` must outlive `'2`
26
26
| | |
27
- | | return type of closure `Map<Chars<'_>, [closure@$DIR/issue-95079-missing-move-in-nested-closure.rs:9 :29: 9 :32]>` contains a lifetime `'2`
27
+ | | return type of closure `Map<Chars<'_>, [closure@$DIR/issue-95079-missing-move-in-nested-closure.rs:11 :29: 11 :32]>` contains a lifetime `'2`
28
28
| lifetime `'1` represents this closure's body
29
29
|
30
30
= note: closure implements `Fn`, so references to captured variables can't escape the closure
@@ -33,5 +33,26 @@ help: consider adding 'move' keyword before the nested closure
33
33
LL | move |()| s.chars().map(move |c| format!("{}{}", c, s))
34
34
| ++++
35
35
36
- error: aborting due to 2 previous errors
36
+ error: captured variable cannot escape `FnMut` closure body
37
+ --> $DIR/issue-95079-missing-move-in-nested-closure.rs:21:9
38
+ |
39
+ LL | bar: &'a X,
40
+ | --- variable defined here
41
+ LL | ) -> impl Iterator<Item = ()> + 'a {
42
+ LL | Some(()).iter().flat_map(move |()| {
43
+ | - inferred to be a `FnMut` closure
44
+ LL | Some(()).iter().map(|()| { bar; })
45
+ | ^^^^^^^^^^^^^^^^^^^^^^^^^^^---^^^^
46
+ | | |
47
+ | | variable captured here
48
+ | returns a closure that contains a reference to a captured variable, which then escapes the closure body
49
+ |
50
+ = note: `FnMut` closures only have access to their captured variables while they are executing...
51
+ = note: ...therefore, they cannot allow references to captured variables to escape
52
+ help: consider adding 'move' keyword before the nested closure
53
+ |
54
+ LL | Some(()).iter().map(move |()| { bar; })
55
+ | ++++
56
+
57
+ error: aborting due to 3 previous errors
37
58
0 commit comments