-
Notifications
You must be signed in to change notification settings - Fork 103
Fix pprintast with ppat_constraint in let bindings #588
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
Fix pprintast with ppat_constraint in let bindings #588
Conversation
…raint Signed-off-by: Nathan Rebours <[email protected]>
fd8a768 to
58dc7a6
Compare
Signed-off-by: Nathan Rebours <[email protected]>
Signed-off-by: Nathan Rebours <[email protected]>
|
@patricoferris if you have some time to review this that would be appreciated, I don't want to oversee anything here! @hhugo if you want to try this one out with gen_js_api it should fix the syntax error thing but I don't expect it will make the test suite pass as some other changes in pprintast seem to be responsible for the diffs. |
I've opened LexiFi/gen_js_api#181 to better use the 5.2 ast |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @NathanReb for this fix, not a very obvious one (at least to me)!
To be more precise, the compiler is still going internally through a translation |
CHANGES: - Make Ast_builder's default `value_binding` constructor generate the proper `pvb_constraint` from the pattern and expression arguments. (ocaml-ppx/ppxlib#589, @NathanReb) - Fix pprintast to output correct syntax from `Ppat_constraint (pat, Ptyp_poly ...)` nodes until they are completely dropped. (ocaml-ppx/ppxlib#588, @NathanReb)
CHANGES: - Make Ast_builder's default `value_binding` constructor generate the proper `pvb_constraint` from the pattern and expression arguments. (ocaml-ppx/ppxlib#589, @NathanReb) - Fix pprintast to output correct syntax from `Ppat_constraint (pat, Ptyp_poly ...)` nodes until they are completely dropped. (ocaml-ppx/ppxlib#588, @NathanReb)
CHANGES: - Make Ast_builder's default `value_binding` constructor generate the proper `pvb_constraint` from the pattern and expression arguments. (ocaml-ppx/ppxlib#589, @NathanReb) - Fix pprintast to output correct syntax from `Ppat_constraint (pat, Ptyp_poly ...)` nodes until they are completely dropped. (ocaml-ppx/ppxlib#588, @NathanReb)
We recently updated our internal copy of
pprintastto reflect our internal bump from the 4.14 AST to the 5.2 one.We used a strict copy of the compiler's 5.2 pprintast with a few minor modification:
Lexer.is_keywordIn 5.1, the AST changed so that type constraint in let bindings are attached to the whole value binding node rather than to the pattern part. That means that the following source code:
is now represented slightly differently, the
int -> inttype constraint is not embedded in the pattern as abut separately, in the
pvb_constraint field, leaving the pattern to be a simplePpat_var ....In addition, the
Ptyp_poly ...core type construction is only allowed in specific places in the AST, though it is not restricted to those places by the AST types. With the change in the 5.1 AST, it is not expected to be found in the pattern part of a value binding but in thepvb_constraintfield.As stated though, one can still build an AST node with the constraint embedded in the pattern. Trying to pretty print this with Ocaml 5.1 or above pprintast will produce code looking like this:
as opposed to the following form:
which is how those nodes were printed pre 5.1 and how the new nodes, embedding the type constraint in the pvb_constraint field are printed now.
It's important to note that the former is not and never was valid ocaml syntax and gets rejected by the parser:
I think it's safe to assume that such constructions (
Ptyp_polyin aPpat_constraintrather than in thepvb_constraintfield) are not supported by the compiler anymore and should be replaced in favor of the new representation.That being said, it seems some part of the compiler are still able to process those correctly and ppx that generate such nodes seem to still function, as long as they don't rely on the unmigrated AST to printed as source code.
For that reason I propose this PR which adds custom support for this construction and prints it with the valid syntax. It's important to note that doing so, the source code won't parse back to it's original AST but I consider this okay as there is no way to produce source code that would parse to this construction anyway.
We will probably drop support for this in the long run as it's likely that the compiler itself will entirely reject those at some point as well.
I'm also considering changing
Ast_builder.value_bindingso that it detectsPpat_constraintin the pattern part and moves it to thepvb_constraintto avoid users the need to update their code.Let me know what you think!