Skip to content

Commit 30cbf0a

Browse files
authored
flambda-backend: Add Jane_syntax Pprintast tests (#1727)
* Add `Pprintast` tests * Transplant existing pprintast tests * Add one more interesting test and delimit tests with double semis * Complain if pprintast produces Jane Syntax attributes/extension nodes * clarify names according to review * Update a comment
1 parent 1269571 commit 30cbf0a

File tree

8 files changed

+178
-116
lines changed

8 files changed

+178
-116
lines changed

testsuite/tests/comprehensions/syntax.ml

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -17,41 +17,6 @@ let this_test_tests_that =
1717

1818
(******************************************************************************)
1919

20-
this_test_tests_that
21-
"we print list comprehensions correctly"
22-
;;
23-
24-
let test_printing parsed =
25-
let expr = Parse.expression (Lexing.from_string parsed) in
26-
let printed = Pprintast.string_of_expression expr in
27-
if parsed = printed then
28-
printf
29-
"Parsing and printing round-tripped successfully!\n\n\
30-
%s\n"
31-
parsed
32-
else
33-
printf
34-
"Parsing and printing failed to round-trip:\n\n\
35-
%s\n\n\
36-
became\n\n\
37-
%s\n"
38-
parsed printed
39-
;;
40-
41-
let () =
42-
(* The wonky formatting here is the best way to keep the line
43-
break visible in the string and also keep things within 80
44-
characters here *)
45-
test_printing
46-
"[(i, j)\n\
47-
\ for i = 0 to 9 when (i mod 2) = 0 for j = 0 to i when (i > 4) && (j > 4)]";
48-
(* Check that only user attributes survive pprintast *)
49-
test_printing
50-
"(([((x)[@attr1 ]) for ((x)[@attr2 ]) in (([])[@attr3 ])])[@attr4 ])";
51-
;;
52-
53-
(******************************************************************************)
54-
5520
this_test_tests_that
5621
"compiler-generated attributes start with \"jane.\""
5722
;;

testsuite/tests/comprehensions/syntax.reference

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,3 @@
1-
**** Test that we print list comprehensions correctly ****
2-
3-
Parsing and printing round-tripped successfully!
4-
5-
[(i, j)
6-
for i = 0 to 9 when (i mod 2) = 0 for j = 0 to i when (i > 4) && (j > 4)]
7-
Parsing and printing round-tripped successfully!
8-
9-
(([((x)[@attr1 ]) for ((x)[@attr2 ]) in (([])[@attr3 ])])[@attr4 ])
10-
111
**** Test that compiler-generated attributes start with "jane." ****
122

133
User attributes [@...] and extension nodes [%...] found:

testsuite/tests/lib-array/iarray_syntax.ml

Lines changed: 0 additions & 31 deletions
This file was deleted.

testsuite/tests/lib-array/iarray_syntax.reference

Lines changed: 0 additions & 6 deletions
This file was deleted.
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
(* Confirm that Pprintast round-trips with Jane Street experimental syntax.
2+
test.ml is the driver that runs this test. test.ml also checks that
3+
Pprintast doesn't print the attribute encoding.
4+
*)
5+
6+
(***********)
7+
(* Layouts *)
8+
9+
let f (type a : immediate) (x : a) = x;;
10+
let f (type (a : immediate)) (x : a) = x;;
11+
let f (type (a : immediate) (b : immediate)) (x : a) = x;;
12+
13+
let f y (type a : immediate) (x : a) = x;;
14+
let f y (type (a : immediate)) (x : a) = x;;
15+
let f y (type (a : immediate) (b : immediate)) (x : a) = x;;
16+
17+
let f y (type a : immediate) = y;;
18+
let f y (type (a : immediate)) = y;;
19+
let f y (type (a : immediate) (b : immediate)) = y;;
20+
21+
(* Just newtypes, no value parameters *)
22+
let f (type a : immediate) (type b : immediate)
23+
(type (c : immediate) (d : immediate))
24+
= ();;
25+
26+
(******************)
27+
(* Comprehensions *)
28+
29+
(* simple range *)
30+
[: x for x = 0 to 100 :];;
31+
[| x for x = 0 to 100 |];;
32+
[ x for x = 0 to 100 ];;
33+
34+
(* simple in-comprehension *)
35+
[: x for x in [: 1; 2; 3 :] :];;
36+
[| x for x in [| 1; 2; 3 |] |];;
37+
[ x for x in [ 1; 2; 3 ] ];;
38+
39+
(* complex comprehension *)
40+
[: x + y
41+
for x in [: 1; 2; 3 :]
42+
for y in [: 4; 5; 6 :]
43+
when x + y > 6
44+
:];;
45+
[| x + y
46+
for x in [| 1; 2; 3 |]
47+
for y in [| 4; 5; 6 |]
48+
when x + y > 6
49+
|];;
50+
[ x + y
51+
for x in [ 1; 2; 3 ]
52+
for y in [ 4; 5; 6 ]
53+
when x + y > 6
54+
];;
55+
56+
57+
(* User-written attributes *)
58+
([(x[@test.attr1]) for (x[@test.attr2]) in ([][@test.attr3])] [@test.attr4]);;
59+
60+
(*********)
61+
(* Local *)
62+
63+
(* parameters *)
64+
let f (local_ x) ~(local_ y) ~z:(local_ z) ?foo:(local_ w = 1) = x + y + z + w;;
65+
66+
(* bindings *)
67+
let g () =
68+
let local_ f = () in
69+
let local_ f : 'a . 'a -> 'a = fun x -> x in
70+
let local_ f x y = x + y in
71+
let local_ f : int -> int = fun z -> z + z in
72+
(* nroberts: we should reenable this test when we fix
73+
* pprint_ast to put the (int -> int) annotation back in
74+
* the correct position. *)
75+
(*let local_ f x y : int -> int = fun z -> x + y + z in*)
76+
();;
77+
78+
(* expressions *)
79+
let g () = local_
80+
let f = local_ () in
81+
let f x y = local_ (x + y) in
82+
local_ ();;
83+
84+
(* types *)
85+
type record =
86+
{ global_ field : int
87+
; normal_field : int
88+
};;
89+
90+
type 'a parameterized_record = {
91+
mutable a: 'a ;
92+
global_ c: 'a };;
93+
94+
type fn = local_ int -> local_ int;;
95+
type nested_fn = (local_ int -> local_ int) -> local_ int;;
96+
type ('a, 'b) labeled_fn =
97+
a:local_ 'a -> ?b:local_ b -> local_ 'a -> (int -> local_ 'b);;
98+
99+
(*******************)
100+
(* Include functor *)
101+
102+
module T = struct
103+
include functor F
104+
end;;
105+
106+
module type S = sig
107+
include functor F
108+
end;;
109+
110+
(********************)
111+
(* Immutable arrays *)
112+
113+
let f x =
114+
match x with
115+
| [::] -> [::]
116+
| ([:x:] [@test.attr1]) -> (([:x:])[@test.attr1])
117+
| ([:x;y:] [@test.attr2][@test.attr3]) ->
118+
([:x;y:] [@test.attr2][@test.attr3]);;

testsuite/tests/parsetree/test.ml

Lines changed: 60 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
(* TEST
22
include ocamlcommon
3-
readonly_files = "source.ml"
3+
readonly_files = "source.ml source_jane_street.ml"
44
*)
55

66
(* (c) Alain Frisch / Lexifi *)
@@ -52,14 +52,22 @@ let to_tmp_file print_fun ast =
5252
close_out oc;
5353
fn
5454

55-
let test parse_fun pprint print map filename =
55+
let test parse_fun pprint print map filename ~extra_checks =
5656
match from_file parse_fun filename with
5757
| exception exn ->
5858
Printf.printf "%s: FAIL, CANNOT PARSE\n" filename;
5959
report_err exn;
6060
print_endline "====================================================="
6161
| ast ->
6262
let str = to_string pprint ast in
63+
begin
64+
match extra_checks str with
65+
| Ok () -> ()
66+
| Error reason ->
67+
Printf.printf "%s: FAIL, %s\n" filename reason;
68+
print_endline str;
69+
print_endline"====================================================="
70+
end;
6371
match from_string parse_fun str with
6472
| exception exn ->
6573
Printf.printf "%s: FAIL, CANNOT REPARSE\n" filename;
@@ -79,28 +87,73 @@ let test parse_fun pprint print map filename =
7987
print_endline"====================================================="
8088
end
8189

82-
let test parse_fun pprint print map filename =
83-
try test parse_fun pprint print map filename
90+
let test parse_fun pprint print map filename ~extra_checks =
91+
try test parse_fun pprint print map filename ~extra_checks
8492
with exn -> report_err exn
8593

86-
let rec process path =
94+
let rec process path ~extra_checks =
8795
if Sys.is_directory path then
8896
let files = Sys.readdir path in
89-
Array.iter (fun s -> process (Filename.concat path s)) files
97+
Array.iter (fun s -> process (Filename.concat path s) ~extra_checks) files
9098
else if Filename.check_suffix path ".ml" then
9199
test
92100
Parse.implementation
93101
Pprintast.structure
94102
Printast.implementation
95103
(fun mapper -> mapper.Ast_mapper.structure)
96104
path
105+
~extra_checks
97106
else if Filename.check_suffix path ".mli" then
98107
test
99108
Parse.interface
100109
Pprintast.signature
101110
Printast.interface
102111
(fun mapper -> mapper.Ast_mapper.signature)
103112
path
113+
~extra_checks
114+
115+
let process ?(extra_checks = fun _ -> Ok ()) text = process text ~extra_checks
116+
117+
(* Produce an error if any attribute/extension node does not start with the
118+
text prefix.
119+
120+
This over-conservatively produces an error for attributes/extension nodes
121+
that don't appear literally as '[@' or '[%' followed immediately by an
122+
identifier. Some things that aren't handled:
123+
- [@ blah]
124+
- [% blah]
125+
- [@@blah]
126+
- [%%blah]
127+
128+
We've chosen to keep those constructs out of the test file in preference
129+
to updating this logic to properly handle them (which is hard).
130+
*)
131+
let check_all_attributes_and_extensions_start_with text ~prefix =
132+
let check introduction_string =
133+
String.split_on_char '[' text
134+
|> List.for_all (fun s ->
135+
if String.starts_with s ~prefix:introduction_string
136+
then String.starts_with s ~prefix:(introduction_string ^ prefix)
137+
else true)
138+
in
139+
if check "%" && check "@"
140+
then Ok ()
141+
else
142+
Error
143+
(Printf.sprintf
144+
"Pprintast produced an extension node or attribute that doesn't \
145+
begin with `%s'"
146+
prefix)
147+
;;
104148
105149
let () =
106-
process "source.ml"
150+
process "source.ml";
151+
Language_extension.enable_maximal ();
152+
process "source_jane_street.ml" ~extra_checks:(fun text ->
153+
(* Check that printing Jane Street language extensions produces no more
154+
attributes or extension nodes than the input program, all of whose
155+
attributes begin with "test". This ensures that Jane Syntax attributes
156+
aren't printed.
157+
*)
158+
check_all_attributes_and_extensions_start_with text ~prefix:"test");
159+
;;

testsuite/tests/typing-local/example_syntax.ml

Lines changed: 0 additions & 12 deletions
This file was deleted.

testsuite/tests/typing-local/print_syntax.ml

Lines changed: 0 additions & 15 deletions
This file was deleted.

0 commit comments

Comments
 (0)