Skip to content

Commit bbb7844

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

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

src/librustc_borrowck/diagnostics.rs

Lines changed: 27 additions & 0 deletions
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.

0 commit comments

Comments
 (0)