-
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
Merged
NathanReb
merged 3 commits into
ocaml-ppx:0.36
from
NathanReb:fix-pprintast-oldschool-constraints
Aug 4, 2025
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| (executables | ||
| (names pprint_pvb_constraint pprint_ppat_constraint) | ||
| (libraries ppxlib astlib) | ||
| (preprocess | ||
| (pps ppxlib.metaquot))) | ||
|
|
||
| (cram | ||
| (package ppxlib) | ||
| (deps pprint_pvb_constraint.exe pprint_ppat_constraint.exe)) |
19 changes: 19 additions & 0 deletions
19
test/pprintast/oldschool-constraints/pprint_ppat_constraint.ml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| open Ppxlib | ||
|
|
||
| let loc = Location.none | ||
|
|
||
| let ast = | ||
| let vbs = | ||
| let pat = | ||
| Ast_builder.Default.ppat_constraint ~loc | ||
| [%pat? f] | ||
| (Ast_builder.Default.ptyp_poly ~loc | ||
| [ Loc.make ~loc "a" ] | ||
| [%type: 'a -> unit]) | ||
| in | ||
| let expr = [%expr fun _ -> ()] in | ||
| [ Ast_builder.Default.Latest.value_binding ~loc ~pat ~expr () ] | ||
| in | ||
| Ast_builder.Default.pstr_value ~loc Nonrecursive vbs | ||
|
|
||
| let () = Format.printf "%a\n" Pprintast.structure_item ast |
23 changes: 23 additions & 0 deletions
23
test/pprintast/oldschool-constraints/pprint_pvb_constraint.ml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| open Ppxlib | ||
|
|
||
| let loc = Location.none | ||
|
|
||
| let ast = | ||
| let vbs = | ||
| let pat = [%pat? f] in | ||
| let expr = [%expr fun _ -> ()] in | ||
| let constraint_ = | ||
| Pvc_constraint | ||
| { | ||
| locally_abstract_univars = []; | ||
| typ = | ||
| Ast_builder.Default.ptyp_poly ~loc | ||
| [ Loc.make ~loc "a" ] | ||
| [%type: 'a -> unit]; | ||
| } | ||
| in | ||
| [ Ast_builder.Default.Latest.value_binding ~loc ~pat ~expr ~constraint_ () ] | ||
| in | ||
| Ast_builder.Default.pstr_value ~loc Nonrecursive vbs | ||
|
|
||
| let () = Format.printf "%a\n" Pprintast.structure_item ast |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| We have two executables that prints the same bit of code: | ||
| let f : 'a . 'a -> unit = () | ||
| but represented with different ASTs: pprint_pvb_constraint encodes the type | ||
| constraint in the pvb_constraint field of the value_binding while | ||
| pprint_ppat_constraint encodes it in the pvb_pat field, i.e. the legacy way. | ||
|
|
||
|
|
||
| $ ./pprint_pvb_constraint.exe | ||
| let f : 'a . 'a -> unit = fun _ -> () | ||
|
|
||
| $ ./pprint_ppat_constraint.exe | ||
| let f : 'a . 'a -> unit = fun _ -> () | ||
|
|
||
| The legacy gets printed the same way as the pvb_constraint version to allow both | ||
| representation to coexist. The compiler's pprintast doesn't support it and prints | ||
| an incorrect syntax that does not parse. The compiler itself still seems to accept | ||
| such ASTs though, hence why we modified our pprintast to allow those. | ||
|
|
||
| The output should be accepted by the parser: | ||
|
|
||
| $ ./pprint_ppat_constraint.exe > test.ml | ||
| $ ocamlc test.ml |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.