-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
fix: lower range patterns in hir #12158
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Was there a reason for this being
ExprId
? I couldn't think of a reason as to why this was the case, since it should bePatId
imoThere 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 values in a range pattern aren't patterns themselves, are they? They're the endpoints of the range you're checking for. You e.g. can't capture them with bind patterns (that wouldn't even make any sense).
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.
Well ye, not all are allowed there but the things accepted there are specifically a subset of patterns are they not (literals and path patterns)? https://doc.rust-lang.org/reference/patterns.html#range-patterns
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.
Hm. If rustc does it this way, I guess we can as well, but it doesn't make sense to me. The range bounds aren't matched against; they're expressions whose value is used to construct the range. But maybe I have the wrong mental model here.
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.
Rustc's HIR uses
Expr
though:https://github.com/rust-lang/rust/blob/12d3f107c1634ed41a800e220ccf99b665d906d8/compiler/rustc_hir/src/hir.rs#L1077
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.
IMO adjusting the AST would be the right thing to do. The mention of
PathPattern
in the reference seems like an error to me; Rustc directly parses a path or qualified path in this place, and produces anExpr
in the AST.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.
Ye, the main problem for us is how to do that. We first parse a pattern, then check if a range op follows. At this point we have to change the already parsed syntax. That is we need to change
IdentPat
into a path at that point for example which I don't think we can easily do.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.
rust-lang/reference#1204
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.
Hmm maybe @matklad has a good idea how to do this in the parser? But I guess it might need some refactoring of how we parse patterns, yeah.
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.
Hm, to me it seems correct to use
pat..pat
in the syntax, butexpr..expr
in the hir.Syntactically, they are patterns, as
..
is an infix binary operator. We could add some LR-style re-structuring of the tree, where we don't create a pat until we haven't seen..
, but that seems complicated. Feels more natural to parse a pat, than notice an..
, and parse anotherpat
.But yeah, in semantics we want to keep those as expressions, so during lowering we need to figure out that, what was a pattern syntactically, actually is an expression semantically.
This feel dual to recently stabilized destructive assignment, where we parse stuff like
(a, b) = (b, a)
asexpr = expr
, but, during lowering, lower the LHS as a pattern.Though, seeing the two example side-by-side, maybe we should bite the bullet and just don't distinguish patterns and expressions at the level of syntax? That is, we'd use
ast::Expr
for both patterns and expressions. We'd then have an API like