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

Parse arbitrarily complex box patterns. #1733

Merged
merged 6 commits into from
Aug 25, 2019

Conversation

ecstatic-morse
Copy link
Contributor

@ecstatic-morse ecstatic-morse commented Aug 23, 2019

This fully resolves the pattern part of #1412 by enabling the parsing of complex box patterns such as:

let box Struct { box i, j: box Inner(box &x) } = todo!();

This introduces a new ast::BoxPat (in the mold of ast::RefPat) that gets translated to hir::Pat::Missing.

@ecstatic-morse
Copy link
Contributor Author

r? @matklad

@ecstatic-morse ecstatic-morse force-pushed the nested-box-pattern branch 2 times, most recently from 26bba51 to 075348a Compare August 23, 2019 22:02
@@ -416,6 +416,7 @@ pub(crate) fn match_arm_list(p: &mut Parser) {
// | X => (),
// box X => (),
// Some(box X) => (),
// Some(box Test{field: 0}) => (),
Copy link
Member

Choose a reason for hiding this comment

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

Hm, actually, I think these last three lines are misplaced. I specifically looked for this in the previous PR and somehow still missed it =/

The purpose of these inline tests is to show what exactly each specific bit of code parses. box patterns are not handled in this function.

I see that you've added tests to fn box_pat, where they belong, so these last three lines should be removed.

Some(m)
}

fn is_literal_pat_start(p: &mut Parser) -> bool {
fn is_literal_pat_start(p: &Parser) -> bool {
Copy link
Member

Choose a reason for hiding this comment

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

👍

// j: Box<Inner<'a>>,
// }
//
// struct Inner<'a>(&'a i32);
Copy link
Member

Choose a reason for hiding this comment

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

Let's remove struct Ounter and struct Inner, they are completely irrelvent for testing syntax

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So it seems that this test only passes if there's something named Inner and Outer in scope. This is probably a shortcoming of the parsing code?

Copy link
Member

Choose a reason for hiding this comment

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

It shouldn't be the case. Judging by the actual .rs file in the inline folder, looks like the test harness is confused over a blank line?

Copy link
Contributor Author

@ecstatic-morse ecstatic-morse Aug 23, 2019

Choose a reason for hiding this comment

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

I get the following error if the names aren't in scope. It's an error in my parsing code, not sure why it went away when I changed the test, maybe the blank line?

thread 'tests::parser_tests' panicked at 'assertion failed: `(left == right)`
  left: `[SyntaxError { kind: ParseError(ParseError("expected a name")), location: 52 }, SyntaxError { kind: ParseError(ParseError("expected COMMA")), location: 55 }]`,
 right: `[]`: There should be no errors in the file "/home/mackendy/src/rust/rust-analyzer/crates/ra_syntax/test_data/parser/inline/ok/0143_box_pat.rs"', crates/ra_syntax/src/tests.rs:23:9

0143_box_pat.rs:

fn main() {
    let box i = ();
    let box Outer { box i, j: box Inner(box &x) } = ();
    let box ref mut i = ();
}

Copy link
Contributor Author

@ecstatic-morse ecstatic-morse Aug 23, 2019

Choose a reason for hiding this comment

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

I needed to extend record_field_pat_list to handle the box i part. This syntax is equivalent to i: box i. I was a little surprised that it is in fact valid.

@ecstatic-morse ecstatic-morse force-pushed the nested-box-pattern branch 2 times, most recently from 19d5c9e to 75192f7 Compare August 23, 2019 22:45
@ecstatic-morse
Copy link
Contributor Author

@matklad. Okay, this should be good to go now. Do you want me to rebase to clean up the latest changes or does bors squash automatically?

@matklad
Copy link
Member

matklad commented Aug 23, 2019

bors doesn't squash, so a rebase wouldn't harm (though I personally don't value clean git history that much for small PR).

Named structs can have `box` patterns that will bind to their fields.
This is similar to the behavior of the `ref` and `mut` fields, but is at
least a little bit surprising.
@ecstatic-morse
Copy link
Contributor Author

@matklad I cleaned it up. It's ready to merge if you want to give it one last look.

@matklad
Copy link
Member

matklad commented Aug 25, 2019

bors r+

Thanks!

bors bot added a commit that referenced this pull request Aug 25, 2019
1733: Parse arbitrarily complex `box` patterns. r=matklad a=ecstatic-morse

This fully resolves the pattern part of #1412 by enabling the parsing of complex `box` patterns such as:

```rust
let box Struct { box i, j: box Inner(box &x) } = todo!();
```

This introduces a new `ast::BoxPat` (in the mold of `ast::RefPat`) that gets translated to `hir::Pat::Missing`.

Co-authored-by: Dylan MacKenzie <ecstaticmorse@gmail.com>
@bors
Copy link
Contributor

bors bot commented Aug 25, 2019

Build succeeded

@bors bors bot merged commit c93903e into rust-lang:master Aug 25, 2019
@ecstatic-morse ecstatic-morse deleted the nested-box-pattern branch August 26, 2019 05:21
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.

2 participants