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

AST cleanup: first class expression and patterns for records with opt… #7192

Merged
merged 2 commits into from
Dec 7, 2024

Conversation

cristianoc
Copy link
Collaborator

@cristianoc cristianoc commented Dec 5, 2024

…ional fields.

Extend record patterns and expressions in untyped and typed AST with a per-field boolean. To indicate x : ? e.
This was previously represented as an attribute res.optional.

Base automatically changed from clean-up-record-optional-labels to master December 5, 2024 19:15
@cristianoc cristianoc force-pushed the cleanup-optional-exp-pat branch from 58fd6c5 to 7a42482 Compare December 5, 2024 19:23
@@ -83,7 +83,8 @@ and pattern_desc =
See {!Types.row_desc} for an explanation of the last parameter.
*)
| Tpat_record of
(Longident.t loc * label_description * pattern) list * closed_flag
(Longident.t loc * label_description * pattern * bool (* optional *)) list
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

optional pattern now explicit

Copy link
Member

Choose a reason for hiding this comment

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

For cases like this, as the number of elements in the payload grows, do you think it would make sense to move to inline records at some point?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Definitely.
Keeping things self contained for this PR.

fields:
(Types.label_description
* record_label_definition
* bool (* optional *))
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

optional record field now explicit

@@ -183,7 +183,8 @@ and pattern_desc =
(* `A (None)
`A P (Some P)
*)
| Ppat_record of (Longident.t loc * pattern) list * closed_flag
| Ppat_record of
(Longident.t loc * pattern * bool (* optional *)) list * closed_flag
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

optional pattern now explicit

@@ -260,7 +261,9 @@ and expression_desc =
(* `A (None)
`A E (Some E)
*)
| Pexp_record of (Longident.t loc * expression) list * expression option
| Pexp_record of
(Longident.t loc * expression * bool (* optional *)) list
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

optional record field now explicit

@@ -261,7 +261,8 @@ let pattern sub pat =
| Tpat_record (list, closed) ->
Ppat_record
( List.map
(fun (lid, _, pat) -> (map_loc sub lid, sub.pat sub pat))
(fun (lid, _, pat, opt) ->
(map_loc sub lid, sub.pat sub pat, opt))
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This is where the extra field in typed ASTs is used: to compile back to untyped ASTs.
Before, this was passed down in attributes.

The nest phase, translation to lambda, does not need this information.

@cristianoc cristianoc requested review from zth and cknitt December 5, 2024 19:43
@@ -83,7 +83,8 @@ and pattern_desc =
See {!Types.row_desc} for an explanation of the last parameter.
*)
| Tpat_record of
(Longident.t loc * label_description * pattern) list * closed_flag
(Longident.t loc * label_description * pattern * bool (* optional *)) list
Copy link
Member

Choose a reason for hiding this comment

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

For cases like this, as the number of elements in the payload grows, do you think it would make sense to move to inline records at some point?

@@ -100,5 +100,6 @@ type ttt = {x: int, y?: string}
let optParen = {x: 3, y: ?(someBool ? Some("") : None)}
let optParen = {x: 3, y: ?(3 + 4)}
let optParen = {x: 3, y: ?foo(bar)}
let optParen = {x: 3, y: ?foo->bar}
let optParen = {x: 3, y: ?(foo->bar)}
Copy link
Member

Choose a reason for hiding this comment

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

Is the output change intentional?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It was random before.
We can make a choice now.

Before it was adding parens to 3+4 because it would see a printable attribute (res.optional was considered printable).
Now I have explicitly made binary operators adding parens.
I can special case -> if without parens is preferable.
Any thoughts?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

One possibility is to treat it just like any other unary prefix operator, so have the same rules as - e.

Copy link
Member

@cknitt cknitt Dec 6, 2024

Choose a reason for hiding this comment

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

Personally I actually like it better with parens, and if that avoids special casing -> here, all the better.

@cristianoc cristianoc enabled auto-merge (rebase) December 7, 2024 14:03
@cristianoc cristianoc merged commit 55f12e0 into master Dec 7, 2024
19 checks passed
@cristianoc cristianoc deleted the cleanup-optional-exp-pat branch December 7, 2024 14:12
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