Skip to content

Commit acfe199

Browse files
author
Timothy McRoy
committed
Add descriptive error explanation for E0502
1 parent 9f58fb7 commit acfe199

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

src/librustc_borrowck/diagnostics.rs

+27-1
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,33 @@ fn foo(a: &mut i32) {
501501
```
502502
"##,
503503

504+
E0502: r##"
505+
This error indicates that you are trying to borrow a variable as mutable when it
506+
has already been borrowed as immutable.
507+
508+
Example of erroneous code:
509+
510+
```compile_fail
511+
fn bar(x: &mut i32) {}
512+
fn foo(a: &mut i32) {
513+
let ref y = a; // a is borrowed as immutable.
514+
bar(a); // error: cannot borrow `*a` as mutable because `a` is also borrowed
515+
// as immutable
516+
}
517+
```
518+
To fix this error, ensure that you don't have any other references to the
519+
variable before trying to access it mutably:
520+
```
521+
fn bar(x: &mut i32) {}
522+
fn foo(a: &mut i32) {
523+
bar(a);
524+
let ref y = a; // ok!
525+
}
526+
```
527+
For more information on the rust ownership system, take a look at
528+
https://doc.rust-lang.org/stable/book/references-and-borrowing.html.
529+
"##,
530+
504531
E0504: r##"
505532
This error occurs when an attempt is made to move a borrowed variable into a
506533
closure.
@@ -983,7 +1010,6 @@ fn main() {
9831010
register_diagnostics! {
9841011
E0385, // {} in an aliasable location
9851012
E0388, // {} in a static location
986-
E0502, // cannot borrow `..`.. as .. because .. is also borrowed as ...
9871013
E0503, // cannot use `..` because it was mutably borrowed
9881014
E0508, // cannot move out of type `..`, a non-copy fixed-size array
9891015
E0524, // two closures require unique access to `..` at the same time

0 commit comments

Comments
 (0)