-
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
Implement slice pattern AST > HIR lowering #3062
Conversation
Also maybe somebody knows a better way to find the rest pattern in a slice pattern than the nested match I wrote? |
crates/ra_hir_def/src/body/lower.rs
Outdated
ast::Pat::SlicePat(p) => { | ||
let mut args = p.args().peekable(); | ||
let prefix = args | ||
.peeking_take_while(|p| match p { |
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.
This bit mostly does syntactic analysis, so I think we should move it to ra_syntax, extensions.rs specifically.
struct SlicePatComponents {
prefix: Vec<Pat>,
// which name rustc uses for this? rest doesn't seem like a perfect one
rest: Option<Pat>,
suffix: Vec<Pat>,
}
impl ast::SlicePat {
fn components(&self) -> SlicePatCompoents {... }
}
Not sure exactly about the precise shape of the type we want 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.
Re. naming, from rust-lang/rust#67712:
Common to both flavors is they use the token
..
, referred as a "rest pattern" in a pattern context.
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
uses the naming convention:
Slice {
before / prefix: List<Pat>,
slice: Option<Pat>,
after / suffix: List<Pat>,
},
where before
and after
are used in HIR and prefix
and suffix
are used in HAIR. I would recommend going with prefix
and suffix
(as I want to change HIR to that some day).
Common to both flavors is they use the token
..
, referred as a "rest pattern" in a pattern context.
This refers to the syntactic pattern production Pat = ... | Rest:".." ;
and is not specific to slice patterns. The semantic {binding @}? @ ..
is called a sub-slice. (syn
also uses Rest
.)
crates/ra_hir_def/src/expr.rs
Outdated
@@ -381,6 +381,7 @@ pub struct RecordFieldPat { | |||
pub enum Pat { | |||
Missing, | |||
Wild, | |||
DotDot, |
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.
Do we actually need a DotDot
pattern, or is it just the same as Wild
? 🤔
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.
Wild is _
and matches a single item, ..
is DotDot
and matches 0, 1, or many items.
We probably could use a better name though...
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.
They need to be distinguishable in both tuple (struct) patterns and slice patterns from my understanding, since e.g. &[_]
and &[..]
have entirely different meaning.
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.
I could rename it to Rest
, does that sound good? I think for the ..
pattern in tuples / tuple structs, this also makes 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.
&[_]
and&[..]
have entirely different meaning.
But isn't that difference in meaning expressed in the separation between prefix, rest and suffix pattern? I.e. in my understanding, [_]
is "prefix [_], rest none, suffix []", whereas [..]
is "prefix [], rest _, suffix []".
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 is to say, the ..
pattern obviously exists in the AST, but I'm not convinced it needs to exist in the desugared HIR.
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 does for tuple patterns, no? For the type (T, T, T)
the pattern (x, ..)
is valid but (x, _)
isn't.
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.
..
needs to be handled specially for tuple patterns. Just adding this as a pattern wouldn't help; tuple pattern inference needs to check whether there's a ..
and act accordingly. So it makes more sense to restructure tuple patterns similarly to Pat::Slice
. The same goes for ..
in record patterns; there it wouldn't even fit into the existing structure because there's no name. In other words, ..
isn't a pattern, it's a marker with special meaning in certain kinds of patterns. It can't stand alone.
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.
Note that there's no such variant in rustc either:
https://github.com/rust-lang/rust/blob/7494250106003d698579edef215d0c01810b5156/src/librustc_hir/hir.rs#L867-L920
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.
sgtm
…On Sun, 9 Feb 2020 at 13:57, Jonas Platte ***@***.***> wrote:
***@***.**** commented on this pull request.
------------------------------
In crates/ra_hir_def/src/expr.rs
<#3062 (comment)>
:
> @@ -381,6 +381,7 @@ pub struct RecordFieldPat {
pub enum Pat {
Missing,
Wild,
+ DotDot,
I could rename it to Rest, does that sound good? I think for the ..
pattern in tuple (structs), this also makes sense.
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#3062>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AANB3MYIZHRF3KJ2HRFDLBTRB74UDANCNFSM4KR57VLQ>
.
|
I don't think I'm going to be working on this anymore, sorry. I don't understand whether I first need to refactor tuples now, and I'm also having a hard time understanding most of the code I've looked at in ra_syntax. I hope the comments in this PR are helpful for whoever decides to work on this next. |
Sorry, just to be clear, there's no need to refactor tuples, they're a completely separate matter. I think the only thing that needs to be done here is to remove |
Could adding that mapping not regress tuples as it's then interpreted the same as Anyway, there's also the issue that I'm having trouble with the ast code. Your description of what has to be done over at #3043 seemed simple enough so I took a stab at it, but it seems to be more involved than I expected, and for now I want to use my time for other things. I might come back to this some time if nobody else implements it. |
So, thanks for your feedback, it was definitely useful. I just tried working on too many things at once. |
That's confusing to me since as far as I can see you're done with the part that touches the AST code. But anyway, it's fine if you've got other things to do, of course :) |
Okay I think I was just confused. Maybe I can finish this afterall.. 🤦♂️ |
Adressed your feedback, will implement the change @matklad suggested next |
I'm done with AST > HIR lowering now, have addressed all the feedback from above. I'll stop here because I don't grok the inference code at all :/ |
crates/ra_syntax/src/ast.rs
Outdated
@@ -217,7 +218,7 @@ fn test_doc_comment_multi_line_block_strips_suffix() { | |||
fn test_comments_preserve_trailing_whitespace() { | |||
let file = SourceFile::parse( | |||
r#" | |||
/// Representation of a Realm. | |||
/// Representation of a Realm. |
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 trailing whitespace here was intentional and is being tested for 😅
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.
My editor deleted it 🤦♂️
bors d+ LGTM, feel free to r+ when the build is fixed. |
✌️ jplatte can now approve this pull request. To approve and merge a pull request, simply reply with |
bors r+ |
3062: Implement slice pattern AST > HIR lowering r=jplatte a=jplatte WIP. The necessary changes for parsing are implemented, but actual inference is not yet. Just wanted to upload what I've got so far so it doesn't get duplicated :) Will fix #3043 Co-authored-by: Jonas Platte <[email protected]>
Build succeeded
|
WIP. The necessary changes for parsing are implemented, but actual inference is not yet. Just wanted to upload what I've got so far so it doesn't get duplicated :)
Will fix #3043