-
Notifications
You must be signed in to change notification settings - Fork 303
blog post: recent & future pattern matching improvements #529
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
Conversation
NOTE: the file name needs to be adjusted depending on when this is merged. |
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.
Overall looks good. Interested to hear what you think on some of the comments, but generally seems like we can likely move ahead.
posts/inside-rust/2020-03-02-recent-future-pattern-matching-improvements.md
Outdated
Show resolved
Hide resolved
let Ok(x) | Err(x) = foo(); | ||
``` | ||
|
||
An OR-pattern covers the *union* of all the `|`-ed ("or-ed") patterns. To ensure that whatever alternative matched, all bindings are consistent and initialized, each or-ed pattern must include the exact same set of bindings, with the same types, and the same binding modes. |
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.
Why do we need the same binding modes? I would've expected e.g. it be fine to have Ok(ref x) | Err(x)
with Result<u32, &u32>
producing x: &u32
.
Is this just a match/borrow check limitation?
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.
It's possible that there's no good reason for this limitation (which is encoded in librustc_resolve) to exist. I'm not exactly sure why it does, so my answer is the non-answer "this is how it has always been" ;)
error[E0409]: variable `x` is bound in inconsistent ways within the same match arm
--> src/main.rs:2:25
|
2 | let Ok(ref x) | Err(x) = Ok(0);
| - ^ bound in different ways
| |
| first binding
Maybe @petrochenkov or @matthewjasper know why.
(Note to self: the diagnostic should not mention "match arm".)
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.
The compiler doesn't currently track binding modes separately for each binding. Since Ok(x) | Err(mut x)
has to be an error, and Ok(ref x) | Err(&ref x)
is allowed, there isn't much reason to change this.
posts/inside-rust/2020-03-02-recent-future-pattern-matching-improvements.md
Outdated
Show resolved
Hide resolved
Co-Authored-By: Mark Rousskov <[email protected]>
posts/inside-rust/2020-03-02-recent-future-pattern-matching-improvements.md
Outdated
Show resolved
Hide resolved
…020-03-04-recent-future-pattern-matching-improvements.md
r? @nikomatsakis