Rollup merge of #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
This commit is contained in:
commit
c9ca735a7b
1 changed files with 27 additions and 1 deletions
|
@ -502,6 +502,33 @@ fn foo(a: &mut i32) {
|
||||||
```
|
```
|
||||||
"##,
|
"##,
|
||||||
|
|
||||||
|
E0502: r##"
|
||||||
|
This error indicates that you are trying to borrow a variable as mutable when it
|
||||||
|
has already been borrowed as immutable.
|
||||||
|
|
||||||
|
Example of erroneous code:
|
||||||
|
|
||||||
|
```compile_fail
|
||||||
|
fn bar(x: &mut i32) {}
|
||||||
|
fn foo(a: &mut i32) {
|
||||||
|
let ref y = a; // a is borrowed as immutable.
|
||||||
|
bar(a); // error: cannot borrow `*a` as mutable because `a` is also borrowed
|
||||||
|
// as immutable
|
||||||
|
}
|
||||||
|
```
|
||||||
|
To fix this error, ensure that you don't have any other references to the
|
||||||
|
variable before trying to access it mutably:
|
||||||
|
```
|
||||||
|
fn bar(x: &mut i32) {}
|
||||||
|
fn foo(a: &mut i32) {
|
||||||
|
bar(a);
|
||||||
|
let ref y = a; // ok!
|
||||||
|
}
|
||||||
|
```
|
||||||
|
For more information on the rust ownership system, take a look at
|
||||||
|
https://doc.rust-lang.org/stable/book/references-and-borrowing.html.
|
||||||
|
"##,
|
||||||
|
|
||||||
E0504: r##"
|
E0504: r##"
|
||||||
This error occurs when an attempt is made to move a borrowed variable into a
|
This error occurs when an attempt is made to move a borrowed variable into a
|
||||||
closure.
|
closure.
|
||||||
|
@ -984,7 +1011,6 @@ fn main() {
|
||||||
register_diagnostics! {
|
register_diagnostics! {
|
||||||
E0385, // {} in an aliasable location
|
E0385, // {} in an aliasable location
|
||||||
E0388, // {} in a static location
|
E0388, // {} in a static location
|
||||||
E0502, // cannot borrow `..`.. as .. because .. is also borrowed as ...
|
|
||||||
E0503, // cannot use `..` because it was mutably borrowed
|
E0503, // cannot use `..` because it was mutably borrowed
|
||||||
E0508, // cannot move out of type `..`, a non-copy fixed-size array
|
E0508, // cannot move out of type `..`, a non-copy fixed-size array
|
||||||
E0524, // two closures require unique access to `..` at the same time
|
E0524, // two closures require unique access to `..` at the same time
|
||||||
|
|
Loading…
Add table
Reference in a new issue