]> git.lizzy.rs Git - rust.git/commitdiff
Rollup merge of #33533 - GuillaumeGomez:add_E0500, r=steveklabnik
authorManish Goregaokar <manishsmail@gmail.com>
Sun, 15 May 2016 14:43:41 +0000 (20:13 +0530)
committerManish Goregaokar <manishsmail@gmail.com>
Sun, 15 May 2016 14:43:41 +0000 (20:13 +0530)
Add E0500 error explanation

r? @Manishearth

Part of #32777.

src/librustc_borrowck/diagnostics.rs

index b5d54567191c89bfe0ac3cf143a0ff1d4960c5c4..cdbad685008f2371e02ad444919e3ee58c0b8b1a 100644 (file)
@@ -378,6 +378,53 @@ fn main() {
 ```
 "##,
 
+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
@@ -936,7 +983,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
     E0508, // cannot move out of type `..`, a non-copy fixed-size array