diff options
| author | Alex Gurganus <gurgalex@iastate.edu> | 2019-02-20 13:03:52 -0600 |
|---|---|---|
| committer | Alex Gurganus <gurgalex@iastate.edu> | 2019-02-20 14:46:42 -0600 |
| commit | b2a02c8d4aeb74fd6adc23e5eec935fc064e153a (patch) | |
| tree | b6a7714f75125d3ae926a4130ca77cf9aa878085 /src | |
| parent | 74e35d270067afff72034312065c48e6d8cfba67 (diff) | |
| download | rust-b2a02c8d4aeb74fd6adc23e5eec935fc064e153a.tar.gz rust-b2a02c8d4aeb74fd6adc23e5eec935fc064e153a.zip | |
Fixes #58586: Make E0505 explain example fail for 2018 edition
Diffstat (limited to 'src')
| -rw-r--r-- | src/librustc_mir/diagnostics.rs | 19 |
1 files changed, 16 insertions, 3 deletions
diff --git a/src/librustc_mir/diagnostics.rs b/src/librustc_mir/diagnostics.rs index 4df3004a9ad..1aac78d8adc 100644 --- a/src/librustc_mir/diagnostics.rs +++ b/src/librustc_mir/diagnostics.rs @@ -1545,6 +1545,8 @@ Erroneous code example: ```compile_fail,E0505 struct Value {} +fn borrow(val: &Value) {} + fn eat(val: Value) {} fn main() { @@ -1552,13 +1554,15 @@ fn main() { { 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. @@ -1569,6 +1573,8 @@ Examples: ``` struct Value {} +fn borrow(val: &Value) {} + fn eat(val: &Value) {} fn main() { @@ -1576,6 +1582,7 @@ fn main() { { let _ref_to_val: &Value = &x; eat(&x); // pass by reference, if it's possible + borrow(_ref_to_val); } } ``` @@ -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. } @@ -1602,6 +1612,8 @@ Or: #[derive(Clone, Copy)] // implement Copy trait struct Value {} +fn borrow(val: &Value) {} + fn eat(val: Value) {} fn main() { @@ -1609,6 +1621,7 @@ fn main() { { let _ref_to_val: &Value = &x; eat(x); // it will be copied here. + borrow(_ref_to_val); } } ``` |
