Skip to content

Commit a1b27d5

Browse files
committed
Simplify Texp_function and related code to only have 1 case.
1 parent 565fb57 commit a1b27d5

File tree

12 files changed

+85
-137
lines changed

12 files changed

+85
-137
lines changed

analysis/reanalyze/src/Arnold.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -908,7 +908,7 @@ module Compile = struct
908908
let open Command in
909909
c +++ ConstrOption Rnone
910910
| _ -> c)
911-
| Texp_function {cases} -> cases |> List.map (case ~ctx) |> Command.nondet
911+
| Texp_function {case = case_} -> case ~ctx case_
912912
| Texp_match (e, casesOk, casesExn, _partial)
913913
when not
914914
(casesExn

analysis/reanalyze/src/DeadValue.ml

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -156,18 +156,16 @@ let rec collectExpr super self (e : Typedtree.expression) =
156156
exp_desc =
157157
Texp_function
158158
{
159-
cases =
160-
[
161-
{
162-
c_lhs = {pat_desc = Tpat_var (etaArg, _)};
163-
c_rhs =
164-
{
165-
exp_desc =
166-
Texp_apply
167-
({exp_desc = Texp_ident (idArg2, _, _)}, args);
168-
};
169-
};
170-
];
159+
case =
160+
{
161+
c_lhs = {pat_desc = Tpat_var (etaArg, _)};
162+
c_rhs =
163+
{
164+
exp_desc =
165+
Texp_apply
166+
({exp_desc = Texp_ident (idArg2, _, _)}, args);
167+
};
168+
};
171169
};
172170
} )
173171
when Ident.name idArg = "arg"

compiler/gentype/TranslateStructure.ml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@ open GenTypeCommon
33
let rec addAnnotationsToTypes_ ~config ~(expr : Typedtree.expression)
44
(arg_types : arg_type list) =
55
match (expr.exp_desc, expr.exp_type.desc, arg_types) with
6-
| ( Texp_function {arg_label; param; cases = [{c_rhs}]},
7-
_,
8-
{a_type} :: next_types ) ->
6+
| Texp_function {arg_label; param; case = {c_rhs}}, _, {a_type} :: next_types
7+
->
98
let next_types1 =
109
next_types |> addAnnotationsToTypes_ ~config ~expr:c_rhs
1110
in
@@ -51,7 +50,7 @@ and add_annotations_to_fields ~config (expr : Typedtree.expression)
5150
(fields : fields) (arg_types : arg_type list) =
5251
match (expr.exp_desc, fields, arg_types) with
5352
| _, [], _ -> ([], arg_types |> add_annotations_to_types ~config ~expr)
54-
| Texp_function {cases = [{c_rhs}]}, field :: next_fields, _ ->
53+
| Texp_function {case = {c_rhs}}, field :: next_fields, _ ->
5554
let next_fields1, types1 =
5655
add_annotations_to_fields ~config c_rhs next_fields arg_types
5756
in

compiler/ml/printtyped.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -285,11 +285,11 @@ and expression i ppf x =
285285
line i ppf "Texp_let %a\n" fmt_rec_flag rf;
286286
list i value_binding ppf l;
287287
expression i ppf e
288-
| Texp_function {arg_label = p; param; cases; partial = _} ->
288+
| Texp_function {arg_label = p; param; case = case_; partial = _} ->
289289
line i ppf "Texp_function\n";
290290
line i ppf "%a" Ident.print param;
291291
arg_label i ppf p;
292-
list i case ppf cases
292+
case i ppf case_
293293
| Texp_apply (e, l) ->
294294
line i ppf "Texp_apply\n";
295295
expression i ppf e;

compiler/ml/rec_check.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -292,8 +292,8 @@ let rec expression : Env.env -> Typedtree.expression -> Use.t =
292292
let case env {Typedtree.c_rhs} = expression env c_rhs in
293293
Use.join (expression env e) (list case env cases)
294294
| Texp_override () -> assert false
295-
| Texp_function {cases} ->
296-
Use.delay (list (case ~scrutinee:Use.empty) env cases)
295+
| Texp_function {case = case_} ->
296+
Use.delay (list (case ~scrutinee:Use.empty) env [case_])
297297
| Texp_lazy e -> (
298298
match Typeopt.classify_lazy_argument e with
299299
| `Constant_or_function | `Identifier _ | `Float -> expression env e

compiler/ml/tast_iterator.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ let expr sub {exp_extra; exp_desc; exp_env; _} =
155155
| Texp_let (rec_flag, list, exp) ->
156156
sub.value_bindings sub (rec_flag, list);
157157
sub.expr sub exp
158-
| Texp_function {cases; _} -> sub.cases sub cases
158+
| Texp_function {case; _} -> sub.case sub case
159159
| Texp_apply (exp, list) ->
160160
sub.expr sub exp;
161161
List.iter (fun (_, o) -> Option.iter (sub.expr sub) o) list

compiler/ml/tast_mapper.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,8 +199,8 @@ let expr sub x =
199199
| Texp_let (rec_flag, list, exp) ->
200200
let rec_flag, list = sub.value_bindings sub (rec_flag, list) in
201201
Texp_let (rec_flag, list, sub.expr sub exp)
202-
| Texp_function {arg_label; param; cases; partial} ->
203-
Texp_function {arg_label; param; cases = sub.cases sub cases; partial}
202+
| Texp_function {arg_label; param; case; partial} ->
203+
Texp_function {arg_label; param; case = sub.case sub case; partial}
204204
| Texp_apply (exp, list) ->
205205
Texp_apply
206206
(sub.expr sub exp, List.map (tuple2 id (opt (sub.expr sub))) list)

compiler/ml/translcore.ml

Lines changed: 55 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -549,58 +549,50 @@ type binding =
549549
| Bind_value of value_binding list
550550
| Bind_module of Ident.t * string loc * module_expr
551551

552-
let rec push_defaults loc bindings cases partial =
553-
match cases with
554-
| [
555-
{
556-
c_lhs = pat;
557-
c_guard = None;
558-
c_rhs =
559-
{exp_desc = Texp_function {arg_label; param; cases; partial}} as exp;
560-
};
561-
] ->
562-
let cases = push_defaults exp.exp_loc bindings cases partial in
563-
[
564-
{
565-
c_lhs = pat;
566-
c_guard = None;
567-
c_rhs =
568-
{exp with exp_desc = Texp_function {arg_label; param; cases; partial}};
569-
};
570-
]
571-
| [
572-
{
573-
c_lhs = pat;
574-
c_guard = None;
575-
c_rhs =
576-
{
577-
exp_attributes = [({txt = "#default"}, _)];
578-
exp_desc =
579-
Texp_let (Nonrecursive, binds, ({exp_desc = Texp_function _} as e2));
580-
};
581-
};
582-
] ->
552+
let rec push_defaults loc bindings case partial =
553+
match case with
554+
| {
555+
c_lhs = pat;
556+
c_guard = None;
557+
c_rhs = {exp_desc = Texp_function {arg_label; param; case; partial}} as exp;
558+
} ->
559+
let case = push_defaults exp.exp_loc bindings case partial in
560+
561+
{
562+
c_lhs = pat;
563+
c_guard = None;
564+
c_rhs =
565+
{exp with exp_desc = Texp_function {arg_label; param; case; partial}};
566+
}
567+
| {
568+
c_lhs = pat;
569+
c_guard = None;
570+
c_rhs =
571+
{
572+
exp_attributes = [({txt = "#default"}, _)];
573+
exp_desc =
574+
Texp_let (Nonrecursive, binds, ({exp_desc = Texp_function _} as e2));
575+
};
576+
} ->
583577
push_defaults loc
584578
(Bind_value binds :: bindings)
585-
[{c_lhs = pat; c_guard = None; c_rhs = e2}]
579+
{c_lhs = pat; c_guard = None; c_rhs = e2}
586580
partial
587-
| [
588-
{
589-
c_lhs = pat;
590-
c_guard = None;
591-
c_rhs =
592-
{
593-
exp_attributes = [({txt = "#modulepat"}, _)];
594-
exp_desc =
595-
Texp_letmodule (id, name, mexpr, ({exp_desc = Texp_function _} as e2));
596-
};
597-
};
598-
] ->
581+
| {
582+
c_lhs = pat;
583+
c_guard = None;
584+
c_rhs =
585+
{
586+
exp_attributes = [({txt = "#modulepat"}, _)];
587+
exp_desc =
588+
Texp_letmodule (id, name, mexpr, ({exp_desc = Texp_function _} as e2));
589+
};
590+
} ->
599591
push_defaults loc
600592
(Bind_module (id, name, mexpr) :: bindings)
601-
[{c_lhs = pat; c_guard = None; c_rhs = e2}]
593+
{c_lhs = pat; c_guard = None; c_rhs = e2}
602594
partial
603-
| [case] ->
595+
| case ->
604596
let exp =
605597
List.fold_left
606598
(fun exp binds ->
@@ -614,45 +606,7 @@ let rec push_defaults loc bindings cases partial =
614606
})
615607
case.c_rhs bindings
616608
in
617-
[{case with c_rhs = exp}]
618-
| {c_lhs = pat; c_rhs = exp; c_guard = _} :: _ when bindings <> [] ->
619-
let param = Typecore.name_pattern "param" cases in
620-
let name = Ident.name param in
621-
let exp =
622-
{
623-
exp with
624-
exp_loc = loc;
625-
exp_desc =
626-
Texp_match
627-
( {
628-
exp with
629-
exp_type = pat.pat_type;
630-
exp_desc =
631-
Texp_ident
632-
( Path.Pident param,
633-
mknoloc (Longident.Lident name),
634-
{
635-
val_type = pat.pat_type;
636-
val_kind = Val_reg;
637-
val_attributes = [];
638-
Types.val_loc = Location.none;
639-
} );
640-
},
641-
cases,
642-
[],
643-
partial );
644-
}
645-
in
646-
push_defaults loc bindings
647-
[
648-
{
649-
c_lhs = {pat with pat_desc = Tpat_var (param, mknoloc name)};
650-
c_guard = None;
651-
c_rhs = exp;
652-
};
653-
]
654-
Total
655-
| _ -> cases
609+
{case with c_rhs = exp}
656610

657611
(* Assertions *)
658612

@@ -716,15 +670,15 @@ and transl_exp0 (e : Typedtree.expression) : Lambda.lambda =
716670
| Texp_constant cst -> Lconst (Const_base cst)
717671
| Texp_let (rec_flag, pat_expr_list, body) ->
718672
transl_let rec_flag pat_expr_list (transl_exp body)
719-
| Texp_function {arg_label = _; param; cases; partial} ->
673+
| Texp_function {arg_label = _; param; case; partial} ->
720674
let async = has_async_attribute e in
721675
let directive =
722676
match extract_directive_for_fn e with
723677
| None -> None
724678
| Some (directive, _) -> Some directive
725679
in
726680
let params, body, return_unit =
727-
let pl = push_defaults e.exp_loc [] cases partial in
681+
let pl = push_defaults e.exp_loc [] case partial in
728682
transl_function e.exp_loc partial param pl
729683
in
730684
let attr =
@@ -1088,32 +1042,28 @@ and transl_apply ?(inlined = Default_inline)
10881042
sargs)
10891043
: Lambda.lambda)
10901044

1091-
and transl_function loc partial param cases =
1092-
match cases with
1093-
| [
1094-
{
1095-
c_lhs = pat;
1096-
c_guard = None;
1097-
c_rhs =
1098-
{
1099-
exp_desc =
1100-
Texp_function
1101-
{arg_label = _; param = param'; cases; partial = partial'};
1102-
} as exp;
1103-
};
1104-
]
1045+
and transl_function loc partial param case =
1046+
match case with
1047+
| {
1048+
c_lhs = pat;
1049+
c_guard = None;
1050+
c_rhs =
1051+
{
1052+
exp_desc =
1053+
Texp_function {arg_label = _; param = param'; case; partial = partial'};
1054+
} as exp;
1055+
}
11051056
when Parmatch.inactive ~partial pat && not (exp |> has_async_attribute) ->
11061057
let params, body, return_unit =
1107-
transl_function exp.exp_loc partial' param' cases
1058+
transl_function exp.exp_loc partial' param' case
11081059
in
11091060
( param :: params,
11101061
Matching.for_function loc None (Lvar param) [(pat, body)] partial,
11111062
return_unit )
1112-
| {c_rhs = {exp_env; exp_type}; _} :: _ ->
1063+
| {c_rhs = {exp_env; exp_type}; _} ->
11131064
( [param],
1114-
Matching.for_function loc None (Lvar param) (transl_cases cases) partial,
1065+
Matching.for_function loc None (Lvar param) [transl_case case] partial,
11151066
is_base_type exp_env exp_type Predef.path_unit )
1116-
| _ -> assert false
11171067

11181068
and transl_let rec_flag pat_expr_list body =
11191069
match rec_flag with

compiler/ml/typecore.ml

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3306,13 +3306,14 @@ and type_function ?in_function loc attrs env ty_expected l caselist =
33063306
type_cases ~in_function:(loc_fun, ty_fun) env ty_arg ty_res true loc
33073307
caselist
33083308
in
3309+
let case = List.hd cases in
33093310
if is_optional l && not_function env ty_res then
3310-
Location.prerr_warning (List.hd cases).c_lhs.pat_loc
3311+
Location.prerr_warning case.c_lhs.pat_loc
33113312
Warnings.Unerasable_optional_argument;
33123313
let param = name_pattern "param" cases in
33133314
re
33143315
{
3315-
exp_desc = Texp_function {arg_label = l; param; cases; partial};
3316+
exp_desc = Texp_function {arg_label = l; param; case; partial};
33163317
exp_loc = loc;
33173318
exp_extra = [];
33183319
exp_type = instance env (newgenty (Tarrow (l, ty_arg, ty_res, Cok)));
@@ -3481,13 +3482,13 @@ and type_argument ?type_clash_context ?recarg env sarg ty_expected' ty_expected
34813482
exp_desc = Texp_apply (texp, args @ [(Nolabel, Some eta_var)]);
34823483
}
34833484
in
3484-
let cases = [case eta_pat e] in
3485-
let param = name_pattern "param" cases in
3485+
let case = case eta_pat e in
3486+
let param = name_pattern "param" [case] in
34863487
{
34873488
texp with
34883489
exp_type = ty_fun;
34893490
exp_desc =
3490-
Texp_function {arg_label = Nolabel; param; cases; partial = Total};
3491+
Texp_function {arg_label = Nolabel; param; case; partial = Total};
34913492
}
34923493
in
34933494
Location.prerr_warning texp.exp_loc

compiler/ml/typedtree.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ and expression_desc =
7979
| Texp_function of {
8080
arg_label: arg_label;
8181
param: Ident.t;
82-
cases: case list;
82+
case: case;
8383
partial: partial;
8484
}
8585
| Texp_apply of expression * (arg_label * expression option) list

compiler/ml/typedtree.mli

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ and expression_desc =
133133
| Texp_function of {
134134
arg_label: arg_label;
135135
param: Ident.t;
136-
cases: case list;
136+
case: case;
137137
partial: partial;
138138
}
139139
(** [Pexp_fun] and [Pexp_function] both translate to [Texp_function].

compiler/ml/typedtreeIter.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ end = struct
230230
| Texp_let (rec_flag, list, exp) ->
231231
iter_bindings rec_flag list;
232232
iter_expression exp
233-
| Texp_function {cases; _} -> iter_cases cases
233+
| Texp_function {case; _} -> iter_case case
234234
| Texp_apply (exp, list) ->
235235
iter_expression exp;
236236
List.iter

0 commit comments

Comments
 (0)