Skip to content
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

Merged
merged 2 commits into from
Feb 23, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 22 additions & 15 deletions src/librustc_mir/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1545,20 +1545,22 @@ Erroneous code example:
```compile_fail,E0505
struct Value {}
Copy link
Contributor

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 {} over struct Value;?

Copy link
Contributor Author

@gurgalex gurgalex Feb 21, 2019

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.


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.
Expand All @@ -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);
}
```

Expand All @@ -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);
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should I keep this set of braces in?
It is one way to resolve the error.

Copy link
Contributor

Choose a reason for hiding this comment

The 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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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
Take by ref: Passes
Implement Copy: Passes

Copy link
Contributor

Choose a reason for hiding this comment

The 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.
}
Expand All @@ -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);
}
```

Expand Down