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

Add match guard to AST #577

Closed
wants to merge 7 commits into from
Closed

Add match guard to AST #577

wants to merge 7 commits into from

Conversation

cmester0
Copy link
Contributor

@cmester0 cmester0 commented Mar 21, 2024

I have added the match guard to the AST. Currently no backend supports these, so this makes the rejection of this feature explicit. Furthermore we can now add a phase for removing match conditionals. This should also allow us to rewrite record pattern matching into guarded match statements (should help with #180 / #168).

@cmester0 cmester0 changed the title WIP Add match guard to AST Mar 21, 2024
@cmester0 cmester0 marked this pull request as ready for review March 21, 2024 15:12
@cmester0 cmester0 requested a review from W95Psp March 21, 2024 15:12
@cmester0
Copy link
Contributor Author

cmester0 commented Mar 21, 2024

We can add a phase that unrolls (guarded) matches, e.g.

match foo {
    Foo { a, .. } if a > 10 => 1,
    Foo { b, .. } if b => 2,
    _ => 3,
};

becomes

if let Foo { a, .. } = foo && a > 10 {
  1
} else if let Foo { b, .. } = foo && b {
  2
} else if let _ = foo {
  3
} else {
  panic!()
};

Which should allow us to do matching on records by moving the projection to the right side

if let a = foo.a && a > 10 {
  1
} else if let b = foo.b && b {
  2
} else if let _ = foo {
  3
} else {
  panic!()
};

Which can be refolded into match statements, as support for let_chains (experimental feature) e.g. if let _ && _ also seems to be missing.

@W95Psp
Copy link
Collaborator

W95Psp commented Apr 8, 2024

Hi Lasse, thanks for the PR, supporting guards on arms would indeed be great.

Both let guards on arms and let chains on if and arms are nightly: I wonder if we want to support any of those, maybe we should only be supporting .. if <bool-expr> => ...

But as you say, having (chainable) let guars on arms might help us fixing #168: that might be a nice intermediate representation. We could rewrite matches that includes patterns some backends don't support into matches with guards, and then have a phase that "multiplexes" such guarded matches into nested matches.

Otherwise the PR looks good, there's just a few TODO to fix. If you want i can fix the ones on the visitors, tell me. (I plan to write a PPX for generating those at some point, this code is not nice to update)

@franziskuskiefer franziskuskiefer added the waiting-on-author Status: This is awaiting some action from the author. label Apr 11, 2024
Copy link

github-actions bot commented Sep 2, 2024

This PR has been marked as stale due to a lack of activity for 60 days. If you believe this pull request is still relevant, please provide an update or comment to keep it open. Otherwise, it will be closed in 7 days.

@github-actions github-actions bot added the stale label Sep 2, 2024
Copy link

This PR has been closed due to a lack of activity since being marked as stale. If you believe this pull request is still relevant, please reopen it with an update or comment.

@github-actions github-actions bot closed this Sep 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stale waiting-on-author Status: This is awaiting some action from the author.
Projects
No open projects
Status: Done
Development

Successfully merging this pull request may close these issues.

3 participants