Skip to content

Commit 55410a4

Browse files
author
Timothy McRoy
committed
Add descriptive error explanation for E0502
1 parent e37f859 commit 55410a4

File tree

1 file changed

+32
-1
lines changed

1 file changed

+32
-1
lines changed

Diff for: src/librustc_borrowck/diagnostics.rs

+32-1
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,38 @@ fn foo(a: &mut i32) {
454454
```
455455
"##,
456456

457+
458+
E0502: r##"
459+
This error indicates that you are trying to borrow a variable as mutable when it
460+
has already been borrowed as immutable.
461+
462+
Example of erroneous code:
463+
464+
```compile_fail
465+
fn bar(x: &mut i32) {}
466+
fn foo(a: &mut i32) {
467+
let ref y = a; // a is borrowed as immutable.
468+
bar(a); // error: cannot borrow `*a` as mutable because `a` is also borrowed
469+
// as immutable
470+
}
471+
```
472+
473+
To fix this error, ensure that you don't have any other references to the
474+
variable before trying to access it mutably:
475+
476+
```
477+
fn bar(x: &mut i32) {}
478+
fn foo(a: &mut i32) {
479+
bar(a);
480+
let ref y = a; // ok!
481+
}
482+
```
483+
484+
For more information on the rust ownership system, take a look at
485+
https://doc.rust-lang.org/stable/book/references-and-borrowing.html.
486+
"##,
487+
488+
457489
E0504: r##"
458490
This error occurs when an attempt is made to move a borrowed variable into a
459491
closure.
@@ -858,7 +890,6 @@ register_diagnostics! {
858890
E0385, // {} in an aliasable location
859891
E0388, // {} in a static location
860892
E0500, // closure requires unique access to `..` but .. is already borrowed
861-
E0502, // cannot borrow `..`.. as .. because .. is also borrowed as ...
862893
E0503, // cannot use `..` because it was mutably borrowed
863894
E0505, // cannot move out of `..` because it is borrowed
864895
E0508, // cannot move out of type `..`, a non-copy fixed-size array

0 commit comments

Comments
 (0)