Skip to content

Commit 5d8a8b6

Browse files
authored
Rollup merge of rust-lang#64428 - GuillaumeGomez:error-explanation-E0524, r=Centril
Error explanation e0524 Part of rust-lang#61137
2 parents 4d54581 + d2b873b commit 5d8a8b6

6 files changed

+72
-11
lines changed

src/librustc_mir/error_codes.rs

+63-1
Original file line numberDiff line numberDiff line change
@@ -1993,6 +1993,69 @@ fn get_owned_iterator() -> IntoIter<i32> {
19931993
```
19941994
"##,
19951995

1996+
E0524: r##"
1997+
A variable which requires unique access is being used in more than one closure
1998+
at the same time.
1999+
2000+
Erroneous code example:
2001+
2002+
```compile_fail,E0524
2003+
fn set(x: &mut isize) {
2004+
*x += 4;
2005+
}
2006+
2007+
fn dragoooon(x: &mut isize) {
2008+
let mut c1 = || set(x);
2009+
let mut c2 = || set(x); // error!
2010+
2011+
c2();
2012+
c1();
2013+
}
2014+
```
2015+
2016+
To solve this issue, multiple solutions are available. First, is it required
2017+
for this variable to be used in more than one closure at a time? If it is the
2018+
case, use reference counted types such as `Rc` (or `Arc` if it runs
2019+
concurrently):
2020+
2021+
```
2022+
use std::rc::Rc;
2023+
use std::cell::RefCell;
2024+
2025+
fn set(x: &mut isize) {
2026+
*x += 4;
2027+
}
2028+
2029+
fn dragoooon(x: &mut isize) {
2030+
let x = Rc::new(RefCell::new(x));
2031+
let y = Rc::clone(&x);
2032+
let mut c1 = || { let mut x2 = x.borrow_mut(); set(&mut x2); };
2033+
let mut c2 = || { let mut x2 = y.borrow_mut(); set(&mut x2); }; // ok!
2034+
2035+
c2();
2036+
c1();
2037+
}
2038+
```
2039+
2040+
If not, just run closures one at a time:
2041+
2042+
```
2043+
fn set(x: &mut isize) {
2044+
*x += 4;
2045+
}
2046+
2047+
fn dragoooon(x: &mut isize) {
2048+
{ // This block isn't necessary since non-lexical lifetimes, it's just to
2049+
// make it more clear.
2050+
let mut c1 = || set(&mut *x);
2051+
c1();
2052+
} // `c1` has been dropped here so we're free to use `x` again!
2053+
let mut c2 = || set(&mut *x);
2054+
c2();
2055+
}
2056+
```
2057+
"##,
2058+
19962059
E0595: r##"
19972060
#### Note: this error code is no longer emitted by the compiler.
19982061
@@ -2393,7 +2456,6 @@ There are some known bugs that trigger this message.
23932456
// E0385, // {} in an aliasable location
23942457
E0493, // destructors cannot be evaluated at compile-time
23952458
E0521, // borrowed data escapes outside of closure
2396-
E0524, // two closures require unique access to `..` at the same time
23972459
E0526, // shuffle indices are not constant
23982460
E0594, // cannot assign to {}
23992461
// E0598, // lifetime of {} is too short to guarantee its contents can be...

src/test/ui/borrowck/borrowck-closures-mut-of-imm.rs

-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
// Tests that two closures cannot simultaneously have mutable
22
// and immutable access to the variable. Issue #6801.
33

4-
fn get(x: &isize) -> isize {
5-
*x
6-
}
7-
84
fn set(x: &mut isize) {
95
*x = 4;
106
}

src/test/ui/borrowck/borrowck-closures-mut-of-imm.stderr

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
error[E0596]: cannot borrow `*x` as mutable, as it is behind a `&` reference
2-
--> $DIR/borrowck-closures-mut-of-imm.rs:13:25
2+
--> $DIR/borrowck-closures-mut-of-imm.rs:9:25
33
|
44
LL | let mut c1 = || set(&mut *x);
55
| ^^^^^^^ cannot borrow as mutable
66

77
error[E0596]: cannot borrow `*x` as mutable, as it is behind a `&` reference
8-
--> $DIR/borrowck-closures-mut-of-imm.rs:15:25
8+
--> $DIR/borrowck-closures-mut-of-imm.rs:11:25
99
|
1010
LL | let mut c2 = || set(&mut *x);
1111
| ^^^^^^^ cannot borrow as mutable
1212

1313
error[E0524]: two closures require unique access to `x` at the same time
14-
--> $DIR/borrowck-closures-mut-of-imm.rs:15:18
14+
--> $DIR/borrowck-closures-mut-of-imm.rs:11:18
1515
|
1616
LL | let mut c1 = || set(&mut *x);
1717
| -- - first borrow occurs due to use of `x` in closure
@@ -28,4 +28,5 @@ LL | c2(); c1();
2828

2929
error: aborting due to 3 previous errors
3030

31-
For more information about this error, try `rustc --explain E0596`.
31+
Some errors have detailed explanations: E0524, E0596.
32+
For more information about an error, try `rustc --explain E0524`.

src/test/ui/borrowck/borrowck-closures-mut-of-mut.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,4 @@ LL | c2(); c1();
1515

1616
error: aborting due to previous error
1717

18+
For more information about this error, try `rustc --explain E0524`.

src/test/ui/borrowck/borrowck-closures-unique.stderr

+2-1
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,5 @@ LL | let c1 = |y: &'static mut isize| x = y;
5050

5151
error: aborting due to 4 previous errors
5252

53-
For more information about this error, try `rustc --explain E0500`.
53+
Some errors have detailed explanations: E0500, E0524.
54+
For more information about an error, try `rustc --explain E0500`.

src/test/ui/nll/closures-in-loops.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,5 @@ LL | v.push(|| *x = String::new());
2727

2828
error: aborting due to 3 previous errors
2929

30-
Some errors have detailed explanations: E0382, E0499.
30+
Some errors have detailed explanations: E0382, E0499, E0524.
3131
For more information about an error, try `rustc --explain E0382`.

0 commit comments

Comments
 (0)