Skip to content

Commit 0d82a3e

Browse files
committed
Adapt ppx ast conversion that was still missing.
1 parent e356454 commit 0d82a3e

File tree

3 files changed

+85
-38
lines changed

3 files changed

+85
-38
lines changed

compiler/ml/ast_mapper_from0.ml

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -319,16 +319,11 @@ module E = struct
319319
| [] -> assert false
320320
in
321321
match arg1 with
322-
| Some ({pexp_desc = Pexp_fun (l, eo, p, e, _)} as e1) ->
322+
| Some
323+
({pexp_desc = Pexp_fun (l, eo, p, e, _); pexp_attributes = attrs} as
324+
e1) ->
323325
let arity = attributes_to_arity attrs in
324-
{
325-
e1 with
326-
pexp_desc =
327-
Pexp_construct
328-
( lid1,
329-
Some {e with pexp_desc = Pexp_fun (l, eo, p, e, Some arity)}
330-
);
331-
}
326+
{e1 with pexp_desc = Pexp_fun (l, eo, p, e, Some arity)}
332327
| _ -> exp1)
333328
| _ -> exp1)
334329
| Pexp_variant (lab, eo) ->

compiler/ml/ast_mapper_to0.ml

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -283,41 +283,43 @@ module E = struct
283283
| Pexp_constant x -> constant ~loc ~attrs (map_constant x)
284284
| Pexp_let (r, vbs, e) ->
285285
let_ ~loc ~attrs r (List.map (sub.value_binding sub) vbs) (sub.expr sub e)
286-
| Pexp_fun (lab, def, p, e, _) ->
287-
fun_ ~loc ~attrs lab
288-
(map_opt (sub.expr sub) def)
289-
(sub.pat sub p) (sub.expr sub e)
286+
| Pexp_fun (lab, def, p, e, arity) -> (
287+
let e =
288+
fun_ ~loc ~attrs lab
289+
(map_opt (sub.expr sub) def)
290+
(sub.pat sub p) (sub.expr sub e)
291+
in
292+
match arity with
293+
| None -> e
294+
| Some arity ->
295+
let arity_to_attributes arity =
296+
[
297+
( Location.mknoloc "res.arity",
298+
Parsetree0.PStr
299+
[
300+
Ast_helper0.Str.eval
301+
(Ast_helper0.Exp.constant
302+
(Pconst_integer (string_of_int arity, None)));
303+
] );
304+
]
305+
in
306+
let e =
307+
{
308+
e with
309+
pexp_attributes = arity_to_attributes arity @ e.pexp_attributes;
310+
}
311+
in
312+
Ast_helper0.Exp.construct
313+
(Location.mkloc (Longident.Lident "Function$") e.pexp_loc)
314+
(Some e))
290315
| Pexp_apply (e, l) ->
291316
apply ~loc ~attrs (sub.expr sub e) (List.map (map_snd (sub.expr sub)) l)
292317
| Pexp_match (e, pel) ->
293318
match_ ~loc ~attrs (sub.expr sub e) (sub.cases sub pel)
294319
| Pexp_try (e, pel) -> try_ ~loc ~attrs (sub.expr sub e) (sub.cases sub pel)
295320
| Pexp_tuple el -> tuple ~loc ~attrs (List.map (sub.expr sub) el)
296-
| Pexp_construct (lid, arg) -> (
297-
let exp0 =
298-
construct ~loc ~attrs (map_loc sub lid) (map_opt (sub.expr sub) arg)
299-
in
300-
match lid.txt with
301-
| Lident "Function$" -> (
302-
match arg with
303-
| Some {pexp_desc = Pexp_fun (_, _, _, _, Some arity)} ->
304-
let arity_to_attributes arity =
305-
[
306-
( Location.mknoloc "res.arity",
307-
Parsetree0.PStr
308-
[
309-
Ast_helper0.Str.eval
310-
(Ast_helper0.Exp.constant
311-
(Pconst_integer (string_of_int arity, None)));
312-
] );
313-
]
314-
in
315-
{
316-
exp0 with
317-
pexp_attributes = arity_to_attributes arity @ exp0.pexp_attributes;
318-
}
319-
| _ -> assert false)
320-
| _ -> exp0)
321+
| Pexp_construct (lid, arg) ->
322+
construct ~loc ~attrs (map_loc sub lid) (map_opt (sub.expr sub) arg)
321323
| Pexp_variant (lab, eo) ->
322324
variant ~loc ~attrs lab (map_opt (sub.expr sub) eo)
323325
| Pexp_record (l, eo) ->
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Generated by ReScript, PLEASE EDIT WITH CARE
2+
'use strict';
3+
4+
let React = require("react");
5+
6+
console.log("ppx test");
7+
8+
console.log("ppx test");
9+
10+
let M = {
11+
v: 10
12+
};
13+
14+
let OptionalFields = {
15+
r: {
16+
y: 1.0
17+
}
18+
};
19+
20+
function one(x) {
21+
return x;
22+
}
23+
24+
function two(x, y) {
25+
return x + y | 0;
26+
}
27+
28+
let n = 6;
29+
30+
let Arity = {
31+
one: one,
32+
two: two,
33+
n: n
34+
};
35+
36+
React.useState(() => 0);
37+
38+
let a = "A";
39+
40+
let b = "B";
41+
42+
let vv = 10;
43+
44+
exports.a = a;
45+
exports.b = b;
46+
exports.M = M;
47+
exports.vv = vv;
48+
exports.OptionalFields = OptionalFields;
49+
exports.Arity = Arity;
50+
/* Not a pure module */

0 commit comments

Comments
 (0)