Skip to content

Commit ba77b7f

Browse files
committed
Add partial flag to untyped and typed AST node and prepare to remove @res.partial.
1 parent f895935 commit ba77b7f

23 files changed

+80
-87
lines changed

analysis/src/CompletionFrontEnd.ml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -262,12 +262,14 @@ let rec exprToContextPathInner (e : Parsetree.expression) =
262262
pexp_loc;
263263
pexp_attributes;
264264
};
265-
args = [(_, lhs); (_, {pexp_desc = Pexp_apply {funct = d; args}})];
265+
args =
266+
[(_, lhs); (_, {pexp_desc = Pexp_apply {funct = d; args; partial}})];
266267
} ->
267268
(* Transform away pipe with apply call *)
268269
exprToContextPath
269270
{
270-
pexp_desc = Pexp_apply {funct = d; args = (Nolabel, lhs) :: args};
271+
pexp_desc =
272+
Pexp_apply {funct = d; args = (Nolabel, lhs) :: args; partial};
271273
pexp_loc;
272274
pexp_attributes;
273275
}
@@ -278,6 +280,7 @@ let rec exprToContextPathInner (e : Parsetree.expression) =
278280
[
279281
(_, lhs); (_, {pexp_desc = Pexp_ident id; pexp_loc; pexp_attributes});
280282
];
283+
partial;
281284
} ->
282285
(* Transform away pipe with identifier *)
283286
exprToContextPath
@@ -287,6 +290,7 @@ let rec exprToContextPathInner (e : Parsetree.expression) =
287290
{
288291
funct = {pexp_desc = Pexp_ident id; pexp_loc; pexp_attributes};
289292
args = [(Nolabel, lhs)];
293+
partial;
290294
};
291295
pexp_loc;
292296
pexp_attributes;

compiler/frontend/ast_compatible.ml

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,22 +40,28 @@ let apply_simple ?(loc = default_loc) ?(attrs = []) (fn : expression)
4040
pexp_attributes = attrs;
4141
pexp_desc =
4242
Pexp_apply
43-
{funct = fn; args = Ext_list.map args (fun x -> (Asttypes.Nolabel, x))};
43+
{
44+
funct = fn;
45+
args = Ext_list.map args (fun x -> (Asttypes.Nolabel, x));
46+
partial = false;
47+
};
4448
}
4549

4650
let app1 ?(loc = default_loc) ?(attrs = []) fn arg1 : expression =
4751
{
4852
pexp_loc = loc;
4953
pexp_attributes = attrs;
50-
pexp_desc = Pexp_apply {funct = fn; args = [(Nolabel, arg1)]};
54+
pexp_desc =
55+
Pexp_apply {funct = fn; args = [(Nolabel, arg1)]; partial = false};
5156
}
5257

5358
let app2 ?(loc = default_loc) ?(attrs = []) fn arg1 arg2 : expression =
5459
{
5560
pexp_loc = loc;
5661
pexp_attributes = attrs;
5762
pexp_desc =
58-
Pexp_apply {funct = fn; args = [(Nolabel, arg1); (Nolabel, arg2)]};
63+
Pexp_apply
64+
{funct = fn; args = [(Nolabel, arg1); (Nolabel, arg2)]; partial = false};
5965
}
6066

6167
let app3 ?(loc = default_loc) ?(attrs = []) fn arg1 arg2 arg3 : expression =
@@ -64,7 +70,11 @@ let app3 ?(loc = default_loc) ?(attrs = []) fn arg1 arg2 arg3 : expression =
6470
pexp_attributes = attrs;
6571
pexp_desc =
6672
Pexp_apply
67-
{funct = fn; args = [(Nolabel, arg1); (Nolabel, arg2); (Nolabel, arg3)]};
73+
{
74+
funct = fn;
75+
args = [(Nolabel, arg1); (Nolabel, arg2); (Nolabel, arg3)];
76+
partial = false;
77+
};
6878
}
6979

7080
let fun_ ?(loc = default_loc) ?(attrs = []) ?(async = false) ~arity pat exp =
@@ -108,6 +118,7 @@ let apply_labels ?(loc = default_loc) ?(attrs = []) fn
108118
{
109119
funct = fn;
110120
args = Ext_list.map args (fun (l, a) -> (Asttypes.Labelled l, a));
121+
partial = false;
111122
};
112123
}
113124

compiler/frontend/ast_exp_apply.ml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,11 @@ let app_exp_mapper (e : exp) (self : Bs_ast_mapper.mapper) : exp =
8888
{f with pexp_desc = Pexp_variant (label, Some a); pexp_loc = e.pexp_loc}
8989
| Pexp_construct (ctor, None) ->
9090
{f with pexp_desc = Pexp_construct (ctor, Some a); pexp_loc = e.pexp_loc}
91-
| Pexp_apply {funct = fn1; args} ->
91+
| Pexp_apply {funct = fn1; args; partial} ->
9292
Bs_ast_invariant.warn_discarded_unused_attributes fn1.pexp_attributes;
9393
{
94-
pexp_desc = Pexp_apply {funct = fn1; args = (Nolabel, a) :: args};
94+
pexp_desc =
95+
Pexp_apply {funct = fn1; args = (Nolabel, a) :: args; partial};
9596
pexp_loc = e.pexp_loc;
9697
pexp_attributes = e.pexp_attributes @ f.pexp_attributes;
9798
}
@@ -116,6 +117,7 @@ let app_exp_mapper (e : exp) (self : Bs_ast_mapper.mapper) : exp =
116117
{
117118
funct = fn;
118119
args = (Nolabel, bounded_obj_arg) :: args;
120+
partial = false;
119121
};
120122
pexp_attributes = [];
121123
pexp_loc = fn.pexp_loc;

compiler/frontend/ast_uncurry_gen.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,5 @@ let to_method_callback loc (self : Bs_ast_mapper.mapper) label
7474
}
7575
[Typ.any ~loc ()]) );
7676
];
77+
partial = false;
7778
}

compiler/frontend/bs_ast_mapper.ml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -320,8 +320,9 @@ module E = struct
320320
fun_ ~loc ~attrs ~arity ~async lab
321321
(map_opt (sub.expr sub) def)
322322
(sub.pat sub p) (sub.expr sub e)
323-
| Pexp_apply {funct = e; args = l} ->
324-
apply ~loc ~attrs (sub.expr sub e) (List.map (map_snd (sub.expr sub)) l)
323+
| Pexp_apply {funct = e; args = l; partial} ->
324+
apply ~loc ~attrs ~partial (sub.expr sub e)
325+
(List.map (map_snd (sub.expr sub)) l)
325326
| Pexp_match (e, pel) ->
326327
match_ ~loc ~attrs (sub.expr sub e) (sub.cases sub pel)
327328
| Pexp_try (e, pel) -> try_ ~loc ~attrs (sub.expr sub e) (sub.cases sub pel)

compiler/ml/ast_helper.ml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,8 @@ module Exp = struct
154154
let fun_ ?loc ?attrs ?(async = false) ~arity a b c d =
155155
mk ?loc ?attrs
156156
(Pexp_fun {arg_label = a; default = b; lhs = c; rhs = d; arity; async})
157-
let apply ?loc ?attrs funct args = mk ?loc ?attrs (Pexp_apply {funct; args})
157+
let apply ?loc ?attrs ?(partial = false) funct args =
158+
mk ?loc ?attrs (Pexp_apply {funct; args; partial})
158159
let match_ ?loc ?attrs a b = mk ?loc ?attrs (Pexp_match (a, b))
159160
let try_ ?loc ?attrs a b = mk ?loc ?attrs (Pexp_try (a, b))
160161
let tuple ?loc ?attrs a = mk ?loc ?attrs (Pexp_tuple a)

compiler/ml/ast_helper.mli

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ module Exp : sig
148148
val apply :
149149
?loc:loc ->
150150
?attrs:attrs ->
151+
?partial:bool ->
151152
expression ->
152153
(arg_label * expression) list ->
153154
expression

compiler/ml/ast_mapper.ml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -283,8 +283,9 @@ module E = struct
283283
fun_ ~loc ~attrs ~arity ~async lab
284284
(map_opt (sub.expr sub) def)
285285
(sub.pat sub p) (sub.expr sub e)
286-
| Pexp_apply {funct = e; args = l} ->
287-
apply ~loc ~attrs (sub.expr sub e) (List.map (map_snd (sub.expr sub)) l)
286+
| Pexp_apply {funct = e; args = l; partial} ->
287+
apply ~loc ~attrs ~partial (sub.expr sub e)
288+
(List.map (map_snd (sub.expr sub)) l)
288289
| Pexp_match (e, pel) ->
289290
match_ ~loc ~attrs (sub.expr sub e) (sub.cases sub pel)
290291
| Pexp_try (e, pel) -> try_ ~loc ~attrs (sub.expr sub e) (sub.cases sub pel)

compiler/ml/parsetree.ml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,11 @@ and expression_desc =
242242
- "fun P1 P2 .. Pn -> E1" is represented as nested Pexp_fun.
243243
- "let f P = E" is represented using Pexp_fun.
244244
*)
245-
| Pexp_apply of {funct: expression; args: (arg_label * expression) list}
245+
| Pexp_apply of {
246+
funct: expression;
247+
args: (arg_label * expression) list;
248+
partial: bool;
249+
}
246250
(* E0 ~l1:E1 ... ~ln:En
247251
li can be empty (non labeled argument) or start with '?'
248252
(optional argument).

compiler/ml/pprintast.ml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -631,7 +631,7 @@ and expression ctxt f x =
631631
(* rec_flag rf *)
632632
pp f "@[<2>%a in@;<1 -2>%a@]" (bindings reset_ctxt) (rf, l)
633633
(expression ctxt) e
634-
| Pexp_apply {funct = e; args = l} -> (
634+
| Pexp_apply {funct = e; args = l; partial} -> (
635635
if not (sugar_expr ctxt f x) then
636636
match view_fixity_of_exp e with
637637
| `Infix s -> (
@@ -667,14 +667,15 @@ and expression ctxt f x =
667667
(list (label_x_expression_param ctxt))
668668
l)
669669
| _ ->
670-
pp f "@[<hov2>%a@]"
670+
let partial_str = if partial then " ..." else "" in
671+
pp f "@[<hov2>%a%s@]"
671672
(fun f (e, l) ->
672673
pp f "%a@ %a" (expression2 ctxt) e
673674
(list (label_x_expression_param reset_ctxt))
674675
l)
675676
(* reset here only because [function,match,try,sequence]
676677
are lower priority *)
677-
(e, l))
678+
(e, l) partial_str)
678679
| Pexp_construct (li, Some eo) when not (is_simple_construct (view_expr x))
679680
-> (
680681
(* Not efficient FIXME*)

compiler/ml/printast.ml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,8 +250,9 @@ and expression i ppf x =
250250
option i expression ppf eo;
251251
pattern i ppf p;
252252
expression i ppf e
253-
| Pexp_apply {funct = e; args = l} ->
253+
| Pexp_apply {funct = e; args = l; partial} ->
254254
line i ppf "Pexp_apply\n";
255+
if partial then line i ppf "partial\n";
255256
expression i ppf e;
256257
list i label_x_expression ppf l
257258
| Pexp_match (e, l) ->

compiler/ml/printtyped.ml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,8 @@ and expression i ppf x =
295295
line i ppf "%a" Ident.print param;
296296
arg_label i ppf p;
297297
case i ppf case_
298-
| Texp_apply {funct = e; args = l} ->
298+
| Texp_apply {funct = e; args = l; partial} ->
299+
if partial then line i ppf "partial\n";
299300
line i ppf "Texp_apply\n";
300301
expression i ppf e;
301302
list i label_x_expression ppf l

compiler/ml/tast_mapper.ml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,11 +202,12 @@ let expr sub x =
202202
| Texp_function {arg_label; arity; param; case; partial; async} ->
203203
Texp_function
204204
{arg_label; arity; param; case = sub.case sub case; partial; async}
205-
| Texp_apply {funct = exp; args = list} ->
205+
| Texp_apply {funct = exp; args = list; partial} ->
206206
Texp_apply
207207
{
208208
funct = sub.expr sub exp;
209209
args = List.map (tuple2 id (opt (sub.expr sub))) list;
210+
partial;
210211
}
211212
| Texp_match (exp, cases, exn_cases, p) ->
212213
Texp_match

compiler/ml/translcore.ml

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -760,17 +760,14 @@ and transl_exp0 (e : Typedtree.expression) : Lambda.lambda =
760760
| Plazyforce, [a] -> wrap (Matching.inline_lazy_force a e.exp_loc)
761761
| Plazyforce, _ -> assert false
762762
| _ -> wrap (Lprim (prim, argl, e.exp_loc))))
763-
| Texp_apply {funct; args = oargs} ->
763+
| Texp_apply {funct; args = oargs; partial} ->
764764
let inlined, funct =
765765
Translattribute.get_and_remove_inlined_attribute funct
766766
in
767767
let uncurried_partial_application =
768768
(* In case of partial application foo(args, ...) when some args are missing,
769769
get the arity *)
770-
let uncurried_partial_app =
771-
Ext_list.exists e.exp_attributes (fun ({txt}, _) -> txt = "res.partial")
772-
in
773-
if uncurried_partial_app then
770+
if partial then
774771
let arity_opt = Ctype.get_arity funct.exp_env funct.exp_type in
775772
match arity_opt with
776773
| Some arity ->

compiler/ml/typecore.ml

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2420,7 +2420,7 @@ and type_expect_ ?type_clash_context ?in_function ?(recarg = Rejected) env sexp
24202420
type_function ?in_function ~arity ~async loc sexp.pexp_attributes env
24212421
ty_expected l
24222422
[Ast_helper.Exp.case spat sbody]
2423-
| Pexp_apply {funct = sfunct; args = sargs} ->
2423+
| Pexp_apply {funct = sfunct; args = sargs; partial} ->
24242424
assert (sargs <> []);
24252425
begin_def ();
24262426
(* one more level for non-returning functions *)
@@ -2429,11 +2429,7 @@ and type_expect_ ?type_clash_context ?in_function ?(recarg = Rejected) env sexp
24292429
end_def ();
24302430
wrap_trace_gadt_instances env (lower_args env []) ty;
24312431
begin_def ();
2432-
let total_app =
2433-
not
2434-
@@ Ext_list.exists sexp.pexp_attributes (fun ({txt}, _) ->
2435-
txt = "res.partial")
2436-
in
2432+
let total_app = not partial in
24372433
let type_clash_context = type_clash_context_from_function sexp sfunct in
24382434
let args, ty_res, fully_applied =
24392435
match translate_unified_ops env funct sargs with
@@ -2446,7 +2442,7 @@ and type_expect_ ?type_clash_context ?in_function ?(recarg = Rejected) env sexp
24462442
let mk_apply funct args =
24472443
rue
24482444
{
2449-
exp_desc = Texp_apply {funct; args};
2445+
exp_desc = Texp_apply {funct; args; partial};
24502446
exp_loc = loc;
24512447
exp_extra = [];
24522448
exp_type = ty_res;

compiler/ml/typedtree.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ and expression_desc =
8787
| Texp_apply of {
8888
funct: expression;
8989
args: (arg_label * expression option) list;
90+
partial: bool;
9091
}
9192
| Texp_match of expression * case list * case list * partial
9293
| Texp_try of expression * case list

compiler/ml/typedtree.mli

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ and expression_desc =
151151
| Texp_apply of {
152152
funct: expression;
153153
args: (arg_label * expression option) list;
154+
partial: bool;
154155
}
155156
(** E0 ~l1:E1 ... ~ln:En
156157

compiler/syntax/src/res_core.ml

Lines changed: 10 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -2221,11 +2221,12 @@ and parse_binary_expr ?(context = OrdinaryExpr) ?a p prec =
22212221
let loc = mk_loc a.Parsetree.pexp_loc.loc_start b.pexp_loc.loc_end in
22222222
let expr =
22232223
match (token, b.pexp_desc) with
2224-
| BarGreater, Pexp_apply {funct = fun_expr; args} ->
2224+
| BarGreater, Pexp_apply {funct = fun_expr; args; partial} ->
22252225
{
22262226
b with
22272227
pexp_desc =
2228-
Pexp_apply {funct = fun_expr; args = args @ [(Nolabel, a)]};
2228+
Pexp_apply
2229+
{funct = fun_expr; args = args @ [(Nolabel, a)]; partial};
22292230
}
22302231
| BarGreater, _ -> Ast_helper.Exp.apply ~loc b [(Nolabel, a)]
22312232
| _ ->
@@ -3682,11 +3683,7 @@ and parse_call_expr p fun_expr =
36823683
parse_comma_delimited_region ~grammar:Grammar.ArgumentList ~closing:Rparen
36833684
~f:parse_argument p
36843685
in
3685-
let res_partial_attr =
3686-
let loc = mk_loc start_pos p.prev_end_pos in
3687-
(Location.mkloc "res.partial" loc, Parsetree.PStr [])
3688-
in
3689-
let is_partial =
3686+
let partial =
36903687
match p.token with
36913688
| DotDotDot when args <> [] ->
36923689
Parser.next p;
@@ -3708,45 +3705,6 @@ and parse_call_expr p fun_expr =
37083705
None;
37093706
};
37103707
]
3711-
| [
3712-
{
3713-
label = Nolabel;
3714-
expr =
3715-
{
3716-
pexp_desc = Pexp_construct ({txt = Longident.Lident "()"}, None);
3717-
pexp_loc = loc;
3718-
pexp_attributes = [];
3719-
} as expr;
3720-
};
3721-
]
3722-
when (not loc.loc_ghost) && p.mode = ParseForTypeChecker && not is_partial
3723-
->
3724-
(* Since there is no syntax space for arity zero vs arity one,
3725-
* we expand
3726-
* `fn(. ())` into
3727-
* `fn(. {let __res_unit = (); __res_unit})`
3728-
* when the parsetree is intended for type checking
3729-
*
3730-
* Note:
3731-
* `fn(.)` is treated as zero arity application.
3732-
* The invisible unit expression here has loc_ghost === true
3733-
*
3734-
* Related: https://github.com/rescript-lang/syntax/issues/138
3735-
*)
3736-
[
3737-
{
3738-
label = Nolabel;
3739-
expr =
3740-
Ast_helper.Exp.let_ Asttypes.Nonrecursive
3741-
[
3742-
Ast_helper.Vb.mk
3743-
(Ast_helper.Pat.var (Location.mknoloc "__res_unit"))
3744-
expr;
3745-
]
3746-
(Ast_helper.Exp.ident
3747-
(Location.mknoloc (Longident.Lident "__res_unit")));
3748-
};
3749-
]
37503708
| args -> args
37513709
in
37523710
let loc = {fun_expr.pexp_loc with loc_end = p.prev_end_pos} in
@@ -3761,9 +3719,13 @@ and parse_call_expr p fun_expr =
37613719
let apply =
37623720
Ext_list.fold_left args fun_expr (fun call_body args ->
37633721
let args, wrap = process_underscore_application args in
3722+
let res_partial_attr =
3723+
let loc = mk_loc start_pos p.prev_end_pos in
3724+
(Location.mkloc "res.partial" loc, Parsetree.PStr [])
3725+
in
37643726
let exp =
3765-
let attrs = if is_partial then [res_partial_attr] else [] in
3766-
Ast_helper.Exp.apply ~loc ~attrs call_body args
3727+
let attrs = if partial then [res_partial_attr] else [] in
3728+
Ast_helper.Exp.apply ~loc ~attrs ~partial call_body args
37673729
in
37683730
wrap exp)
37693731
in

0 commit comments

Comments
 (0)