Skip to content

Commit 7a9f4c2

Browse files
Add E0500 error explanation
1 parent a4d2424 commit 7a9f4c2

File tree

1 file changed

+47
-1
lines changed

1 file changed

+47
-1
lines changed

src/librustc_borrowck/diagnostics.rs

+47-1
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,53 @@ let c = &i; // still ok!
378378
```
379379
"##,
380380

381+
E0500: r##"
382+
A borrowed variable was used in another closure. Example of erroneous code:
383+
384+
```compile_fail
385+
fn you_know_nothing(jon_snow: &mut i32) {
386+
let nights_watch = || {
387+
*jon_snow = 2;
388+
};
389+
let starks = || {
390+
*jon_snow = 3; // error: closure requires unique access to `jon_snow`
391+
// but it is already borrowed
392+
};
393+
}
394+
395+
In here, `jon_snow` is already borrowed by the `nights_watch` closure, so it
396+
cannot be borrowed by the `starks` closure at the same time. To fix this issue,
397+
you can put the closure in its own scope:
398+
399+
```
400+
fn you_know_nothing(jon_snow: &mut i32) {
401+
{
402+
let nights_watch = || {
403+
*jon_snow = 2;
404+
};
405+
} // At this point, `jon_snow` is free.
406+
let starks = || {
407+
*jon_snow = 3;
408+
};
409+
}
410+
```
411+
412+
Or, if the type implements the `Clone` trait, you can clone it between
413+
closures:
414+
415+
```
416+
fn you_know_nothing(jon_snow: &mut i32) {
417+
let mut jon_copy = jon_snow.clone();
418+
let nights_watch = || {
419+
jon_copy = 2;
420+
};
421+
let starks = || {
422+
*jon_snow = 3;
423+
};
424+
}
425+
```
426+
"##,
427+
381428
E0501: r##"
382429
This error indicates that a mutable variable is being used while it is still
383430
captured by a closure. Because the closure has borrowed the variable, it is not
@@ -753,7 +800,6 @@ fn main() {
753800
register_diagnostics! {
754801
E0385, // {} in an aliasable location
755802
E0388, // {} in a static location
756-
E0500, // closure requires unique access to `..` but .. is already borrowed
757803
E0502, // cannot borrow `..`.. as .. because .. is also borrowed as ...
758804
E0503, // cannot use `..` because it was mutably borrowed
759805
E0504, // cannot move `..` into closure because it is borrowed

0 commit comments

Comments
 (0)