about summary refs log tree commit diff
diff options
context:
space:
mode:
authorGuillaume Gomez <guillaume1.gomez@gmail.com>2019-09-13 15:14:28 +0200
committerGuillaume Gomez <guillaume1.gomez@gmail.com>2019-09-21 17:43:56 +0200
commit983e035b11d90787fe27c5c31203b4e03ea0266a (patch)
treed0f3b3c5664edb112168e07eefcd3478445e711c
parentc9edc02e8320a9e5799d185910ece7d491f524e6 (diff)
downloadrust-983e035b11d90787fe27c5c31203b4e03ea0266a.tar.gz
rust-983e035b11d90787fe27c5c31203b4e03ea0266a.zip
add long error explanation for E0524
-rw-r--r--src/librustc_mir/error_codes.rs64
1 files changed, 63 insertions, 1 deletions
diff --git a/src/librustc_mir/error_codes.rs b/src/librustc_mir/error_codes.rs
index ba299e9463b..196bcf147f8 100644
--- a/src/librustc_mir/error_codes.rs
+++ b/src/librustc_mir/error_codes.rs
@@ -1993,6 +1993,69 @@ fn get_owned_iterator() -> IntoIter<i32> {
 ```
 "##,
 
+E0524: r##"
+A variable which requires unique access is being used in more than one closure
+at the same time.
+
+Erroneous code example:
+
+```compile_fail,E0524
+fn set(x: &mut isize) {
+    *x += 4;
+}
+
+fn dragoooon(x: &mut isize) {
+    let mut c1 = || set(x);
+    let mut c2 = || set(x); // error!
+
+    c2();
+    c1();
+}
+```
+
+To solve this issue, multiple solutions are available. First, is it required
+for this variable to be used in more than one closure at a time? If it is the
+case, use reference counted types such as `Rc` (or `Arc` if it runs
+concurrently):
+
+```
+use std::rc::Rc;
+use std::cell::RefCell;
+
+fn set(x: &mut isize) {
+    *x += 4;
+}
+
+fn dragoooon(x: &mut isize) {
+    let x = Rc::new(RefCell::new(x));
+    let y = Rc::clone(&x);
+    let mut c1 = || { let mut x2 = x.borrow_mut(); set(&mut x2); };
+    let mut c2 = || { let mut x2 = y.borrow_mut(); set(&mut x2); }; // ok!
+
+    c2();
+    c1();
+}
+```
+
+If not, just run closures one at a time:
+
+```
+fn set(x: &mut isize) {
+    *x += 4;
+}
+
+fn dragoooon(x: &mut isize) {
+    { // This block isn't necessary since non-lexical lifetimes, it's just to
+      // make it more clear.
+        let mut c1 = || set(&mut *x);
+        c1();
+    } // `c1` has been dropped here so we're free to use `x` again!
+    let mut c2 = || set(&mut *x);
+    c2();
+}
+```
+"##,
+
 E0595: r##"
 #### Note: this error code is no longer emitted by the compiler.
 
@@ -2393,7 +2456,6 @@ There are some known bugs that trigger this message.
 //  E0385, // {} in an aliasable location
     E0493, // destructors cannot be evaluated at compile-time
     E0521, // borrowed data escapes outside of closure
-    E0524, // two closures require unique access to `..` at the same time
     E0526, // shuffle indices are not constant
     E0594, // cannot assign to {}
 //  E0598, // lifetime of {} is too short to guarantee its contents can be...