Skip to content

Commit 983e035

Browse files
add long error explanation for E0524
1 parent c9edc02 commit 983e035

File tree

1 file changed

+63
-1
lines changed

1 file changed

+63
-1
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...

0 commit comments

Comments
 (0)