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

Multiway match parsing for pattern guards #1588

Merged

Conversation

rajgodse
Copy link

Implements parsing for the more general "multiway match" syntax, allowing pattern guards to contain multiple cases, each with their own associated right-hand side.

We change the parsetree to reflect this change, instead of having a guard as an optional argument independent of the rhs, we roll it into a new "case rhs" type.

This changes the allowed Ocaml syntax for parsing, and this PR tests that the new construct is parsed successfully. For the moment, this PR breaks existing pattern guard typechecking and translation.

There are a lot of parts of the compiler that assume that the right hand side of a case is an expression: this is no longer true under the new parsetree type. For this reason, some existing checks needed to be modified to accomodate pattern guards.

A common transformation: for typechecking procedures, a pattern guard | p1 when e1 match (| pi -> ei) is treated as | p1 -> (match e1 with | pi -> ei).

@ncik-roberts ncik-roberts self-assigned this Jul 24, 2023
Copy link
Contributor

@ncik-roberts ncik-roberts left a comment

Choose a reason for hiding this comment

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

This is coming along well.

ocaml/parsing/ast_iterator.ml Outdated Show resolved Hide resolved
ocaml/parsing/parser.mly Show resolved Hide resolved
ocaml/parsing/parsetree.mli Outdated Show resolved Hide resolved
ocaml/parsing/ast_iterator.ml Outdated Show resolved Hide resolved
ocaml/parsing/parsetree.mli Show resolved Hide resolved
ocaml/testsuite/tests/pattern-guards/test.ml Outdated Show resolved Hide resolved
ocaml/typing/typecore.ml Show resolved Hide resolved
ocaml/typing/typecore.ml Outdated Show resolved Hide resolved
ocaml/typing/typecore.ml Outdated Show resolved Hide resolved
ocaml/typing/typecore.ml Outdated Show resolved Hide resolved
Copy link
Contributor

@ncik-roberts ncik-roberts left a comment

Choose a reason for hiding this comment

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

I'm preemptively approving to remind myself that I think this PR on the whole looks good -- there are just a few minor comments left to resolve.

ocaml/typing/typecore.ml Outdated Show resolved Hide resolved
@rajgodse
Copy link
Author

Rebase onto #1583 to improve test formatting.

@rajgodse
Copy link
Author

Rebase onto origin/pattern-guards to prevent future work

@ncik-roberts ncik-roberts merged commit 6d702c8 into ocaml-flambda:pattern-guards Aug 2, 2023
17 checks passed
rajgodse added a commit to rajgodse/flambda-backend that referenced this pull request Aug 18, 2023
* implements parsing for multiway matches

* implements parsing for multiway pattern guards

* fixes test output

* support optional bar before single pattern guard

* adds some multiway tests

* fix ocamlprof

* fix and test bar before single pattern guard

* make typecore changes less disruptive

* further refactor typecore

* further improve typecore code readability

* maps location in untypeast

* fixes printast_with_mappings typo

* translates single-case pattern guards successfully

* move case_rhs down in iterator and mapper record

* update parsetree comments

* iterate over location

* add whitespace to parser

* fix pretty printing

* matched style of printast

* add tests, disable extraneous warning

* fix typecore indentation

* replaces polymorphic variant with rhs variant

* removes staging from rhs type approx

* format: nested pattern guard line break

Co-authored-by: Nick Roberts <nroberts02@gmail.com>

* add pattern guard syntax to source test

* refactor type_approx_aux to avoid desc logic

* comment purpose of jane_test

* adds back test comments deleted in rebase

* promote test output after rebase

* refactor to sync Pexp_match and pg type approx

---------

Co-authored-by: Nick Roberts <nroberts02@gmail.com>
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.

None yet

2 participants