Skip to content

Commit dd373f0

Browse files
committed
Remove remaining uses of type_to_arity.
1 parent 5c74ce5 commit dd373f0

File tree

4 files changed

+62
-36
lines changed

4 files changed

+62
-36
lines changed

compiler/ml/ast_uncurried.ml

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -78,16 +78,18 @@ let arity_to_type arity =
7878
row_name = None;
7979
})
8080

81-
let type_to_arity (t_arity : Types.type_expr) =
82-
match (Ctype.repr t_arity).desc with
83-
| Tvariant {row_fields = [(label, _)]} -> decode_arity_string label
84-
| _ -> assert false
85-
86-
let fun_type_to_arity (t_arity : Types.type_expr) =
81+
let tarrow_to_arity (t_arity : Types.type_expr) =
8782
match (Ctype.repr t_arity).desc with
8883
| Tarrow (_, _, _, _, Some arity) -> arity
8984
| Tarrow _ -> assert false
90-
| _ -> assert false
85+
| _ ->
86+
Format.eprintf "t: %a@." Printtyp.raw_type_expr t_arity;
87+
assert false
88+
89+
let tarrow_to_arity_opt (t_arity : Types.type_expr) =
90+
match (Ctype.repr t_arity).desc with
91+
| Tarrow (_, _, _, _, arity) -> arity
92+
| _ -> None
9193

9294
let make_uncurried_type ~env ~arity (t : Types.type_expr) =
9395
let typ_arity = arity_to_type arity in
@@ -105,11 +107,11 @@ let make_uncurried_type ~env ~arity (t : Types.type_expr) =
105107

106108
let uncurried_type_get_arity ~env typ =
107109
match (Ctype.expand_head env typ).desc with
108-
| Tconstr (Pident {name = "function$"}, [t; _arity], _) -> fun_type_to_arity t
110+
| Tconstr (Pident {name = "function$"}, [t; _arity], _) -> tarrow_to_arity t
109111
| _ -> assert false
110112

111113
let uncurried_type_get_arity_opt ~env typ =
112114
match (Ctype.expand_head env typ).desc with
113115
| Tconstr (Pident {name = "function$"}, [t; _arity], _) ->
114-
Some (fun_type_to_arity t)
116+
Some (tarrow_to_arity t)
115117
| _ -> None

compiler/ml/ctype.ml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2310,10 +2310,11 @@ and unify3 env t1 t1' t2 t2' =
23102310
| Pattern -> add_type_equality t1' t2');
23112311
try
23122312
(match (d1, d2) with
2313-
| Tarrow (l1, t1, u1, c1, _), Tarrow (l2, t2, u2, c2, _)
2314-
when Asttypes.same_arg_label l1 l2
2315-
|| (!umode = Pattern && not (is_optional l1 || is_optional l2))
2316-
-> (
2313+
| Tarrow (l1, t1, u1, c1, a1), Tarrow (l2, t2, u2, c2, a2)
2314+
when a1 = a2
2315+
&& (Asttypes.same_arg_label l1 l2
2316+
|| (!umode = Pattern && not (is_optional l1 || is_optional l2))
2317+
) -> (
23172318
unify env t1 t2;
23182319
unify env u1 u2;
23192320
match (commu_repr c1, commu_repr c2) with

compiler/ml/printtyp.ml

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,10 @@ let string_of_label = function
146146
| Labelled s -> s
147147
| Optional s -> "?" ^ s
148148

149+
let string_of_arity = function
150+
| None -> ""
151+
| Some arity -> string_of_int arity
152+
149153
let visited = ref []
150154
let rec raw_type ppf ty =
151155
let ty = safe_repr [] ty in
@@ -159,9 +163,10 @@ and raw_type_list tl = raw_list raw_type tl
159163

160164
and raw_type_desc ppf = function
161165
| Tvar name -> fprintf ppf "Tvar %a" print_name name
162-
| Tarrow (l, t1, t2, c, _) ->
163-
fprintf ppf "@[<hov1>Tarrow(\"%s\",@,%a,@,%a,@,%s)@]" (string_of_label l)
164-
raw_type t1 raw_type t2 (safe_commu_repr [] c)
166+
| Tarrow (l, t1, t2, c, a) ->
167+
fprintf ppf "@[<hov1>Tarrow(\"%s\",@,%a,@,%a,@,%s,@,%s)@]"
168+
(string_of_label l) raw_type t1 raw_type t2 (safe_commu_repr [] c)
169+
(string_of_arity a)
165170
| Ttuple tl -> fprintf ppf "@[<1>Ttuple@,%a@]" raw_type_list tl
166171
| Tconstr (p, tl, abbrev) ->
167172
fprintf ppf "@[<hov1>Tconstr(@,%a,@,%a,@,%a)@]" path p raw_type_list tl

compiler/ml/typecore.ml

Lines changed: 38 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -315,8 +315,6 @@ let unify_pat_types loc env ty ty' =
315315

316316
(* unification inside type_exp and type_expect *)
317317
let unify_exp_types ?type_clash_context loc env ty expected_ty =
318-
(* Format.eprintf "@[%a@ %a@]@." Printtyp.raw_type_expr exp.exp_type
319-
Printtyp.raw_type_expr expected_ty; *)
320318
try unify env ty expected_ty with
321319
| Unify trace ->
322320
raise (Error (loc, env, Expr_type_clash (trace, type_clash_context)))
@@ -3268,7 +3266,7 @@ and type_function ?in_function ~arity loc attrs env ty_expected_ l caselist =
32683266
match arity with
32693267
| None -> ty_expected_
32703268
| Some arity ->
3271-
let fun_t = newvar () in
3269+
let fun_t = newty (Tarrow (l, newvar (), newvar (), Cok, Some arity)) in
32723270
let uncurried_typ = Ast_uncurried.make_uncurried_type ~env ~arity fun_t in
32733271
unify_exp_types loc env uncurried_typ ty_expected_;
32743272
fun_t
@@ -3519,7 +3517,6 @@ and translate_unified_ops (env : Env.t) (funct : Typedtree.expression)
35193517
35203518
and type_application ?type_clash_context uncurried env funct (sargs : sargs) :
35213519
targs * Types.type_expr * bool =
3522-
(* funct.exp_type may be generic *)
35233520
let result_type omitted ty_fun =
35243521
List.fold_left
35253522
(fun ty_fun (l, ty, lv) -> newty2 lv (Tarrow (l, ty, ty_fun, Cok, None)))
@@ -3530,15 +3527,20 @@ and type_application ?type_clash_context uncurried env funct (sargs : sargs) :
35303527
tvar || List.mem l ls
35313528
in
35323529
let ignored = ref [] in
3533-
let has_uncurried_type t =
3530+
let has_uncurried_type funct =
3531+
let t = funct.exp_type in
35343532
match (expand_head env t).desc with
3535-
| Tconstr (Pident {name = "function$"}, [t; t_arity], _) ->
3536-
let arity = Ast_uncurried.type_to_arity t_arity in
3533+
| Tconstr (Pident {name = "function$"}, [t; _t_arity], _) ->
3534+
let arity =
3535+
match Ast_uncurried.tarrow_to_arity_opt t with
3536+
| Some arity -> arity
3537+
| None -> List.length sargs
3538+
in
35373539
Some (arity, t)
35383540
| _ -> None
35393541
in
35403542
let force_uncurried_type funct =
3541-
match has_uncurried_type funct.exp_type with
3543+
match has_uncurried_type funct with
35423544
| None -> (
35433545
let arity = List.length sargs in
35443546
let uncurried_typ =
@@ -3554,8 +3556,9 @@ and type_application ?type_clash_context uncurried env funct (sargs : sargs) :
35543556
Apply_non_function (expand_head env funct.exp_type) )))
35553557
| Some _ -> ()
35563558
in
3557-
let extract_uncurried_type t =
3558-
match has_uncurried_type t with
3559+
let extract_uncurried_type funct =
3560+
let t = funct.exp_type in
3561+
match has_uncurried_type funct with
35593562
| Some (arity, t1) ->
35603563
if List.length sargs > arity then
35613564
raise
@@ -3566,8 +3569,8 @@ and type_application ?type_clash_context uncurried env funct (sargs : sargs) :
35663569
(t1, arity)
35673570
| None -> (t, max_int)
35683571
in
3569-
let update_uncurried_arity ~nargs t new_t =
3570-
match has_uncurried_type t with
3572+
let update_uncurried_arity ~nargs funct new_t =
3573+
match has_uncurried_type funct with
35713574
| Some (arity, _) ->
35723575
let newarity = arity - nargs in
35733576
let fully_applied = newarity <= 0 in
@@ -3576,7 +3579,8 @@ and type_application ?type_clash_context uncurried env funct (sargs : sargs) :
35763579
(Error
35773580
( funct.exp_loc,
35783581
env,
3579-
Uncurried_arity_mismatch (t, arity, List.length sargs) ));
3582+
Uncurried_arity_mismatch
3583+
(funct.exp_type, arity, List.length sargs) ));
35803584
let new_t =
35813585
if fully_applied then new_t
35823586
else Ast_uncurried.make_uncurried_type ~env ~arity:newarity new_t
@@ -3721,7 +3725,7 @@ and type_application ?type_clash_context uncurried env funct (sargs : sargs) :
37213725
(Ext_list.filter labels (fun x -> x <> Nolabel))) ))
37223726
in
37233727
if uncurried then force_uncurried_type funct;
3724-
let ty, max_arity = extract_uncurried_type funct.exp_type in
3728+
let ty, max_arity = extract_uncurried_type funct in
37253729
let top_arity = if uncurried then Some max_arity else None in
37263730
match sargs with
37273731
(* Special case for ignore: avoid discarding warning *)
@@ -3744,7 +3748,7 @@ and type_application ?type_clash_context uncurried env funct (sargs : sargs) :
37443748
~sargs ~top_arity
37453749
in
37463750
let fully_applied, ret_t =
3747-
update_uncurried_arity funct.exp_type
3751+
update_uncurried_arity funct
37483752
~nargs:(List.length !ignored + List.length sargs)
37493753
ret_t
37503754
in
@@ -4340,13 +4344,27 @@ let report_error env ppf = function
43404344
"This function is an uncurried function where a curried function is \
43414345
expected"
43424346
| Expr_type_clash
4343-
( (_, {desc = Tconstr (Pident {name = "function$"}, [_; t_a], _)})
4344-
:: (_, {desc = Tconstr (Pident {name = "function$"}, [_; t_b], _)})
4347+
( ( _,
4348+
{
4349+
desc =
4350+
Tconstr
4351+
( Pident {name = "function$"},
4352+
[{desc = Tarrow (_, _, _, _, Some arity_a)}; _],
4353+
_ );
4354+
} )
4355+
:: ( _,
4356+
{
4357+
desc =
4358+
Tconstr
4359+
( Pident {name = "function$"},
4360+
[{desc = Tarrow (_, _, _, _, Some arity_b)}; _],
4361+
_ );
4362+
} )
43454363
:: _,
43464364
_ )
4347-
when Ast_uncurried.type_to_arity t_a <> Ast_uncurried.type_to_arity t_b ->
4348-
let arity_a = Ast_uncurried.type_to_arity t_a |> string_of_int in
4349-
let arity_b = Ast_uncurried.type_to_arity t_b |> string_of_int in
4365+
when arity_a <> arity_b ->
4366+
let arity_a = arity_a |> string_of_int in
4367+
let arity_b = arity_b |> string_of_int in
43504368
report_arity_mismatch ~arity_a ~arity_b ppf
43514369
| Expr_type_clash
43524370
( ( _,

0 commit comments

Comments
 (0)