-
Notifications
You must be signed in to change notification settings - Fork 12.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fixes #58586: Make E0505 erronous example fail for the 2018 edition #58607
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should I keep this set of braces in? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry, I forgot that we still have AST borrow check on the 2015 edition. Keep them for now. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I can remove the braces from all the examples except this one, and they give the expected results for both 2015 and 2018. erroneous: Fails There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok, it's fine like this for now. |
||
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); | ||
} | ||
``` | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there an opinion for
struct Value {}
overstruct Value;
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Didn't know that an empty struct could be made like that.
I'd want to keep the
{}
so that the reader doesn't have to guess what is being expressed.