Skip to content

Commit da5210d

Browse files
authored
flambda-backend: Fix layout annotation encoding to work better with ppxlib (#2234)
* add jane prefix * add test
1 parent e8351b7 commit da5210d

File tree

3 files changed

+44
-8
lines changed

3 files changed

+44
-8
lines changed

parsing/jane_syntax.ml

+3-2
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ module Make_payload_protocol_of_stringable (Stringable : Stringable) :
211211
let structure_item_of_none =
212212
{ pstr_desc =
213213
Pstr_attribute
214-
{ attr_name = Location.mknoloc "none";
214+
{ attr_name = Location.mknoloc "jane.none";
215215
attr_payload = PStr [];
216216
attr_loc = Location.none
217217
};
@@ -278,7 +278,8 @@ module Make_payload_protocol_of_stringable (Stringable : Stringable) :
278278
| _ -> raise Unexpected
279279

280280
let is_none_structure_item = function
281-
| { pstr_desc = Pstr_attribute { attr_name = { txt = "none" } } } ->
281+
| { pstr_desc = Pstr_attribute { attr_name = { txt = "jane.none" } } }
282+
->
282283
true
283284
| _ -> false
284285

testsuite/tests/parsetree/source_jane_street.ml

+3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
let f (type a : immediate) (x : a) = x;;
1010
let f (type (a : immediate)) (x : a) = x;;
1111
let f (type (a : immediate) (b : immediate)) (x : a) = x;;
12+
let f (type (a : immediate) (b : immediate) c) (x : a) = x;;
1213

1314
let f y (type a : immediate) (x : a) = x;;
1415
let f y (type (a : immediate)) (x : a) = x;;
@@ -17,6 +18,8 @@ let f y (type (a : immediate) (b : immediate)) (x : a) = x;;
1718
let f y (type a : immediate) = y;;
1819
let f y (type (a : immediate)) = y;;
1920
let f y (type (a : immediate) (b : immediate)) = y;;
21+
let f y (type (a : immediate) (b : immediate) c) = y;;
22+
2023

2124
(* Just newtypes, no value parameters *)
2225
let f (type a : immediate) (type b : immediate)

testsuite/tests/parsetree/test.ml

+38-6
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ let test parse_fun pprint print map filename ~extra_checks =
6161
| ast ->
6262
let str = to_string pprint ast in
6363
begin
64-
match extra_checks str with
64+
match extra_checks (to_string print ast) str with
6565
| Ok () -> ()
6666
| Error reason ->
6767
Printf.printf "%s: FAIL, %s\n" filename reason;
@@ -112,7 +112,7 @@ let rec process path ~extra_checks =
112112
path
113113
~extra_checks
114114

115-
let process ?(extra_checks = fun _ -> Ok ()) text = process text ~extra_checks
115+
let process ?(extra_checks = fun _ _ -> Ok ()) text = process text ~extra_checks
116116
117117
(* Produce an error if any attribute/extension node does not start with the
118118
text prefix.
@@ -128,7 +128,7 @@ let process ?(extra_checks = fun _ -> Ok ()) text = process text ~extra_checks
128128
We've chosen to keep those constructs out of the test file in preference
129129
to updating this logic to properly handle them (which is hard).
130130
*)
131-
let check_all_attributes_and_extensions_start_with text ~prefix =
131+
let check_all_printed_attributes_and_extensions_start_with text ~prefix =
132132
let check introduction_string =
133133
String.split_on_char '[' text
134134
|> List.for_all (fun s ->
@@ -146,14 +146,46 @@ let check_all_attributes_and_extensions_start_with text ~prefix =
146146
prefix)
147147
;;
148148
149+
let check_all_ast_attributes_and_extensions_start_with raw_parsetree_str ~prefixes =
150+
(* Sadly can't use Ast_mapper here because it decodes Jane Syntax by default and
151+
we will need quite a bit of code duplication for it to work for this use case. *)
152+
let check introduction_string =
153+
Misc.Stdlib.String.split_on_string ~split_on:(introduction_string ^ " \"")
154+
raw_parsetree_str
155+
|> List.tl
156+
|> List.for_all (fun s ->
157+
List.exists
158+
(fun prefix -> String.starts_with s ~prefix)
159+
prefixes)
160+
in
161+
if check "extension" && check "attribute"
162+
then Ok ()
163+
else
164+
Error
165+
(Printf.sprintf
166+
"Printast produced an extension node or attribute that doesn't \
167+
begin with one of [%s]"
168+
(String.concat ", " prefixes))
169+
;;
170+
149171
let () =
150172
process "source.ml";
151173
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
174+
process "source_jane_street.ml" ~extra_checks:(fun raw_parsetree_str text ->
175+
(* Additionally check that:
176+
177+
1. Jane Street language extensions only use "extension." and "jane." prefixed
178+
attributes and exntensions for its parsetree encoding. This is important for
179+
ppx support.
180+
181+
2. Printing Jane Street language extensions produces no more
154182
attributes or extension nodes than the input program, all of whose
155183
attributes begin with "test". This ensures that Jane Syntax attributes
156184
aren't printed.
157185
*)
158-
check_all_attributes_and_extensions_start_with text ~prefix:"test");
186+
Result.bind
187+
(check_all_ast_attributes_and_extensions_start_with raw_parsetree_str
188+
~prefixes:["extension."; "jane."; "test."])
189+
(fun () -> check_all_printed_attributes_and_extensions_start_with text
190+
~prefix:"test"));
159191
;;

0 commit comments

Comments
 (0)