Skip to content
This repository was archived by the owner on Jun 15, 2023. It is now read-only.

Commit a9ff68c

Browse files
author
Iwan
committed
Parse arity1 as f(. ignore())
The use of `ignore()` prevents possible naming conflicts with the user putting magic binding names in scope.
2 parents d73bab9 + bb822fd commit a9ff68c

File tree

5 files changed

+24
-11
lines changed

5 files changed

+24
-11
lines changed

src/res_ast_conversion.ml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,8 @@ let normalize =
413413
]
414414
) when hasUncurriedAttribute expr.pexp_attributes
415415
->
416+
(* In Reason both f(.) and f(. ()) express arity0. In ReScript only f(.)
417+
* expresses arity0. We tweak the Reason version to always match the f(.) on the Reason side. *)
416418
{expr with
417419
pexp_attributes = mapper.attributes mapper expr.pexp_attributes;
418420
pexp_desc = Pexp_apply (

src/res_core.ml

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3423,12 +3423,12 @@ and parseCallExpr p funExpr =
34233423
pexp_desc = Pexp_construct ({txt = Longident.Lident "()"}, None);
34243424
pexp_loc = loc;
34253425
pexp_attributes = []
3426-
} as expr)
3426+
} as unitExpr)
34273427
] when (not loc.loc_ghost) && p.mode = ParseForTypeChecker ->
34283428
(* Since there is no syntax space for arity zero vs arity one,
34293429
* we expand
34303430
* `fn(. ())` into
3431-
* `fn(. {let __res_unit = (); __res_unit})`
3431+
* `fn(. ignore())`
34323432
* when the parsetree is intended for type checking
34333433
*
34343434
* Note:
@@ -3440,12 +3440,12 @@ and parseCallExpr p funExpr =
34403440
[
34413441
true,
34423442
Asttypes.Nolabel,
3443-
Ast_helper.Exp.let_
3444-
Asttypes.Nonrecursive
3445-
[Ast_helper.Vb.mk
3446-
(Ast_helper.Pat.var (Location.mknoloc "__res_unit"))
3447-
expr]
3448-
(Ast_helper.Exp.ident (Location.mknoloc (Longident.Lident "__res_unit")))
3443+
Ast_helper.Exp.apply
3444+
(Ast_helper.Exp.ident (Location.mknoloc (Longident.Lident "ignore")))
3445+
[
3446+
Asttypes.Nolabel,
3447+
unitExpr
3448+
]
34493449
]
34503450
| args -> args
34513451
in

tests/conversion/reason/__snapshots__/render.spec.js.snap

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1599,6 +1599,12 @@ let _ =
15991599
),
16001600
)
16011601
)
1602+
1603+
let () = {
1604+
resolve(.)
1605+
resolve(.)
1606+
resolve(. ignore())
1607+
}
16021608
"
16031609
`;
16041610

tests/conversion/reason/uncurrried.re

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,9 @@ let _ =
7575
),
7676
)
7777
});
78+
79+
let () = {
80+
resolve(.)
81+
resolve(. ())
82+
resolve(. ignore())
83+
}

tests/parsing/grammar/expressions/__snapshots__/parse.spec.js.snap

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@ let unitUncurried = ((apply ())[@bs ])
1212
`;
1313

1414
exports[`argument.js 1`] = `
15-
"let foo ~a:((a)[@ns.namedArgLoc ]) =
16-
((a (let __res_unit = () in __res_unit))[@bs ]) +. 1.
15+
"let foo ~a:((a)[@ns.namedArgLoc ]) = ((a (ignore ()))[@bs ]) +. 1.
1716
let a = ((fun () -> 2)[@bs ])
1817
let bar = foo ~a:((a)[@ns.namedArgLoc ])
1918
let comparisonResult =
@@ -24,7 +23,7 @@ let comparisonResult =
2423
(elementProps ~onClick:((fun _ -> Js.log \\"hello world\\")
2524
[@ns.namedArgLoc ])))[@bs ])
2625
;;((resolve ())[@bs ])
27-
;;((resolve (let __res_unit = () in __res_unit))[@bs ])"
26+
;;((resolve (ignore ()))[@bs ])"
2827
`;
2928

3029
exports[`array.js 1`] = `

0 commit comments

Comments
 (0)