Skip to content
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

Merged
merged 1 commit into from
Feb 11, 2020
Merged

Implement slice pattern AST > HIR lowering #3062

merged 1 commit into from
Feb 11, 2020

Conversation

jplatte
Copy link
Contributor

@jplatte jplatte commented Feb 9, 2020

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

@jplatte
Copy link
Contributor Author

jplatte commented Feb 9, 2020

Also maybe somebody knows a better way to find the rest pattern in a slice pattern than the nested match I wrote?

ast::Pat::SlicePat(p) => {
let mut args = p.args().peekable();
let prefix = args
.peeking_take_while(|p| match p {
Copy link
Member

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

Copy link
Contributor Author

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.

Copy link
Contributor

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

@@ -381,6 +381,7 @@ pub struct RecordFieldPat {
pub enum Pat {
Missing,
Wild,
DotDot,
Copy link
Member

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? 🤔

Copy link
Member

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

Copy link
Contributor Author

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.

Copy link
Contributor Author

@jplatte jplatte Feb 9, 2020

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.

Copy link
Member

@flodiebold flodiebold Feb 9, 2020

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 []".

Copy link
Member

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.

Copy link
Contributor Author

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.

Copy link
Member

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.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@matklad
Copy link
Member

matklad commented Feb 9, 2020 via email

@jplatte
Copy link
Contributor Author

jplatte commented Feb 10, 2020

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.

@jplatte jplatte closed this Feb 10, 2020
@flodiebold
Copy link
Member

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 Pat::DotDot and instead map ast::Pat::DotDotPat to Pat::Wild.

@jplatte
Copy link
Contributor Author

jplatte commented Feb 10, 2020

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.

@jplatte
Copy link
Contributor Author

jplatte commented Feb 10, 2020

So, thanks for your feedback, it was definitely useful. I just tried working on too many things at once.

@flodiebold
Copy link
Member

Could adding that mapping not regress tuples as it's then interpreted the same as _?

.. in tuples is not currently working either, as far as I know, so it wouldn't change anything.

I'm having trouble with the ast code

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 :)

@jplatte
Copy link
Contributor Author

jplatte commented Feb 10, 2020

That's confusing to me since as far as I can see you're done with the part that touches the AST code.

Okay I think I was just confused. Maybe I can finish this afterall.. 🤦‍♂️

@jplatte jplatte reopened this Feb 10, 2020
@jplatte
Copy link
Contributor Author

jplatte commented Feb 10, 2020

Adressed your feedback, will implement the change @matklad suggested next and then move to inference.

@jplatte jplatte changed the title Implement slice pattern inference Implement slice pattern AST > HIR lowering Feb 11, 2020
@jplatte jplatte marked this pull request as ready for review February 11, 2020 18:04
@jplatte
Copy link
Contributor Author

jplatte commented Feb 11, 2020

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 :/

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

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 😅

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My editor deleted it 🤦‍♂️

@flodiebold
Copy link
Member

bors d+

LGTM, feel free to r+ when the build is fixed.

@bors
Copy link
Contributor

bors bot commented Feb 11, 2020

✌️ jplatte can now approve this pull request. To approve and merge a pull request, simply reply with bors r+. More detailed instructions are available here.

@jplatte
Copy link
Contributor Author

jplatte commented Feb 11, 2020

bors r+

bors bot added a commit that referenced this pull request Feb 11, 2020
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]>
@bors
Copy link
Contributor

bors bot commented Feb 11, 2020

Build succeeded

  • Rust (macos-latest)
  • Rust (ubuntu-latest)
  • Rust (windows-latest)
  • TypeScript

@bors bors bot merged commit a3b104a into rust-lang:master Feb 11, 2020
@jplatte jplatte deleted the slice_patterns branch February 11, 2020 22:10
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.

No type inference for slice patterns
4 participants