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

Express pattern guards using Jane_syntax #1672

Merged
merged 39 commits into from
Aug 15, 2023

Conversation

rajgodse
Copy link

Changing Parsetree requires painful updates to ppx's, so this PR rewrites the pattern guard feature using Jane_syntax.

In doing so, it creates a new syntactic category: Jane_syntax.Case.

The attribute marking the extension and the location of a Parsetree.case which represents a Jane_syntax.Case.t are stored in its field pc_rhs: Parsetree.expression. This rhs should never be interpreted as an expression by itself, which is why everywhere in the code that accesses the rhs of a Parsetree.case should first apply Jane_syntax.Case.of_ast (as documented in parsetree.mli). Ensuring that this invariant is introduced everywhere is one of the main difficulties of this PR.

However, the field pc_rhs is rarely accessed: ripgrepping yields the following files:

printer/printast_with_mappings.ml
ocaml/typing/typecore.ml
ocaml/tools/ocamlprof.ml
ocaml/typing/untypeast.ml
ocaml/parsing/parsetree.mli
ocaml/parsing/depend.ml
ocaml/parsing/jane_syntax.ml
ocaml/parsing/ast_iterator.ml
ocaml/parsing/jane_syntax_parsing.ml
ocaml/parsing/ast_helper.ml
ocaml/parsing/printast.ml
ocaml/parsing/pprintast.ml
ocaml/parsing/ast_mapper.ml

Of these, only printast.ml doesn't apply of_ast for cases, but this is fine as it doesn't apply it for any other syntactic category (it just prints the raw Parsetree instead).

Currently, there is only one constructor for the variant Jane_syntax.Case.t, namely Jcase_pattern_guarded of Jane_syntax.Pattern_guarded.case. The payload of this constructor stores the same data as Ppattern_guarded_rhs did previously, together with the case's lhs.

It is also worth noting the exact ast representation of Jane_syntax.Pattern_guarded.Pg_case { scrutinee; cases } is case : Parsetree.case with case.pc_rhs.pexp_desc = Pexp_match (scrutinee, cases), with extension attribute and location stored as described above.

@mshinwell mshinwell added typing lexer/parser Changes to the lexer and parser labels Aug 1, 2023
@rajgodse rajgodse marked this pull request as ready for review August 3, 2023 14:54
@ncik-roberts ncik-roberts self-assigned this Aug 3, 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.

I haven't finished review, but I've marked the files I have finished reviewing as "Viewed" and will continue from there the next time I take a look.

ocaml/parsing/jane_syntax.mli Outdated Show resolved Hide resolved
ocaml/parsing/jane_syntax_parsing.ml Outdated Show resolved Hide resolved
ocaml/parsing/jane_syntax_parsing.mli Outdated Show resolved Hide resolved
ocaml/parsing/jane_syntax_parsing.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.

Done revieweing for now. I made a few minor comments.

ocaml/parsing/parser.mly Outdated Show resolved Hide resolved
ocaml/parsing/parsetree.mli Outdated Show resolved Hide resolved
ocaml/parsing/parsetree.mli Outdated Show resolved Hide resolved
ocaml/testsuite/tests/parsetree/source.ml Outdated Show resolved Hide resolved
ocaml/tools/ocamlprof.ml Outdated 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.

Thanks, this is easier to review now.

ocaml/parsing/jane_syntax_parsing.ml Outdated Show resolved Hide resolved
ocaml/parsing/jane_syntax.ml Outdated 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
@rajgodse rajgodse force-pushed the jane-syntax branch 2 times, most recently from 9166657 to 1c51765 Compare August 9, 2023 22:14
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 have a few small comments, but nothing major. This is coming along well — we should be able to merge soon.

ocaml/parsing/jane_syntax_parsing.ml Show resolved Hide resolved
ocaml/testsuite/tests/parsetree/source.ml Outdated Show resolved Hide resolved
ocaml/parsing/jane_syntax_parsing.ml Outdated Show resolved Hide resolved
ocaml/parsing/jane_syntax.ml Outdated Show resolved Hide resolved
@rajgodse
Copy link
Author

Builds will fail until #1747 is merged.

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 will be good to merge once CI turns green.

@ncik-roberts ncik-roberts merged commit 06469e2 into ocaml-flambda:pattern-guards Aug 15, 2023
17 checks passed
rajgodse added a commit to rajgodse/flambda-backend that referenced this pull request Aug 18, 2023
* express pattern guards in jane syntax

* fix ocamlprof and flags

* update ast_mapper and ast_iterator

* add flag for failure on wrong syntactic category

* fix parsetree printer

* promote seq_bad output

* clean up types

* fix parsing test

* ignore attrs in pretty printer

* update field names in ocamlprof

* whitespace in seq_bad reference

* fix typechecking order of boolean guards

* add test for disambiguation from guard

* test: move nested match to test functionality

* remove attributes from jane_syntax cases

* rewrite case jane_syntax to use extension nodes

* update jane_syntax_parsing documentation

* remove debug print

* inline pattern guard generation in parser

* fix ocamlprof type

* remove bad advice in parsetree comment

* comment: give better advice about parsed cases

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

* reintroduce pc_rhs variants within typecore

* fix `Ast_helper.Exp.case` API

* remove unnecessary let

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

* remove unnecessary optional arguments

* unabbreviate parsed case rhs in helper

* type_approx_function bug

* check for jane syntax for uncurried function

* delete test that didn't exhibit previous badness

* move [fail_if_wrong_syntactic_category] to functor

* add test flags

* minimize diff from base

* fix syntax error post-rebase

* add parsetree tests back

* update [fail_if_wrong_syntactic_category] comment

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

* minor wording change to comment

* fail if pattern guarded case has pc_guard set

* documented Case0 fail_if_wrong_syntactic_category

---------

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
lexer/parser Changes to the lexer and parser typing
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants