"captured variable cannot escape `FnMut` closure body",
);
+ // We should check if the return type of this closure is in fact a closure - in that
+ // case, we can special case the error further.
+ let return_type_is_closure = self.universal_regions.unnormalized_output_ty.is_closure();
+ let message = if return_type_is_closure {
+ "returns a closure that contains a reference to a captured variable, which then \
+ escapes the closure body"
+ } else {
+ "returns a reference to a captured variable which escapes the closure body"
+ };
+
diag.span_label(
span,
- "creates a reference to a captured variable which escapes the closure body",
+ message,
);
match self.give_region_a_name(infcx, mir, mir_def_id, outlived_fr, &mut 1).source {
LL | | *y = 1;
LL | | drop(y);
LL | | }
- | |_________________^ creates a reference to a captured variable which escapes the closure body
+ | |_________________^ returns a closure that contains a reference to a captured variable, which then escapes the closure body
|
= 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
LL | | *y = 1;
LL | | drop(y);
LL | | }
- | |_________________^ creates a reference to a captured variable which escapes the closure body
+ | |_________________^ returns a closure that contains a reference to a captured variable, which then escapes the closure body
|
= 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
LL | || {
| - inferred to be a `FnMut` closure
LL | &mut x
- | ^^^^^^ creates a reference to a captured variable which escapes the closure body
+ | ^^^^^^ returns a reference to a captured variable which escapes the closure body
|
= 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
LL | / || {
LL | | x.push(())
LL | | }
- | |_________^ creates a reference to a captured variable which escapes the closure body
+ | |_________^ returns a closure that contains a reference to a captured variable, which then escapes the closure body
|
= 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
LL | / || {
LL | | let _y = &mut x;
LL | | }
- | |_________^ creates a reference to a captured variable which escapes the closure body
+ | |_________^ returns a closure that contains a reference to a captured variable, which then escapes the closure body
|
= 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
--> $DIR/issue-53040.rs:15:8
|
LL | || &mut v;
- | - ^^^^^^ creates a reference to a captured variable which escapes the closure body
+ | - ^^^^^^ returns a reference to a captured variable which escapes the closure body
| |
| inferred to be a `FnMut` closure
|
--> $DIR/regions-return-ref-to-upvar-issue-17403.rs:17:24
|
LL | let mut f = || &mut x; //~ ERROR cannot infer
- | - ^^^^^^ creates a reference to a captured variable which escapes the closure body
+ | - ^^^^^^ returns a reference to a captured variable which escapes the closure body
| |
| inferred to be a `FnMut` closure
|