Skip to content

Commit

Permalink
Rollup merge of rust-lang#58607 - gurgalex:fail_E0505_for_2018_editio…
Browse files Browse the repository at this point in the history
…n, r=matthewjasper

Fixes rust-lang#58586: Make E0505 erronous example fail for the 2018 edition

The original example worked for 2015, but not the 2018 edition of Rust.

Borrowing the moved value after ownership is transferred seems required for 2018.

[this](rust-lang/rust@rust-lang:f66e469...gurgalex:b2a02c8#diff-4ca866aea4a6efecd732f1975faaad88R1564) line though is correct for 2018, but not for the 2015 edition.

Fix rust-lang#58586
  • Loading branch information
Centril committed Feb 22, 2019
2 parents b28a32f + 6a5abea commit 59f1a56
Showing 1 changed file with 22 additions and 15 deletions.
37 changes: 22 additions & 15 deletions src/librustc_mir/diagnostics.rs
Expand Up @@ -1545,20 +1545,22 @@ 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);
}
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,14 +1571,15 @@ 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
}
let _ref_to_val: &Value = &x;
eat(&x); // pass by reference, if it's possible
borrow(_ref_to_val);
}
```
Expand All @@ -1585,12 +1588,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,14 +1608,15 @@ 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.
}
let _ref_to_val: &Value = &x;
eat(x); // it will be copied here.
borrow(_ref_to_val);
}
```
Expand Down

0 comments on commit 59f1a56

Please sign in to comment.