Improve message for closure returning a closure.
Now when a `FnMut` closure is returning a closure that contains a reference to a captured variable, we provide an error that makes it more clear what is happening.
This commit is contained in:
parent
c65e119229
commit
98633b458b
8 changed files with 18 additions and 8 deletions
|
@ -310,9 +310,19 @@ impl<'tcx> RegionInferenceContext<'tcx> {
|
|||
"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 {
|
||||
|
|
|
@ -32,7 +32,7 @@ LL | | //[mir]~^ ERROR cannot borrow `x` as mutable more than
|
|||
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
|
||||
|
|
|
@ -32,7 +32,7 @@ LL | | //[mir]~^ ERROR cannot borrow `x` as mutable more than
|
|||
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
|
||||
|
|
|
@ -4,7 +4,7 @@ error: captured variable cannot escape `FnMut` closure body
|
|||
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
|
||||
|
|
|
@ -6,7 +6,7 @@ LL | || {
|
|||
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
|
||||
|
|
|
@ -6,7 +6,7 @@ LL | || {
|
|||
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
|
||||
|
|
|
@ -2,7 +2,7 @@ error: captured variable cannot escape `FnMut` closure body
|
|||
--> $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
|
||||
|
|
||||
|
|
|
@ -2,7 +2,7 @@ error: captured variable cannot escape `FnMut` closure body
|
|||
--> $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
|
||||
|
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue