Skip to content

Commit e96f2f2

Browse files
authored
Remove res partial (#7240)
* Add partial flag to untyped and typed AST node and prepare to remove `@res.partial`. * Remove `@res.partial` entirely. * Update CHANGELOG.md
1 parent 38a93a4 commit e96f2f2

40 files changed

+159
-165
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
- AST cleanup: Prepare for ast async cleanup: Refactor code for "@res.async" payload handling and clean up handling of type and term parameters, so that now each `=>` in a function definition corresponds to a function. https://github.com/rescript-lang/rescript/pull/7223
2525
- AST: always put type parameters first in function definitions. https://github.com/rescript-lang/rescript/pull/7233
2626
- AST cleanup: Remove `@res.async` attribute from the internal representation, and add a flag to untyped and typed ASTs instead. https://github.com/rescript-lang/rescript/pull/7234
27+
- AST cleanup: Remove `@res.partial` attribute from the internal representation, and add a flag to untyped and typed ASTs instead. https://github.com/rescript-lang/rescript/pull/7238 https://github.com/rescript-lang/rescript/pull/7240
2728

2829
# 12.0.0-alpha.7
2930

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/ast_mapper_from0.ml

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,18 @@ module E = struct
310310
(sub.pat sub p) (sub.expr sub e)
311311
| Pexp_function _ -> assert false
312312
| Pexp_apply (e, l) ->
313-
apply ~loc ~attrs (sub.expr sub e) (List.map (map_snd (sub.expr sub)) l)
313+
let process_partial_app_attribute attrs =
314+
let rec process partial_app acc attrs =
315+
match attrs with
316+
| [] -> (partial_app, List.rev acc)
317+
| ({Location.txt = "res.partial"}, _) :: rest -> process true acc rest
318+
| attr :: rest -> process partial_app (attr :: acc) rest
319+
in
320+
process false [] attrs
321+
in
322+
let partial, attrs = process_partial_app_attribute attrs in
323+
apply ~loc ~attrs ~partial (sub.expr sub e)
324+
(List.map (map_snd (sub.expr sub)) l)
314325
| Pexp_match (e, pel) ->
315326
match_ ~loc ~attrs (sub.expr sub e) (sub.cases sub pel)
316327
| Pexp_try (e, pel) -> try_ ~loc ~attrs (sub.expr sub e) (sub.cases sub pel)

compiler/ml/ast_mapper_to0.ml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,11 @@ module E = struct
325325
~attrs:(arity_to_attributes arity)
326326
(Location.mkloc (Longident.Lident "Function$") e.pexp_loc)
327327
(Some e))
328-
| Pexp_apply {funct = e; args = l} ->
328+
| Pexp_apply {funct = e; args = l; partial} ->
329+
let attrs =
330+
if partial then (Location.mknoloc "res.partial", Pt.PStr []) :: attrs
331+
else []
332+
in
329333
apply ~loc ~attrs (sub.expr sub e) (List.map (map_snd (sub.expr sub)) l)
330334
| Pexp_match (e, pel) ->
331335
match_ ~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

0 commit comments

Comments
 (0)