diff options
| author | Mazdak Farrokhzad <twingoow@gmail.com> | 2019-02-22 14:58:11 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-02-22 14:58:11 +0100 |
| commit | 59f1a563d5a5f6aa9f34df5ed4b7a3ad9d154ac4 (patch) | |
| tree | 78a81b28058e86fb26d6eeb4f2f308ff442c2862 /src | |
| parent | b28a32fba77f7c50497e15109773838fef07b0e7 (diff) | |
| parent | 6a5abea7092cf7ede5bb6b3489db94eb701cd04e (diff) | |
| download | rust-59f1a563d5a5f6aa9f34df5ed4b7a3ad9d154ac4.tar.gz rust-59f1a563d5a5f6aa9f34df5ed4b7a3ad9d154ac4.zip | |
Rollup merge of #58607 - gurgalex:fail_E0505_for_2018_edition, r=matthewjasper
Fixes #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](https://github.com/rust-lang/rust/compare/rust-lang:f66e469...gurgalex:b2a02c8#diff-4ca866aea4a6efecd732f1975faaad88R1564) line though is correct for 2018, but not for the 2015 edition. Fix #58586
Diffstat (limited to 'src')
| -rw-r--r-- | src/librustc_mir/diagnostics.rs | 37 |
1 files changed, 22 insertions, 15 deletions
diff --git a/src/librustc_mir/diagnostics.rs b/src/librustc_mir/diagnostics.rs index 4df3004a9ad..f369324157a 100644 --- a/src/librustc_mir/diagnostics.rs +++ b/src/librustc_mir/diagnostics.rs @@ -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. @@ -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); } ``` @@ -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. } @@ -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); } ``` |
