]> git.lizzy.rs Git - rust.git/commitdiff
Improve message for closure returning a closure.
authorDavid Wood <david@davidtw.co>
Thu, 4 Oct 2018 19:48:50 +0000 (21:48 +0200)
committerDavid Wood <david@davidtw.co>
Tue, 9 Oct 2018 10:31:51 +0000 (12:31 +0200)
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.

src/librustc_mir/borrow_check/nll/region_infer/error_reporting/mod.rs
src/test/ui/borrowck/borrowck-describe-lvalue.ast.nll.stderr
src/test/ui/borrowck/borrowck-describe-lvalue.mir.stderr
src/test/ui/issues/issue-40510-1.nll.stderr
src/test/ui/issues/issue-40510-3.nll.stderr
src/test/ui/issues/issue-49824.nll.stderr
src/test/ui/nll/issue-53040.stderr
src/test/ui/regions/regions-return-ref-to-upvar-issue-17403.nll.stderr

index c906ed3129dc8bb89ec9234df676c832f52c2ea6..8191dd720e7b2db1a2abfa419c2940c723d0c15b 100644 (file)
@@ -310,9 +310,19 @@ fn report_fnmut_error(
             "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 {
index c2fd674d56ce28bf2e605e8f12792022c7d7a2ab..5721c52ba217287063d75333f8032ff8ae8567e0 100644 (file)
@@ -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
index c2fd674d56ce28bf2e605e8f12792022c7d7a2ab..5721c52ba217287063d75333f8032ff8ae8567e0 100644 (file)
@@ -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
index c5a6934484e79323fd51885c7668f1e03bbe2a0e..1aeb1a89ead94f3789bf472efd709b136d98bc3f 100644 (file)
@@ -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
index f0a88fa1cdff6b4589c4b9222888183e5a9f70f9..c334e592fbc2bb0be5c0fb5ea0d77f364bbf1128 100644 (file)
@@ -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
index c95e8169176dae5d77220028b697cd307181b0e2..2e0463fdd1d860b7e2cf72952e6a681a4c43d357 100644 (file)
@@ -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
index d65d3aafbc5723349358176ab2c92ef713a21b24..fac9969f1934a3684143126f82134fefcf3b6d9a 100644 (file)
@@ -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
    |
index 8ce249f2a296e125211cd8ee8cdec742f787a46c..300a563982253df694561b4d814cb2078446799b 100644 (file)
@@ -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
    |