@@ -2638,6 +2638,17 @@ let unify_exp env exp expected_ty =
2638
2638
with Error (loc , env , Expr_type_clash(trace , tfc , None)) ->
2639
2639
raise (Error (loc, env, Expr_type_clash (trace, tfc, Some exp.exp_desc)))
2640
2640
2641
+ (* If [is_inferred e] is true, [e] will be typechecked without using
2642
+ the "expected type" provided by the context. *)
2643
+
2644
+ let rec is_inferred sexp =
2645
+ match sexp.pexp_desc with
2646
+ | Pexp_ident _ | Pexp_apply _ | Pexp_field _ | Pexp_constraint _
2647
+ | Pexp_coerce _ | Pexp_send _ | Pexp_new _ -> true
2648
+ | Pexp_sequence (_ , e ) | Pexp_open (_ , e ) -> is_inferred e
2649
+ | Pexp_ifthenelse (_ , e1 , Some e2 ) -> is_inferred e1 && is_inferred e2
2650
+ | _ -> false
2651
+
2641
2652
let rec type_exp ?recarg env (mode : expected_mode ) sexp =
2642
2653
(* We now delegate everything to type_expect *)
2643
2654
type_expect ?recarg env mode sexp (mk_expected (newvar () ))
@@ -2834,30 +2845,47 @@ and type_expect_
2834
2845
(Nontail alloc_local) e ty_expected_explained
2835
2846
| Pexp_apply (sfunct , sargs ) ->
2836
2847
assert (sargs <> [] );
2837
- begin_def () ; (* one more level for non-returning functions *)
2838
- if ! Clflags. principal then begin_def () ;
2839
2848
let funct_mode =
2840
2849
match mode with
2841
2850
| Nontail _ -> Alloc_mode. newvar ()
2842
2851
| Tail _ -> Alloc_mode. heap
2843
2852
in
2844
- let funct = type_exp env (Nontail funct_mode) sfunct in
2845
- if ! Clflags. principal then begin
2846
- end_def () ;
2847
- generalize_structure funct.exp_type
2848
- end ;
2849
2853
let rec lower_args seen ty_fun =
2850
2854
let ty = expand_head env ty_fun in
2851
2855
if List. memq ty seen then () else
2852
- match ty.desc with
2853
- Tarrow (_l , ty_arg , ty_fun , _com ) ->
2854
- (try unify_var env (newvar() ) ty_arg with Unify _ -> assert false );
2855
- lower_args (ty::seen) ty_fun
2856
- | _ -> ()
2856
+ match ty.desc with
2857
+ Tarrow (_l , ty_arg , ty_fun , _com ) ->
2858
+ (try unify_var env (newvar() ) ty_arg
2859
+ with Unify _ -> assert false );
2860
+ lower_args (ty::seen) ty_fun
2861
+ | _ -> ()
2862
+ in
2863
+ let type_sfunct sfunct =
2864
+ begin_def () ; (* one more level for non-returning functions *)
2865
+ if ! Clflags. principal then begin_def () ;
2866
+ let funct = type_exp env (Nontail funct_mode) sfunct in
2867
+ if ! Clflags. principal then begin
2868
+ end_def () ;
2869
+ generalize_structure funct.exp_type
2870
+ end ;
2871
+ let ty = instance funct.exp_type in
2872
+ end_def () ;
2873
+ wrap_trace_gadt_instances env (lower_args [] ) ty;
2874
+ funct
2875
+ in
2876
+ let funct, sargs =
2877
+ let funct = type_sfunct sfunct in
2878
+ match funct.exp_desc, sargs with
2879
+ | Texp_ident (_, _, {val_kind = Val_prim {prim_name = " %revapply" }}),
2880
+ [Nolabel , sarg; Nolabel , actual_sfunct]
2881
+ when is_inferred actual_sfunct ->
2882
+ type_sfunct actual_sfunct, [Nolabel , sarg]
2883
+ | Texp_ident (_, _, {val_kind = Val_prim {prim_name = " %apply" }}),
2884
+ [Nolabel , actual_sfunct; Nolabel , sarg] ->
2885
+ type_sfunct actual_sfunct, [Nolabel , sarg]
2886
+ | _ ->
2887
+ funct, sargs
2857
2888
in
2858
- let ty = instance funct.exp_type in
2859
- end_def () ;
2860
- wrap_trace_gadt_instances env (lower_args [] ) ty;
2861
2889
begin_def () ;
2862
2890
let (args, ty_res) = type_application env loc mode funct sargs in
2863
2891
end_def () ;
@@ -4346,17 +4374,6 @@ and type_argument ?explanation ?recarg env mode sarg ty_expected' ty_expected =
4346
4374
let ls, tvar = list_labels env ty in
4347
4375
not tvar && List. for_all ((= ) Nolabel ) ls
4348
4376
in
4349
- let rec is_inferred sexp =
4350
- match sexp.pexp_desc with
4351
- | Pexp_apply
4352
- ({ pexp_desc = Pexp_extension ({txt = " stack" }, PStr [] ) },
4353
- [Nolabel , _]) -> false
4354
- | Pexp_ident _ | Pexp_apply _ | Pexp_field _ | Pexp_constraint _
4355
- | Pexp_coerce _ | Pexp_send _ | Pexp_new _ -> true
4356
- | Pexp_sequence (_ , e ) | Pexp_open (_ , e ) -> is_inferred e
4357
- | Pexp_ifthenelse (_ , e1 , Some e2 ) -> is_inferred e1 && is_inferred e2
4358
- | _ -> false
4359
- in
4360
4377
match expand_head env ty_expected' with
4361
4378
{desc = Tarrow ((Nolabel,marg ,mret ),ty_arg ,ty_res ,_ ); level = lv }
4362
4379
when is_inferred sarg ->
0 commit comments