Skip to content

Commit 98633b4

Browse files
committed
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.
1 parent c65e119 commit 98633b4

File tree

8 files changed

+18
-8
lines changed

8 files changed

+18
-8
lines changed

src/librustc_mir/borrow_check/nll/region_infer/error_reporting/mod.rs

+11-1
Original file line numberDiff line numberDiff line change
@@ -310,9 +310,19 @@ impl<'tcx> RegionInferenceContext<'tcx> {
310310
"captured variable cannot escape `FnMut` closure body",
311311
);
312312

313+
// We should check if the return type of this closure is in fact a closure - in that
314+
// case, we can special case the error further.
315+
let return_type_is_closure = self.universal_regions.unnormalized_output_ty.is_closure();
316+
let message = if return_type_is_closure {
317+
"returns a closure that contains a reference to a captured variable, which then \
318+
escapes the closure body"
319+
} else {
320+
"returns a reference to a captured variable which escapes the closure body"
321+
};
322+
313323
diag.span_label(
314324
span,
315-
"creates a reference to a captured variable which escapes the closure body",
325+
message,
316326
);
317327

318328
match self.give_region_a_name(infcx, mir, mir_def_id, outlived_fr, &mut 1).source {

src/test/ui/borrowck/borrowck-describe-lvalue.ast.nll.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ LL | | //[mir]~^ ERROR cannot borrow `x` as mutable more than
3232
LL | | *y = 1;
3333
LL | | drop(y);
3434
LL | | }
35-
| |_________________^ creates a reference to a captured variable which escapes the closure body
35+
| |_________________^ returns a closure that contains a reference to a captured variable, which then escapes the closure body
3636
|
3737
= note: `FnMut` closures only have access to their captured variables while they are executing...
3838
= note: ...therefore, they cannot allow references to captured variables to escape

src/test/ui/borrowck/borrowck-describe-lvalue.mir.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ LL | | //[mir]~^ ERROR cannot borrow `x` as mutable more than
3232
LL | | *y = 1;
3333
LL | | drop(y);
3434
LL | | }
35-
| |_________________^ creates a reference to a captured variable which escapes the closure body
35+
| |_________________^ returns a closure that contains a reference to a captured variable, which then escapes the closure body
3636
|
3737
= note: `FnMut` closures only have access to their captured variables while they are executing...
3838
= note: ...therefore, they cannot allow references to captured variables to escape

src/test/ui/issues/issue-40510-1.nll.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error: captured variable cannot escape `FnMut` closure body
44
LL | || {
55
| - inferred to be a `FnMut` closure
66
LL | &mut x
7-
| ^^^^^^ creates a reference to a captured variable which escapes the closure body
7+
| ^^^^^^ returns a reference to a captured variable which escapes the closure body
88
|
99
= note: `FnMut` closures only have access to their captured variables while they are executing...
1010
= note: ...therefore, they cannot allow references to captured variables to escape

src/test/ui/issues/issue-40510-3.nll.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ LL | || {
66
LL | / || {
77
LL | | x.push(())
88
LL | | }
9-
| |_________^ creates a reference to a captured variable which escapes the closure body
9+
| |_________^ returns a closure that contains a reference to a captured variable, which then escapes the closure body
1010
|
1111
= note: `FnMut` closures only have access to their captured variables while they are executing...
1212
= note: ...therefore, they cannot allow references to captured variables to escape

src/test/ui/issues/issue-49824.nll.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ LL | || {
66
LL | / || {
77
LL | | let _y = &mut x;
88
LL | | }
9-
| |_________^ creates a reference to a captured variable which escapes the closure body
9+
| |_________^ returns a closure that contains a reference to a captured variable, which then escapes the closure body
1010
|
1111
= note: `FnMut` closures only have access to their captured variables while they are executing...
1212
= note: ...therefore, they cannot allow references to captured variables to escape

src/test/ui/nll/issue-53040.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error: captured variable cannot escape `FnMut` closure body
22
--> $DIR/issue-53040.rs:15:8
33
|
44
LL | || &mut v;
5-
| - ^^^^^^ creates a reference to a captured variable which escapes the closure body
5+
| - ^^^^^^ returns a reference to a captured variable which escapes the closure body
66
| |
77
| inferred to be a `FnMut` closure
88
|

src/test/ui/regions/regions-return-ref-to-upvar-issue-17403.nll.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error: captured variable cannot escape `FnMut` closure body
22
--> $DIR/regions-return-ref-to-upvar-issue-17403.rs:17:24
33
|
44
LL | let mut f = || &mut x; //~ ERROR cannot infer
5-
| - ^^^^^^ creates a reference to a captured variable which escapes the closure body
5+
| - ^^^^^^ returns a reference to a captured variable which escapes the closure body
66
| |
77
| inferred to be a `FnMut` closure
88
|

0 commit comments

Comments
 (0)