Skip to content

Commit 62ee403

Browse files
committed
Rollup merge of rust-lang#33353 - timothy-mcroy:E0502, r=steveklabnik
Add error explanation for E0502 I am questioning the order of presentation on the suggested code fixes, but I'm not sure what would be best. Thoughts? r? @GuillaumeGomez
2 parents 786b26d + bbb7844 commit 62ee403

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)