Skip to content

Commit

Permalink
Fixes rust-lang#58586: Make E0505 explain example fail for 2018 edition
Browse files Browse the repository at this point in the history
  • Loading branch information
gurgalex committed Feb 20, 2019
1 parent 74e35d2 commit b2a02c8
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions src/librustc_mir/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1545,20 +1545,24 @@ Erroneous code example:
```compile_fail,E0505
struct Value {}
fn borrow(val: &Value) {}
fn eat(val: Value) {}
fn main() {
let x = Value{};
{
let _ref_to_val: &Value = &x;
eat(x);
borrow(_ref_to_val);
}
}
```
Here, the function `eat` takes the ownership of `x`. However,
`x` cannot be moved because it was borrowed to `_ref_to_val`.
To fix that you can do few different things:
Here, the function `eat` takes ownership of `x`. However,
`x` cannot be moved because the borrow to `_ref_to_val`
needs to last till the function `borrow`.
To fix that you can do a few different things:
* Try to avoid moving the variable.
* Release borrow before move.
Expand All @@ -1569,13 +1573,16 @@ Examples:
```
struct Value {}
fn borrow(val: &Value) {}
fn eat(val: &Value) {}
fn main() {
let x = Value{};
{
let _ref_to_val: &Value = &x;
eat(&x); // pass by reference, if it's possible
borrow(_ref_to_val);
}
}
```
Expand All @@ -1585,12 +1592,15 @@ Or:
```
struct Value {}
fn borrow(val: &Value) {}
fn eat(val: Value) {}
fn main() {
let x = Value{};
{
let _ref_to_val: &Value = &x;
borrow(_ref_to_val);
}
eat(x); // release borrow and then move it.
}
Expand All @@ -1602,13 +1612,16 @@ Or:
#[derive(Clone, Copy)] // implement Copy trait
struct Value {}
fn borrow(val: &Value) {}
fn eat(val: Value) {}
fn main() {
let x = Value{};
{
let _ref_to_val: &Value = &x;
eat(x); // it will be copied here.
borrow(_ref_to_val);
}
}
```
Expand Down

0 comments on commit b2a02c8

Please sign in to comment.