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

Add long diagnostics for "bind by-ref and by-move" #24482

Closed

Conversation

GuillaumeGomez
Copy link
Member

Part of #24407.

@rust-highfive
Copy link
Collaborator

r? @pcwalton

(rust_highfive has picked a reviewer for you, use r? to override)

@@ -112,6 +112,40 @@ reference when using guards or refactor the entire expression, perhaps by
putting the condition inside the body of the arm.
"##,

E0009: r##"
In a pattern, all values that don't implement the Copy trait have to be binded the same way
Copy link
Member

Choose a reason for hiding this comment

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

nit: s/binded/bound

Preferably keep "Copy" between backticks

Copy link
Contributor

Choose a reason for hiding this comment

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

I think it's also more natural if we leave off the "than the others".

@Manishearth
Copy link
Member

I'd prefer if code was marked as such, both with inline backticks and fences. It makes it easier to read.

Aside from that and a couple of nits, r=me

@GuillaumeGomez
Copy link
Member Author

@Manishearth, @michaelsproul: Thanks for your comments ! I corrected what you pointed. Do you see remaining things that could be improved ?

@@ -112,6 +112,49 @@ reference when using guards or refactor the entire expression, perhaps by
putting the condition inside the body of the arm.
"##,

E0009: r##"
In a pattern, all values that don't implement the `Copy` trait have to be bound
the same way. The goal here is to avoid to bind by-move and by-ref at the same
Copy link
Member

Choose a reason for hiding this comment

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

"The goal here is to avoid binding simultaneous by-move and by-ref"

@Manishearth
Copy link
Member

teensy bit

@michaelsproul
Copy link
Contributor

I feel like this doesn't give a good reason for why bind by-move and by-ref are disallowed in the same pattern. Does anyone know? I've been unable to come up with an example that would violate memory safety. Partial moves are allowed by the compiler, which seem almost identical. Is this error due to a limitation in the compiler's ability to reason about data-overlap in patterns?

@Manishearth
Copy link
Member

I feel like this doesn't give a good reason for why bind by-move and by-ref are disallowed in the same pattern. Does anyone know why?

So if we have (x, ref y), we invalidate the tuple with the move out via x, but we still need the tuple to exist for the ref y to work.

@GuillaumeGomez
Copy link
Member Author

@michaelsproul: Hum... Don't you think it would be too much ? I thought here was to give a help to the developer in order to make him understand what he made wrong and how to fix it.

@michaelsproul
Copy link
Contributor

@GuillaumeGomez: I think meaningful justifications both help people learn and make them happier to follow recommendations.

@Manishearth: I thought that too, but essentially the same thing works if you do it step by step:

fn main() {
    let p = ("x".to_string(), "y".to_string());
    let (_, ref y) = p;
    let (x, _) = p;
    println!("{} {}", x, y);
}

@Manishearth
Copy link
Member

Huh. That should work then.

}
```

You have two solutions: either you implement the `Copy` trait on the X structure:
Copy link
Contributor

Choose a reason for hiding this comment

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

This line is longer than 80 characters, might be best flowing across the next line:

There are two possible solutions.

1. Implement the `Copy` trait for the X structure:

// Code here

2. Bind the pattern's names the same way:

// Code here

(there's also a "the the" in the current version of the second case)

@michaelsproul
Copy link
Contributor

Thanks for doing this, sorry about the endless comments! 😄

(I avoided this error specifically because it looked hard)

@GuillaumeGomez
Copy link
Member Author

@michaelsproul: No problem, it's very kind of you to help me like this ! I actually took this error because I didn't know what it was, I discovered a whole new type of errors haha.

```

You have two solutions:
1. Implement the `Copy` trait for the X structure:
Copy link

Choose a reason for hiding this comment

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

I don't think this is a particularly good piece of advice. While it does indeed make the code in question compile, implementing Copy is often undesirable (such as for wide structures) and has its own set of ramifications that we should perhaps expand on here, or link to a document that does that.

Copy link
Member Author

Choose a reason for hiding this comment

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

I see your point. Should I precise that it would be better to do the second solution ?

Copy link
Member

Choose a reason for hiding this comment

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

Yes, and make the second solution the first solution.

@GuillaumeGomez
Copy link
Member Author

@jakub- @Manishearth: Done. Do you see anything else ?

@GuillaumeGomez
Copy link
Member Author

And it's good. If anyone does see something else to change, please just ask !

@alexcrichton
Copy link
Member

Thanks! Could you squash the commits together as well?

@GuillaumeGomez GuillaumeGomez force-pushed the error-explanation branch 2 times, most recently from bd1e778 to 9a15234 Compare April 16, 2015 21:20
@GuillaumeGomez
Copy link
Member Author

@alexcrichton: Done !

@alexcrichton
Copy link
Member

@bors: r+ 9a15234

@alexcrichton
Copy link
Member

@bors: rollup

@bors
Copy link
Contributor

bors commented Apr 17, 2015

☔ The latest upstream changes (presumably #24512) made this pull request unmergeable. Please resolve the merge conflicts.

Manishearth added a commit to Manishearth/rust that referenced this pull request Apr 17, 2015
@GuillaumeGomez
Copy link
Member Author

I resolved the conflicts.

@alexcrichton
Copy link
Member

@bors: r+ cf53f03

@alexcrichton
Copy link
Member

Closing in favor of the merged version at #24542

Manishearth added a commit to Manishearth/rust that referenced this pull request Apr 18, 2015
I did a manual merge of all the extended error PRs as we were getting merge conflicts yesterday. I think this is preferable to merging separately as I ended up having to manually merge @nham and @GuillaumeGomez's commits.

Rollup of rust-lang#24458, rust-lang#24482 and rust-lang#24488.

rust-lang#24482 and rust-lang#24488 were already re-approved, and would need to be cancelled if this is merged instead.
@GuillaumeGomez GuillaumeGomez deleted the error-explanation branch April 18, 2015 22:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants