Skip to content

Commit f325dd7

Browse files
authored
Merge pull request #600 from NathanReb/reimport-pprintast-tests
Reimport some pprintast tests from 0.36 branch
2 parents 7974f28 + 53533bb commit f325dd7

File tree

4 files changed

+162
-0
lines changed

4 files changed

+162
-0
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
(executables
2+
(names pprint_pvb_constraint pprint_ppat_constraint)
3+
(libraries ppxlib astlib)
4+
(preprocess
5+
(pps ppxlib.metaquot)))
6+
7+
(cram
8+
(package ppxlib)
9+
(deps pprint_pvb_constraint.exe pprint_ppat_constraint.exe))
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
open Ppxlib
2+
3+
let loc = Location.none
4+
5+
let ast =
6+
let vbs =
7+
let pat =
8+
Ast_builder.Default.ppat_constraint ~loc
9+
[%pat? f]
10+
(Ast_builder.Default.ptyp_poly ~loc
11+
[ Loc.make ~loc "a" ]
12+
[%type: 'a -> unit])
13+
in
14+
let expr = [%expr fun _ -> ()] in
15+
(* It is important that this is either built using [Latest.value_binding]
16+
or assembled manually as [Ast_builder.Defaut.value_binding] will
17+
generate a pvb_constraint, entirely defeatin the test's purpose. *)
18+
[ Ast_builder.Default.Latest.value_binding ~loc ~pat ~expr () ]
19+
in
20+
Ast_builder.Default.pstr_value ~loc Nonrecursive vbs
21+
22+
let print_source () = Format.printf "%a\n" Pprintast.structure_item ast
23+
let print_ast () = Format.printf "%a\n" Pp_ast.Default.structure_item ast
24+
25+
let () =
26+
match Sys.argv with
27+
| [| _exec |] -> print_source ()
28+
| [| _exec; _flag |] ->
29+
print_ast ();
30+
Format.printf "------- PRINTED AS -------\n";
31+
print_source ()
32+
| _ ->
33+
Printf.eprintf "Invalid usage!";
34+
exit 1
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
open Ppxlib
2+
3+
let loc = Location.none
4+
5+
let ast =
6+
let vbs =
7+
let pat = [%pat? f] in
8+
let expr = [%expr fun _ -> ()] in
9+
let constraint_ =
10+
Pvc_constraint
11+
{
12+
locally_abstract_univars = [];
13+
typ =
14+
Ast_builder.Default.ptyp_poly ~loc
15+
[ Loc.make ~loc "a" ]
16+
[%type: 'a -> unit];
17+
}
18+
in
19+
[ Ast_builder.Default.Latest.value_binding ~loc ~pat ~expr ~constraint_ () ]
20+
in
21+
Ast_builder.Default.pstr_value ~loc Nonrecursive vbs
22+
23+
let print_source () = Format.printf "%a\n" Pprintast.structure_item ast
24+
let print_ast () = Format.printf "%a\n" Pp_ast.Default.structure_item ast
25+
26+
let () =
27+
match Sys.argv with
28+
| [| _exec |] -> print_source ()
29+
| [| _exec; _flag |] ->
30+
print_ast ();
31+
Format.printf "------- PRINTED AS -------\n";
32+
print_source ()
33+
| _ ->
34+
Printf.eprintf "Invalid usage!";
35+
exit 1
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
We have two executables that prints the same bit of code:
2+
let f : 'a . 'a -> unit = ()
3+
but represented with different ASTs: pprint_pvb_constraint encodes the type
4+
constraint in the pvb_constraint field of the value_binding while
5+
pprint_ppat_constraint encodes it in the pvb_pat field, i.e. the legacy way.
6+
7+
8+
$ ./pprint_pvb_constraint.exe --with-ast
9+
Pstr_value
10+
( Nonrecursive
11+
, [ { pvb_pat = Ppat_var "f"
12+
; pvb_expr =
13+
Pexp_function
14+
( [ { pparam_loc = __loc
15+
; pparam_desc = Pparam_val ( Nolabel, None, Ppat_any)
16+
}
17+
]
18+
, None
19+
, Pfunction_body (Pexp_construct ( Lident "()", None))
20+
)
21+
; pvb_constraint =
22+
Some
23+
(Pvc_constraint
24+
{ locally_abstract_univars = []
25+
; typ =
26+
Ptyp_poly
27+
( [ "a"]
28+
, Ptyp_arrow
29+
( Nolabel
30+
, Ptyp_var "a"
31+
, Ptyp_constr ( Lident "unit", [])
32+
)
33+
)
34+
})
35+
; pvb_attributes = __attrs
36+
; pvb_loc = __loc
37+
}
38+
]
39+
)
40+
------- PRINTED AS -------
41+
let f : 'a . 'a -> unit = fun _ -> ()
42+
43+
$ ./pprint_ppat_constraint.exe --with-ast
44+
Pstr_value
45+
( Nonrecursive
46+
, [ { pvb_pat =
47+
Ppat_constraint
48+
( Ppat_var "f"
49+
, Ptyp_poly
50+
( [ "a"]
51+
, Ptyp_arrow
52+
( Nolabel
53+
, Ptyp_var "a"
54+
, Ptyp_constr ( Lident "unit", [])
55+
)
56+
)
57+
)
58+
; pvb_expr =
59+
Pexp_function
60+
( [ { pparam_loc = __loc
61+
; pparam_desc = Pparam_val ( Nolabel, None, Ppat_any)
62+
}
63+
]
64+
, None
65+
, Pfunction_body (Pexp_construct ( Lident "()", None))
66+
)
67+
; pvb_constraint = None
68+
; pvb_attributes = __attrs
69+
; pvb_loc = __loc
70+
}
71+
]
72+
)
73+
------- PRINTED AS -------
74+
let f : 'a . 'a -> unit = fun _ -> ()
75+
76+
The legacy gets printed the same way as the pvb_constraint version to allow both
77+
representation to coexist. The compiler's pprintast doesn't support it and prints
78+
an incorrect syntax that does not parse. The compiler itself still seems to accept
79+
such ASTs though, hence why we modified our pprintast to allow those.
80+
81+
The output should be accepted by the parser:
82+
83+
$ ./pprint_ppat_constraint.exe > test.ml
84+
$ ocamlc test.ml

0 commit comments

Comments
 (0)