Skip to content

Commit b2a02c8

Browse files
committed
Fixes rust-lang#58586: Make E0505 explain example fail for 2018 edition
1 parent 74e35d2 commit b2a02c8

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

src/librustc_mir/diagnostics.rs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1545,20 +1545,24 @@ 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{};
15521554
{
15531555
let _ref_to_val: &Value = &x;
15541556
eat(x);
1557+
borrow(_ref_to_val);
15551558
}
15561559
}
15571560
```
15581561
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:
1562+
Here, the function `eat` takes ownership of `x`. However,
1563+
`x` cannot be moved because the borrow to `_ref_to_val`
1564+
needs to last till the function `borrow`.
1565+
To fix that you can do a few different things:
15621566
15631567
* Try to avoid moving the variable.
15641568
* Release borrow before move.
@@ -1569,13 +1573,16 @@ Examples:
15691573
```
15701574
struct Value {}
15711575
1576+
fn borrow(val: &Value) {}
1577+
15721578
fn eat(val: &Value) {}
15731579
15741580
fn main() {
15751581
let x = Value{};
15761582
{
15771583
let _ref_to_val: &Value = &x;
15781584
eat(&x); // pass by reference, if it's possible
1585+
borrow(_ref_to_val);
15791586
}
15801587
}
15811588
```
@@ -1585,12 +1592,15 @@ Or:
15851592
```
15861593
struct Value {}
15871594
1595+
fn borrow(val: &Value) {}
1596+
15881597
fn eat(val: Value) {}
15891598
15901599
fn main() {
15911600
let x = Value{};
15921601
{
15931602
let _ref_to_val: &Value = &x;
1603+
borrow(_ref_to_val);
15941604
}
15951605
eat(x); // release borrow and then move it.
15961606
}
@@ -1602,13 +1612,16 @@ Or:
16021612
#[derive(Clone, Copy)] // implement Copy trait
16031613
struct Value {}
16041614
1615+
fn borrow(val: &Value) {}
1616+
16051617
fn eat(val: Value) {}
16061618
16071619
fn main() {
16081620
let x = Value{};
16091621
{
16101622
let _ref_to_val: &Value = &x;
16111623
eat(x); // it will be copied here.
1624+
borrow(_ref_to_val);
16121625
}
16131626
}
16141627
```

0 commit comments

Comments
 (0)