Skip to content

Commit c9ca735

Browse files
committed
Rollup merge of rust-lang#33353 - timothy-mcroy:E0502, r=sanxiyn
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 d0ca0ca + acfe199 commit c9ca735

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
@@ -502,6 +502,33 @@ fn foo(a: &mut i32) {
502502
```
503503
"##,
504504

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

0 commit comments

Comments
 (0)