Skip to content

Commit c0f3647

Browse files
committed
Rollup merge of rust-lang#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 rust-lang#32777.
2 parents c71db95 + 5439806 commit c0f3647

File tree

1 file changed

+100
-2
lines changed

1 file changed

+100
-2
lines changed

src/librustc_borrowck/diagnostics.rs

Lines changed: 100 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -633,6 +633,62 @@ fn main() {
633633
```
634634
"##,
635635

636+
E0503: r##"
637+
A value was used after it was mutably borrowed.
638+
639+
Example of erroneous code:
640+
641+
```compile_fail
642+
fn main() {
643+
let mut value = 3;
644+
// Create a mutable borrow of `value`. This borrow
645+
// lives until the end of this function.
646+
let _borrow = &mut value;
647+
let _sum = value + 1; // error: cannot use `value` because
648+
// it was mutably borrowed
649+
}
650+
```
651+
652+
In this example, `value` is mutably borrowed by `borrow` and cannot be
653+
used to calculate `sum`. This is not possible because this would violate
654+
Rust's mutability rules.
655+
656+
You can fix this error by limiting the scope of the borrow:
657+
658+
```
659+
fn main() {
660+
let mut value = 3;
661+
// By creating a new block, you can limit the scope
662+
// of the reference.
663+
{
664+
let _borrow = &mut value; // Use `_borrow` inside this block.
665+
}
666+
// The block has ended and with it the borrow.
667+
// You can now use `value` again.
668+
let _sum = value + 1;
669+
}
670+
```
671+
672+
Or by cloning `value` before borrowing it:
673+
674+
```
675+
fn main() {
676+
let mut value = 3;
677+
// We clone `value`, creating a copy.
678+
let value_cloned = value.cloned();
679+
// The mutable borrow is a reference to `value` and
680+
// not to `value_cloned`...
681+
let _borrow = &mut value;
682+
// ... which means we can still use `value_cloned`,
683+
let _sum = value_cloned + 1;
684+
// even though the borrow only ends here.
685+
}
686+
```
687+
688+
You can find more information about borrowing in the rust-book:
689+
http://doc.rust-lang.org/stable/book/references-and-borrowing.html
690+
"##,
691+
636692
E0506: r##"
637693
This error occurs when an attempt is made to assign to a borrowed value.
638694
@@ -911,6 +967,50 @@ You can find more information about borrowing in the rust-book:
911967
http://doc.rust-lang.org/stable/book/references-and-borrowing.html
912968
"##,
913969

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

0 commit comments

Comments
 (0)