@@ -2371,7 +2371,7 @@ and type_expect_ ?type_clash_context ?in_function ?(recarg = Rejected) env sexp
2371
2371
exp_attributes = sexp.pexp_attributes;
2372
2372
exp_env = env;
2373
2373
}
2374
- | Pexp_fun (l , Some default , spat , sbody , _arity ) ->
2374
+ | Pexp_fun (l , Some default , spat , sbody , arity ) ->
2375
2375
assert (is_optional l);
2376
2376
(* default allowed only with optional argument *)
2377
2377
let open Ast_helper in
@@ -2409,10 +2409,10 @@ and type_expect_ ?type_clash_context ?in_function ?(recarg = Rejected) env sexp
2409
2409
[Vb. mk spat smatch]
2410
2410
sbody
2411
2411
in
2412
- type_function ?in_function loc sexp.pexp_attributes env ty_expected l
2412
+ type_function ?in_function ~arity loc sexp.pexp_attributes env ty_expected l
2413
2413
[Exp. case pat body]
2414
- | Pexp_fun (l , None, spat , sbody , _arity ) ->
2415
- type_function ?in_function loc sexp.pexp_attributes env ty_expected l
2414
+ | Pexp_fun (l , None, spat , sbody , arity ) ->
2415
+ type_function ?in_function ~arity loc sexp.pexp_attributes env ty_expected l
2416
2416
[Ast_helper.Exp. case spat sbody]
2417
2417
| Pexp_apply (sfunct , sargs ) ->
2418
2418
assert (sargs <> [] );
@@ -3273,7 +3273,7 @@ and type_expect_ ?type_clash_context ?in_function ?(recarg = Rejected) env sexp
3273
3273
| Pexp_extension ext ->
3274
3274
raise (Error_forward (Builtin_attributes. error_of_extension ext))
3275
3275
3276
- and type_function ?in_function loc attrs env ty_expected l caselist =
3276
+ and type_function ?in_function ~ arity loc attrs env ty_expected l caselist =
3277
3277
let loc_fun, ty_fun =
3278
3278
match in_function with
3279
3279
| Some p -> p
@@ -3313,7 +3313,7 @@ and type_function ?in_function loc attrs env ty_expected l caselist =
3313
3313
let param = name_pattern " param" cases in
3314
3314
re
3315
3315
{
3316
- exp_desc = Texp_function {arg_label = l; param; case; partial};
3316
+ exp_desc = Texp_function {arg_label = l; arity; param; case; partial};
3317
3317
exp_loc = loc;
3318
3318
exp_extra = [] ;
3319
3319
exp_type = instance env (newgenty (Tarrow (l, ty_arg, ty_res, Cok )));
@@ -3404,119 +3404,9 @@ and type_label_exp ?type_clash_context create env loc ty_expected
3404
3404
3405
3405
and type_argument ?type_clash_context ?recarg env sarg ty_expected' ty_expected
3406
3406
=
3407
- (* ty_expected' may be generic *)
3408
- let no_labels ty =
3409
- let ls, tvar = list_labels env ty in
3410
- (not tvar) && List. for_all (fun x -> x = Nolabel ) ls
3411
- in
3412
- let rec is_inferred sexp =
3413
- match sexp.pexp_desc with
3414
- | Pexp_ident _ | Pexp_apply _ | Pexp_field _ | Pexp_constraint _
3415
- | Pexp_coerce _ | Pexp_send _ | Pexp_new _ ->
3416
- true
3417
- | Pexp_sequence (_ , e ) | Pexp_open (_ , _ , e ) -> is_inferred e
3418
- | Pexp_ifthenelse (_ , e1 , Some e2 ) -> is_inferred e1 && is_inferred e2
3419
- | _ -> false
3420
- in
3421
- match expand_head env ty_expected' with
3422
- | {desc = Tarrow (Nolabel , ty_arg, ty_res, _); level = _}
3423
- when is_inferred sarg ->
3424
- (* apply optional arguments when expected type is "" *)
3425
- (* we must be very careful about not breaking the semantics *)
3426
- let texp = type_exp env sarg in
3427
- let rec make_args args ty_fun =
3428
- match (expand_head env ty_fun).desc with
3429
- | Tarrow (l , ty_arg , ty_fun , _ ) when is_optional l ->
3430
- let ty = option_none (instance env ty_arg) sarg.pexp_loc in
3431
- make_args ((l, Some ty) :: args) ty_fun
3432
- | Tarrow (Nolabel, _ , ty_res' , _ ) ->
3433
- (List. rev args, ty_fun, no_labels ty_res')
3434
- | Tvar _ -> (List. rev args, ty_fun, false )
3435
- | _ -> ([] , texp.exp_type, false )
3436
- in
3437
- let args, ty_fun', simple_res = make_args [] texp.exp_type in
3438
- let texp = {texp with exp_type = instance env texp.exp_type}
3439
- and ty_fun = instance env ty_fun' in
3440
- if not (simple_res || no_labels ty_res) then (
3441
- unify_exp env texp ty_expected;
3442
- texp)
3443
- else (
3444
- unify_exp env {texp with exp_type = ty_fun} ty_expected;
3445
- if args = [] then texp
3446
- else
3447
- (* eta-expand to avoid side effects *)
3448
- let var_pair name ty =
3449
- let id = Ident. create name in
3450
- ( {
3451
- pat_desc = Tpat_var (id, mknoloc name);
3452
- pat_type = ty;
3453
- pat_extra = [] ;
3454
- pat_attributes = [] ;
3455
- pat_loc = Location. none;
3456
- pat_env = env;
3457
- },
3458
- {
3459
- exp_type = ty;
3460
- exp_loc = Location. none;
3461
- exp_env = env;
3462
- exp_extra = [] ;
3463
- exp_attributes = [] ;
3464
- exp_desc =
3465
- Texp_ident
3466
- ( Path. Pident id,
3467
- mknoloc (Longident. Lident name),
3468
- {
3469
- val_type = ty;
3470
- val_kind = Val_reg ;
3471
- val_attributes = [] ;
3472
- Types. val_loc = Location. none;
3473
- } );
3474
- } )
3475
- in
3476
- let eta_pat, eta_var = var_pair " eta" ty_arg in
3477
- let func texp =
3478
- let e =
3479
- {
3480
- texp with
3481
- exp_type = ty_res;
3482
- exp_desc = Texp_apply (texp, args @ [(Nolabel , Some eta_var)]);
3483
- }
3484
- in
3485
- let case = case eta_pat e in
3486
- let param = name_pattern " param" [case] in
3487
- {
3488
- texp with
3489
- exp_type = ty_fun;
3490
- exp_desc =
3491
- Texp_function {arg_label = Nolabel ; param; case; partial = Total };
3492
- }
3493
- in
3494
- Location. prerr_warning texp.exp_loc
3495
- (Warnings. Eliminated_optional_arguments
3496
- (List. map (fun (l , _ ) -> Printtyp. string_of_label l) args));
3497
- (* let-expand to have side effects *)
3498
- let let_pat, let_var = var_pair " arg" texp.exp_type in
3499
- re
3500
- {
3501
- texp with
3502
- exp_type = ty_fun;
3503
- exp_desc =
3504
- Texp_let
3505
- ( Nonrecursive ,
3506
- [
3507
- {
3508
- vb_pat = let_pat;
3509
- vb_expr = texp;
3510
- vb_attributes = [] ;
3511
- vb_loc = Location. none;
3512
- };
3513
- ],
3514
- func let_var );
3515
- })
3516
- | _ ->
3517
- let texp = type_expect ?type_clash_context ?recarg env sarg ty_expected' in
3518
- unify_exp ?type_clash_context env texp ty_expected;
3519
- texp
3407
+ let texp = type_expect ?type_clash_context ?recarg env sarg ty_expected' in
3408
+ unify_exp ?type_clash_context env texp ty_expected;
3409
+ texp
3520
3410
3521
3411
and is_automatic_curried_application env funct =
3522
3412
(* When a curried function is used with uncurried application, treat it as a curried application *)
0 commit comments