Skip to content

Commit 30b69c5

Browse files
authored
Rollup merge of rust-lang#58607 - gurgalex:fail_E0505_for_2018_edition, 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
2 parents 49ef8cf + 6a5abea commit 30b69c5

File tree

1 file changed

+22
-15
lines changed

1 file changed

+22
-15
lines changed

src/librustc_mir/diagnostics.rs

+22-15
Original file line numberDiff line numberDiff line change
@@ -1545,20 +1545,22 @@ Erroneous code example:
15451545
```compile_fail,E0505
15461546
struct Value {}
15471547
1548+
fn borrow(val: &Value) {}
1549+
15481550
fn eat(val: Value) {}
15491551
15501552
fn main() {
15511553
let x = Value{};
1552-
{
1553-
let _ref_to_val: &Value = &x;
1554-
eat(x);
1555-
}
1554+
let _ref_to_val: &Value = &x;
1555+
eat(x);
1556+
borrow(_ref_to_val);
15561557
}
15571558
```
15581559
1559-
Here, the function `eat` takes the ownership of `x`. However,
1560-
`x` cannot be moved because it was borrowed to `_ref_to_val`.
1561-
To fix that you can do few different things:
1560+
Here, the function `eat` takes ownership of `x`. However,
1561+
`x` cannot be moved because the borrow to `_ref_to_val`
1562+
needs to last till the function `borrow`.
1563+
To fix that you can do a few different things:
15621564
15631565
* Try to avoid moving the variable.
15641566
* Release borrow before move.
@@ -1569,14 +1571,15 @@ Examples:
15691571
```
15701572
struct Value {}
15711573
1574+
fn borrow(val: &Value) {}
1575+
15721576
fn eat(val: &Value) {}
15731577
15741578
fn main() {
15751579
let x = Value{};
1576-
{
1577-
let _ref_to_val: &Value = &x;
1578-
eat(&x); // pass by reference, if it's possible
1579-
}
1580+
let _ref_to_val: &Value = &x;
1581+
eat(&x); // pass by reference, if it's possible
1582+
borrow(_ref_to_val);
15801583
}
15811584
```
15821585
@@ -1585,12 +1588,15 @@ Or:
15851588
```
15861589
struct Value {}
15871590
1591+
fn borrow(val: &Value) {}
1592+
15881593
fn eat(val: Value) {}
15891594
15901595
fn main() {
15911596
let x = Value{};
15921597
{
15931598
let _ref_to_val: &Value = &x;
1599+
borrow(_ref_to_val);
15941600
}
15951601
eat(x); // release borrow and then move it.
15961602
}
@@ -1602,14 +1608,15 @@ Or:
16021608
#[derive(Clone, Copy)] // implement Copy trait
16031609
struct Value {}
16041610
1611+
fn borrow(val: &Value) {}
1612+
16051613
fn eat(val: Value) {}
16061614
16071615
fn main() {
16081616
let x = Value{};
1609-
{
1610-
let _ref_to_val: &Value = &x;
1611-
eat(x); // it will be copied here.
1612-
}
1617+
let _ref_to_val: &Value = &x;
1618+
eat(x); // it will be copied here.
1619+
borrow(_ref_to_val);
16131620
}
16141621
```
16151622

0 commit comments

Comments
 (0)