about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorMazdak Farrokhzad <twingoow@gmail.com>2019-09-25 03:48:24 +0200
committerGitHub <noreply@github.com>2019-09-25 03:48:24 +0200
commit0204f364418dbcc3a200141ad91c06049983d337 (patch)
tree10a27b8b02879c9180a750d3ec6fd26fd036549f /src
parent40fae88fa82db350f1233b9b93df0be30e6579bc (diff)
parentd2b873b06706e97dc54317b8c0ad8f9efa1c3b70 (diff)
Rollup merge of #64428 - GuillaumeGomez:error-explanation-E0524, r=Centril
Error explanation e0524

Part of https://github.com/rust-lang/rust/issues/61137
Diffstat (limited to 'src')
-rw-r--r--src/librustc_mir/error_codes.rs64
-rw-r--r--src/test/ui/borrowck/borrowck-closures-mut-of-imm.rs4
-rw-r--r--src/test/ui/borrowck/borrowck-closures-mut-of-imm.stderr9
-rw-r--r--src/test/ui/borrowck/borrowck-closures-mut-of-mut.stderr1
-rw-r--r--src/test/ui/borrowck/borrowck-closures-unique.stderr3
-rw-r--r--src/test/ui/nll/closures-in-loops.stderr2
6 files changed, 72 insertions, 11 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...
diff --git a/src/test/ui/borrowck/borrowck-closures-mut-of-imm.rs b/src/test/ui/borrowck/borrowck-closures-mut-of-imm.rs
index 24e06e3c4e6..d7e187a2b39 100644
--- a/src/test/ui/borrowck/borrowck-closures-mut-of-imm.rs
+++ b/src/test/ui/borrowck/borrowck-closures-mut-of-imm.rs
@@ -1,10 +1,6 @@
 // Tests that two closures cannot simultaneously have mutable
 // and immutable access to the variable. Issue #6801.
 
-fn get(x: &isize) -> isize {
-    *x
-}
-
 fn set(x: &mut isize) {
     *x = 4;
 }
diff --git a/src/test/ui/borrowck/borrowck-closures-mut-of-imm.stderr b/src/test/ui/borrowck/borrowck-closures-mut-of-imm.stderr
index 3be7d725eda..784b903a589 100644
--- a/src/test/ui/borrowck/borrowck-closures-mut-of-imm.stderr
+++ b/src/test/ui/borrowck/borrowck-closures-mut-of-imm.stderr
@@ -1,17 +1,17 @@
 error[E0596]: cannot borrow `*x` as mutable, as it is behind a `&` reference
-  --> $DIR/borrowck-closures-mut-of-imm.rs:13:25
+  --> $DIR/borrowck-closures-mut-of-imm.rs:9:25
    |
 LL |     let mut c1 = || set(&mut *x);
    |                         ^^^^^^^ cannot borrow as mutable
 
 error[E0596]: cannot borrow `*x` as mutable, as it is behind a `&` reference
-  --> $DIR/borrowck-closures-mut-of-imm.rs:15:25
+  --> $DIR/borrowck-closures-mut-of-imm.rs:11:25
    |
 LL |     let mut c2 = || set(&mut *x);
    |                         ^^^^^^^ cannot borrow as mutable
 
 error[E0524]: two closures require unique access to `x` at the same time
-  --> $DIR/borrowck-closures-mut-of-imm.rs:15:18
+  --> $DIR/borrowck-closures-mut-of-imm.rs:11:18
    |
 LL |     let mut c1 = || set(&mut *x);
    |                  --           - first borrow occurs due to use of `x` in closure
@@ -28,4 +28,5 @@ LL |     c2(); c1();
 
 error: aborting due to 3 previous errors
 
-For more information about this error, try `rustc --explain E0596`.
+Some errors have detailed explanations: E0524, E0596.
+For more information about an error, try `rustc --explain E0524`.
diff --git a/src/test/ui/borrowck/borrowck-closures-mut-of-mut.stderr b/src/test/ui/borrowck/borrowck-closures-mut-of-mut.stderr
index a1743887121..471173e595f 100644
--- a/src/test/ui/borrowck/borrowck-closures-mut-of-mut.stderr
+++ b/src/test/ui/borrowck/borrowck-closures-mut-of-mut.stderr
@@ -15,3 +15,4 @@ LL |     c2(); c1();
 
 error: aborting due to previous error
 
+For more information about this error, try `rustc --explain E0524`.
diff --git a/src/test/ui/borrowck/borrowck-closures-unique.stderr b/src/test/ui/borrowck/borrowck-closures-unique.stderr
index 9b53af4c01f..2ed08b83c58 100644
--- a/src/test/ui/borrowck/borrowck-closures-unique.stderr
+++ b/src/test/ui/borrowck/borrowck-closures-unique.stderr
@@ -50,4 +50,5 @@ LL |     let c1 = |y: &'static mut isize| x = y;
 
 error: aborting due to 4 previous errors
 
-For more information about this error, try `rustc --explain E0500`.
+Some errors have detailed explanations: E0500, E0524.
+For more information about an error, try `rustc --explain E0500`.
diff --git a/src/test/ui/nll/closures-in-loops.stderr b/src/test/ui/nll/closures-in-loops.stderr
index 7603f9650b5..0b15d9bcfe6 100644
--- a/src/test/ui/nll/closures-in-loops.stderr
+++ b/src/test/ui/nll/closures-in-loops.stderr
@@ -27,5 +27,5 @@ LL |         v.push(|| *x = String::new());
 
 error: aborting due to 3 previous errors
 
-Some errors have detailed explanations: E0382, E0499.
+Some errors have detailed explanations: E0382, E0499, E0524.
 For more information about an error, try `rustc --explain E0382`.