Skip to content

Commit fb56287

Browse files
authored
flambda-backend: Fix ast iteration/mapping for layout type declarations (#2145)
* Demonstrate bug in using ppxes with certain jane syntax elements * Fix bug
1 parent 034e085 commit fb56287

File tree

7 files changed

+83
-13
lines changed

7 files changed

+83
-13
lines changed

parsing/ast_iterator.ml

+9-2
Original file line numberDiff line numberDiff line change
@@ -178,12 +178,19 @@ module T = struct
178178
| Ptyp_extension x -> sub.extension sub x
179179

180180
let iter_type_declaration sub
181-
{ptype_name; ptype_params; ptype_cstrs;
181+
({ptype_name; ptype_params; ptype_cstrs;
182182
ptype_kind;
183183
ptype_private = _;
184184
ptype_manifest;
185185
ptype_attributes;
186-
ptype_loc} =
186+
ptype_loc} as ty_decl) =
187+
let ptype_attributes =
188+
match Jane_syntax.Layouts.of_type_declaration ty_decl with
189+
| Some (jkind, attrs) ->
190+
iter_loc_txt sub sub.jkind_annotation jkind;
191+
attrs
192+
| None -> ptype_attributes
193+
in
187194
iter_loc sub ptype_name;
188195
List.iter (iter_fst (sub.typ sub)) ptype_params;
189196
List.iter

parsing/ast_mapper.ml

+14-4
Original file line numberDiff line numberDiff line change
@@ -217,22 +217,32 @@ module T = struct
217217
| Ptyp_extension x -> extension ~loc ~attrs (sub.extension sub x)
218218

219219
let map_type_declaration sub
220-
{ptype_name; ptype_params; ptype_cstrs;
220+
({ptype_name; ptype_params; ptype_cstrs;
221221
ptype_kind;
222222
ptype_private;
223223
ptype_manifest;
224224
ptype_attributes;
225-
ptype_loc} =
225+
ptype_loc} as tyd) =
226226
let loc = sub.location sub ptype_loc in
227+
let jkind, ptype_attributes =
228+
match Jane_syntax.Layouts.of_type_declaration tyd with
229+
| None -> None, ptype_attributes
230+
| Some (jkind, attributes) ->
231+
let jkind = map_loc_txt sub sub.jkind_annotation jkind in
232+
Some jkind, attributes
233+
in
227234
let attrs = sub.attributes sub ptype_attributes in
228-
Type.mk ~loc ~attrs (map_loc sub ptype_name)
235+
Jane_syntax.Layouts.type_declaration_of ~loc ~attrs (map_loc sub ptype_name)
229236
~params:(List.map (map_fst (sub.typ sub)) ptype_params)
230237
~priv:ptype_private
231238
~cstrs:(List.map
232239
(map_tuple3 (sub.typ sub) (sub.typ sub) (sub.location sub))
233240
ptype_cstrs)
234241
~kind:(sub.type_kind sub ptype_kind)
235-
?manifest:(map_opt (sub.typ sub) ptype_manifest)
242+
~manifest:(map_opt (sub.typ sub) ptype_manifest)
243+
~jkind
244+
~docs:Docstrings.empty_docs
245+
~text:None
236246

237247
let map_type_kind sub = function
238248
| Ptype_abstract -> Ptype_abstract

parsing/pprintast.ml

+10-2
Original file line numberDiff line numberDiff line change
@@ -1742,10 +1742,18 @@ and type_def_list ctxt f (rf, exported, l) =
17421742
else if exported then " ="
17431743
else " :="
17441744
in
1745-
pp f "@[<2>%s %a%a%s%s%a@]%a" kwd
1745+
let layout_annot, x =
1746+
match Jane_syntax.Layouts.of_type_declaration x with
1747+
| None -> "", x
1748+
| Some (jkind, remaining_attributes) ->
1749+
Printf.sprintf " : %s"
1750+
(Jane_asttypes.jkind_to_string jkind.txt),
1751+
{ x with ptype_attributes = remaining_attributes }
1752+
in
1753+
pp f "@[<2>%s %a%a%s%s%s%a@]%a" kwd
17461754
nonrec_flag rf
17471755
(type_params ctxt) x.ptype_params
1748-
x.ptype_name.txt eq
1756+
x.ptype_name.txt layout_annot eq
17491757
(type_declaration ctxt) x
17501758
(item_attributes ctxt) x.ptype_attributes
17511759
in
+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
open Ast_mapper
2+
3+
(* This PPX rewriter does nothing. *)
4+
5+
let () =
6+
Language_extension.enable_maximal ();
7+
Ast_mapper.register "no-op" (fun _ -> Ast_mapper.default_mapper);
8+
;;

testsuite/tests/parsetree/source_jane_street.ml

+23-5
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,14 @@ let f (type a : immediate) (type b : immediate)
2323
(type (c : immediate) (d : immediate))
2424
= ();;
2525

26+
module type S_for_layouts = sig
27+
type t : float64
28+
29+
type variant = A : ('a : immediate). 'a -> variant
30+
end;;
31+
32+
type ('a : immediate) for_layouts = 'a;;
33+
2634
(******************)
2735
(* Comprehensions *)
2836

@@ -61,7 +69,7 @@ let f (type a : immediate) (type b : immediate)
6169
(* Local *)
6270

6371
(* parameters *)
64-
let f (local_ x) ~(local_ y) ~z:(local_ z) ?foo:(local_ w = 1) = x + y + z + w;;
72+
let f (local_ x) ~(local_ y) ~z:(local_ z) ?foo:(local_ w = 1) () = x + y + z + w;;
6573

6674
(* bindings *)
6775
let g () =
@@ -94,17 +102,22 @@ type 'a parameterized_record = {
94102
type fn = local_ int -> local_ int;;
95103
type nested_fn = (local_ int -> local_ int) -> local_ int;;
96104
type ('a, 'b) labeled_fn =
97-
a:local_ 'a -> ?b:local_ b -> local_ 'a -> (int -> local_ 'b);;
105+
a:local_ 'a -> ?b:local_ 'b -> local_ 'a -> (int -> local_ 'b);;
98106

99107
(*******************)
100108
(* Include functor *)
101109

110+
module F_struct (_ : sig end) = struct
111+
end
112+
113+
module type F_sig = functor (_ : sig end) -> sig end
114+
102115
module T = struct
103-
include functor F
116+
include functor F_struct
104117
end;;
105118

106119
module type S = sig
107-
include functor F
120+
include functor F_sig
108121
end;;
109122

110123
(********************)
@@ -115,10 +128,15 @@ let f x =
115128
| [::] -> [::]
116129
| ([:x:] [@test.attr1]) -> (([:x:])[@test.attr1])
117130
| ([:x;y:] [@test.attr2][@test.attr3]) ->
118-
([:x;y:] [@test.attr2][@test.attr3]);;
131+
([:x;y:] [@test.attr2][@test.attr3])
132+
| _ -> assert false;;
119133

120134
(******************)
121135
(* Labeled tuples *)
136+
let z, punned = 4, 5
137+
let x_must_be_even _ = assert false
138+
exception Odd
139+
122140
let x = (~x:1, ~y:2)
123141
let x = ((~x:1, ~y:2) [@test.attr])
124142
let _ = ( ~x: 5, 2, ~z, ~(punned:int))

testsuite/tests/parsetree/test_ppx.compilers.reference

Whitespace-only changes.

testsuite/tests/parsetree/test_ppx.ml

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
(* TEST
2+
readonly_files = "source_jane_street.ml ppx_no_op.ml"
3+
include ocamlcommon
4+
* setup-ocamlc.byte-build-env
5+
** ocamlc.byte
6+
program = "${test_build_directory}/ppx_no_op.exe"
7+
all_modules = "ppx_no_op.ml"
8+
*** ocamlc.byte
9+
module = "source_jane_street.ml"
10+
flags = "-I ${test_build_directory} \
11+
-w -26 \
12+
-extension layouts \
13+
-extension comprehensions \
14+
-ppx ${program}"
15+
**** check-ocamlc.byte-output
16+
*)
17+
18+
(* This test ensures that Jane Street syntax continues to be
19+
handled properly by the compiler even after applying a PPX rewriter. *)

0 commit comments

Comments
 (0)