-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Implement if let
#16741
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
Implement if let
#16741
Conversation
One thing to note is the error message for having a non-refutable pattern is kind of confusing: fn main() {
if let a = 1i {
println!("irrefutable pattern");
}
}
This could perhaps be fixed by attaching an attribute to the desugared |
Another option is to put an attribute on the generated |
I implemented the attribute approach, and then replaced it with an approach based on Prior to merging, the discarded approach (currently the attribute approach) should be removed from the commit list. |
If we do take this approach, I would mildly prefer that enum MatchSource {
MatchNormal,
MatchIfLetDesugar
} |
@huonw At first glance, it looks like that would require updating at least 19 locations across librustc and libsyntax just to account for the new parameter, and that's without actually even using it. It's certainly doable, but is it really appropriate? Do clients of Using |
I have a feeling that some/most of those could be changed to give error diagnostics specific to |
Most of what? Those aren't errors I was talking about. Those were just uses of the token |
They are 'just' uses that lead to compiler diagnostics, e.g.
They should not be talking about |
@huonw Ok I pushed a commit that uses |
As before, this commit obsoletes the two previous commits, but I haven't removed them, for comparison's sake. They will be removed if we go ahead with |
I've added the feature gate. At this point we just need to decide which approach to use for error reporting. Since it's already implemented, I'm in favor of @huonw's @nikomatsakis Since you seemed to care strongly that |
@huonw's suggestion is the same as what I had in mind. |
Modify ast::ExprMatch to include a new value of type ast::MatchSource, making it easy to tell whether the match was written literally or produced via desugaring. This allows us to customize error messages appropriately.
When an `if let` translates into a `match` with more than two arms, we don't want to print multiple "irrefutable if-let pattern" errors.
Force-pushed to remove the rejected approaches from commit history. r? |
@@ -2457,6 +2457,8 @@ The currently implemented features of the reference compiler are: | |||
whether this will continue as a feature or not. For these reasons, | |||
the glob import statement has been hidden behind this feature flag. | |||
|
|||
* `if_let` - Allows use of the `if let` desugaring syntax. |
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.
replace 'desugaring syntax' with 'feature' or 'statement' - the fact that it is implemented with desugaring is not relevant to the user
if self.is_keyword(kw) { | ||
self.bump(); | ||
true | ||
} else { false } |
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.
} else {
false
}
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.
We actually have no style guidelines here, and some minor precedence for making very short else expressions into one-liners, such as https://github.com/rust-lang/rust/blob/master/src/librustdoc/html/render.rs#L885
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.
That said, I will make the change, because this isn't something worth fighting for.
Looks good! r=me with the nits addressed |
@nick29581 Thanks! I'll push the updated version as soon as I make sure it works locally |
Same weird error three times in a row. It's looking less and less like a weird ephemeral error. But this PR has nothing to do with any of those run-pass-fulldeps issues, or with the plugin registry. |
A belated review comment - we should add @kballard If you are going to add stuff to this PR for the error, could you do that here. Otherwise, could you file an issue for it please? |
@nick29581 Good point. I'm still trying to figure out what the heck is going on with the errors though. |
This needs a rebase. |
Yes it does. I've been really busy lately, but I'm going to try and get back to this soon. Even before the rebase it was still having weird issues though, which is what was giving me problems in the first place. |
I chatted with @kballard on IRC and we agreed I could pick this up. I'll open a new PR. |
Closing in favor of #17634, thanks @jakub-! |
Implement the new desugaring
if let
syntax.Fixes #16779.