Rollup merge of #64428 - GuillaumeGomez:error-explanation-E0524, r=Centril

Error explanation e0524

Part of https://github.com/rust-lang/rust/issues/61137
This commit is contained in:
Mazdak Farrokhzad 2019-09-25 03:48:24 +02:00 committed by GitHub
commit 0204f36441
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 72 additions and 11 deletions

View File

@ -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...

View File

@ -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;
}

View File

@ -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`.

View File

@ -15,3 +15,4 @@ LL | c2(); c1();
error: aborting due to previous error
For more information about this error, try `rustc --explain E0524`.

View File

@ -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`.

View File

@ -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`.