Skip to content

Commit 7003253

Browse files
committed
Rollup merge of rust-lang#33475 - billyevans:master, r=guillaumegomez
Add detailed error explanation for E0505 Part of rust-lang#32777
2 parents a9a130f + bf09a9e commit 7003253

File tree

1 file changed

+79
-1
lines changed

1 file changed

+79
-1
lines changed

src/librustc_borrowck/diagnostics.rs

+79-1
Original file line numberDiff line numberDiff line change
@@ -642,6 +642,85 @@ fn print_fancy_ref(fancy_ref: &FancyNum){
642642
```
643643
"##,
644644

645+
E0505: r##"
646+
A value was moved out while it was still borrowed.
647+
Erroneous code example:
648+
649+
```compile_fail
650+
struct Value {}
651+
652+
fn eat(val: Value) {}
653+
654+
fn main() {
655+
let x = Value{};
656+
{
657+
let _ref_to_val: &Value = &x;
658+
eat(x);
659+
}
660+
}
661+
```
662+
663+
Here, the function `eat` takes the ownership of `x`. However,
664+
`x` cannot be moved because it was borrowed to `_ref_to_val`.
665+
To fix that you can do few different things:
666+
667+
* Try to avoid moving the variable.
668+
* Release borrow before move.
669+
* Implement the `Copy` trait on the type.
670+
671+
Examples:
672+
673+
```
674+
struct Value {}
675+
676+
fn eat(val: &Value) {}
677+
678+
fn main() {
679+
let x = Value{};
680+
{
681+
let _ref_to_val: &Value = &x;
682+
eat(&x); // pass by reference, if it's possible
683+
}
684+
}
685+
```
686+
687+
Or:
688+
689+
```
690+
struct Value {}
691+
692+
fn eat(val: Value) {}
693+
694+
fn main() {
695+
let x = Value{};
696+
{
697+
let _ref_to_val: &Value = &x;
698+
}
699+
eat(x); // release borrow and then move it.
700+
}
701+
```
702+
703+
Or:
704+
705+
```
706+
#[derive(Clone, Copy)] // implement Copy trait
707+
struct Value {}
708+
709+
fn eat(val: Value) {}
710+
711+
fn main() {
712+
let x = Value{};
713+
{
714+
let _ref_to_val: &Value = &x;
715+
eat(x); // it will be copied here.
716+
}
717+
}
718+
```
719+
720+
You can find more information about borrowing in the rust-book:
721+
http://doc.rust-lang.org/stable/book/references-and-borrowing.html
722+
"##,
723+
645724
E0507: r##"
646725
You tried to move out of a value which was borrowed. Erroneous code example:
647726
@@ -860,7 +939,6 @@ register_diagnostics! {
860939
E0500, // closure requires unique access to `..` but .. is already borrowed
861940
E0502, // cannot borrow `..`.. as .. because .. is also borrowed as ...
862941
E0503, // cannot use `..` because it was mutably borrowed
863-
E0505, // cannot move out of `..` because it is borrowed
864942
E0508, // cannot move out of type `..`, a non-copy fixed-size array
865943
E0524, // two closures require unique access to `..` at the same time
866944
}

0 commit comments

Comments
 (0)