about summary refs log tree commit diff
diff options
context:
space:
mode:
authorggomez <guillaume1.gomez@gmail.com>2016-05-10 10:54:29 +0200
committerGuillaume Gomez <guillaume1.gomez@gmail.com>2016-05-11 08:03:34 +0200
commit7a9f4c22ff974ec68300f68c46df49d3f83329de (patch)
tree1de0e1fe29451db4f6a34b53f5e22c21fb4f7727
parenta4d2424cc304e97f553c6d8eef17a24dc2f12c01 (diff)
downloadrust-7a9f4c22ff974ec68300f68c46df49d3f83329de.tar.gz
rust-7a9f4c22ff974ec68300f68c46df49d3f83329de.zip
Add E0500 error explanation
-rw-r--r--src/librustc_borrowck/diagnostics.rs48
1 files changed, 47 insertions, 1 deletions
diff --git a/src/librustc_borrowck/diagnostics.rs b/src/librustc_borrowck/diagnostics.rs
index 8d9b88e899b..2c38bc36dc3 100644
--- a/src/librustc_borrowck/diagnostics.rs
+++ b/src/librustc_borrowck/diagnostics.rs
@@ -378,6 +378,53 @@ let c = &i; // still ok!
 ```
 "##,
 
+E0500: r##"
+A borrowed variable was used in another closure. Example of erroneous code:
+
+```compile_fail
+fn you_know_nothing(jon_snow: &mut i32) {
+    let nights_watch = || {
+        *jon_snow = 2;
+    };
+    let starks = || {
+        *jon_snow = 3; // error: closure requires unique access to `jon_snow`
+                       //        but it is already borrowed
+    };
+}
+
+In here, `jon_snow` is already borrowed by the `nights_watch` closure, so it
+cannot be borrowed by the `starks` closure at the same time. To fix this issue,
+you can put the closure in its own scope:
+
+```
+fn you_know_nothing(jon_snow: &mut i32) {
+    {
+        let nights_watch = || {
+            *jon_snow = 2;
+        };
+    } // At this point, `jon_snow` is free.
+    let starks = || {
+        *jon_snow = 3;
+    };
+}
+```
+
+Or, if the type implements the `Clone` trait, you can clone it between
+closures:
+
+```
+fn you_know_nothing(jon_snow: &mut i32) {
+    let mut jon_copy = jon_snow.clone();
+    let nights_watch = || {
+        jon_copy = 2;
+    };
+    let starks = || {
+        *jon_snow = 3;
+    };
+}
+```
+"##,
+
 E0501: r##"
 This error indicates that a mutable variable is being used while it is still
 captured by a closure. Because the closure has borrowed the variable, it is not
@@ -753,7 +800,6 @@ fn main() {
 register_diagnostics! {
     E0385, // {} in an aliasable location
     E0388, // {} in a static location
-    E0500, // closure requires unique access to `..` but .. is already borrowed
     E0502, // cannot borrow `..`.. as .. because .. is also borrowed as ...
     E0503, // cannot use `..` because it was mutably borrowed
     E0504, // cannot move `..` into closure because it is borrowed