Skip to content

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

Merged
merged 4 commits into from
Mar 4, 2020

Conversation

Centril
Copy link
Contributor

@Centril Centril commented Mar 2, 2020

@Centril
Copy link
Contributor Author

Centril commented Mar 3, 2020

NOTE: the file name needs to be adjusted depending on when this is merged.

Copy link
Member

@Mark-Simulacrum Mark-Simulacrum left a 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.

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.
Copy link
Member

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?

Copy link
Contributor Author

@Centril Centril Mar 3, 2020

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".)

Copy link
Contributor

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.

@Mark-Simulacrum Mark-Simulacrum merged commit a450ede into rust-lang:master Mar 4, 2020
@Centril Centril deleted the patch-3 branch March 4, 2020 00:05
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.

4 participants