Skip to content

Commit 51d2d3d

Browse files
authored
Auto merge of #34133 - m-decoster:master, r=GuillaumeGomez
Add explanations for E0503 and E0508. (cannot use `..` because it was mutably borrowed, cannot move out of type `..`, a non-copy fixed-size array) Part of #32777.
2 parents abc57ab + e06fbe1 commit 51d2d3d

File tree

1 file changed

+100
-2
lines changed

1 file changed

+100
-2
lines changed

src/librustc_borrowck/diagnostics.rs

+100-2
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,62 @@ For more information on the rust ownership system, take a look at
532532
https://doc.rust-lang.org/stable/book/references-and-borrowing.html.
533533
"##,
534534

535+
E0503: r##"
536+
A value was used after it was mutably borrowed.
537+
538+
Example of erroneous code:
539+
540+
```compile_fail
541+
fn main() {
542+
let mut value = 3;
543+
// Create a mutable borrow of `value`. This borrow
544+
// lives until the end of this function.
545+
let _borrow = &mut value;
546+
let _sum = value + 1; // error: cannot use `value` because
547+
// it was mutably borrowed
548+
}
549+
```
550+
551+
In this example, `value` is mutably borrowed by `borrow` and cannot be
552+
used to calculate `sum`. This is not possible because this would violate
553+
Rust's mutability rules.
554+
555+
You can fix this error by limiting the scope of the borrow:
556+
557+
```
558+
fn main() {
559+
let mut value = 3;
560+
// By creating a new block, you can limit the scope
561+
// of the reference.
562+
{
563+
let _borrow = &mut value; // Use `_borrow` inside this block.
564+
}
565+
// The block has ended and with it the borrow.
566+
// You can now use `value` again.
567+
let _sum = value + 1;
568+
}
569+
```
570+
571+
Or by cloning `value` before borrowing it:
572+
573+
```
574+
fn main() {
575+
let mut value = 3;
576+
// We clone `value`, creating a copy.
577+
let value_cloned = value.clone();
578+
// The mutable borrow is a reference to `value` and
579+
// not to `value_cloned`...
580+
let _borrow = &mut value;
581+
// ... which means we can still use `value_cloned`,
582+
let _sum = value_cloned + 1;
583+
// even though the borrow only ends here.
584+
}
585+
```
586+
587+
You can find more information about borrowing in the rust-book:
588+
http://doc.rust-lang.org/stable/book/references-and-borrowing.html
589+
"##,
590+
535591
E0504: r##"
536592
This error occurs when an attempt is made to move a borrowed variable into a
537593
closure.
@@ -914,6 +970,50 @@ You can find more information about borrowing in the rust-book:
914970
http://doc.rust-lang.org/stable/book/references-and-borrowing.html
915971
"##,
916972

973+
E0508: r##"
974+
A value was moved out of a non-copy fixed-size array.
975+
976+
Example of erroneous code:
977+
978+
```compile_fail
979+
struct NonCopy;
980+
981+
fn main() {
982+
let array = [NonCopy; 1];
983+
let _value = array[0]; // error: cannot move out of type `[NonCopy; 1]`,
984+
// a non-copy fixed-size array
985+
}
986+
```
987+
988+
The first element was moved out of the array, but this is not
989+
possible because `NonCopy` does not implement the `Copy` trait.
990+
991+
Consider borrowing the element instead of moving it:
992+
993+
```
994+
struct NonCopy;
995+
996+
fn main() {
997+
let array = [NonCopy; 1];
998+
let _value = &array[0]; // Borrowing is allowed, unlike moving.
999+
}
1000+
```
1001+
1002+
Alternatively, if your type implements `Clone` and you need to own the value,
1003+
consider borrowing and then cloning:
1004+
1005+
```
1006+
#[derive(Clone)]
1007+
struct NonCopy;
1008+
1009+
fn main() {
1010+
let array = [NonCopy; 1];
1011+
// Now you can clone the array element.
1012+
let _value = array[0].clone();
1013+
}
1014+
```
1015+
"##,
1016+
9171017
E0509: r##"
9181018
This error occurs when an attempt is made to move out of a value whose type
9191019
implements the `Drop` trait.
@@ -1014,7 +1114,5 @@ fn main() {
10141114
register_diagnostics! {
10151115
E0385, // {} in an aliasable location
10161116
E0388, // {} in a static location
1017-
E0503, // cannot use `..` because it was mutably borrowed
1018-
E0508, // cannot move out of type `..`, a non-copy fixed-size array
10191117
E0524, // two closures require unique access to `..` at the same time
10201118
}

0 commit comments

Comments
 (0)