@@ -80,6 +80,7 @@ type error =
8080 | Type_params_not_supported of Longident .t
8181 | Field_access_on_dict_type
8282exception Error of Location .t * Env .t * error
83+ exception Errors of exn list
8384exception Error_forward of Location .error
8485
8586(* Forward declaration, to be filled in by Typemod.type_module *)
@@ -89,8 +90,13 @@ let delayed_typechecking_errors = ref []
8990let add_delayed_error e =
9091 delayed_typechecking_errors := e :: ! delayed_typechecking_errors
9192
92- let get_first_delayed_error () =
93- List. nth_opt (! delayed_typechecking_errors |> List. rev) 0
93+ let raise_delayed_error_if_exists () =
94+ (* Might have duplicate errors, so remove those. *)
95+ let errors = List. sort_uniq compare ! delayed_typechecking_errors in
96+ if errors <> [] then raise (Errors errors)
97+
98+ let raise_or_continue exn =
99+ if ! Clflags. editor_mode then add_delayed_error exn else raise exn
94100
95101let type_module =
96102 ref
@@ -322,15 +328,18 @@ let check_optional_attr env ld optional loc =
322328(* unification inside type_pat*)
323329let unify_pat_types loc env ty ty' =
324330 try unify env ty ty' with
325- | Unify trace -> raise (Error (loc, env, Pattern_type_clash trace))
331+ | Unify trace ->
332+ raise_or_continue (Error (loc, env, Pattern_type_clash trace))
326333 | Tags (l1 , l2 ) ->
327- raise (Typetexp. Error (loc, env, Typetexp. Variant_tags (l1, l2)))
334+ raise_or_continue
335+ (Typetexp. Error (loc, env, Typetexp. Variant_tags (l1, l2)))
328336
329337(* unification inside type_exp and type_expect *)
330338let unify_exp_types ?type_clash_context loc env ty expected_ty =
331339 try unify env ty expected_ty with
332340 | Unify trace ->
333- raise (Error (loc, env, Expr_type_clash (trace, type_clash_context)))
341+ raise_or_continue
342+ (Error (loc, env, Expr_type_clash (trace, type_clash_context)))
334343 | Tags (l1 , l2 ) ->
335344 raise (Typetexp. Error (loc, env, Typetexp. Variant_tags (l1, l2)))
336345
@@ -348,11 +357,13 @@ let unify_pat_types_gadt loc env ty ty' =
348357 | Some x -> x
349358 in
350359 try unify_gadt ~newtype_level env ty ty' with
351- | Unify trace -> raise (Error (loc, ! env, Pattern_type_clash trace))
360+ | Unify trace ->
361+ raise_or_continue (Error (loc, ! env, Pattern_type_clash trace))
352362 | Tags (l1 , l2 ) ->
353- raise (Typetexp. Error (loc, ! env, Typetexp. Variant_tags (l1, l2)))
363+ raise_or_continue
364+ (Typetexp. Error (loc, ! env, Typetexp. Variant_tags (l1, l2)))
354365 | Unification_recursive_abbrev trace ->
355- raise (Error (loc, ! env, Recursive_local_constraint trace))
366+ raise_or_continue (Error (loc, ! env, Recursive_local_constraint trace))
356367
357368(* Creating new conjunctive types is not allowed when typing patterns *)
358369
@@ -460,7 +471,8 @@ let enter_orpat_variables loc env p1_vs p2_vs =
460471 else (
461472 (try unify env t1 t2
462473 with Unify trace ->
463- raise (Error (loc, env, Or_pattern_type_clash (x1, trace))));
474+ raise_or_continue
475+ (Error (loc, env, Or_pattern_type_clash (x1, trace))));
464476 (x2, x1) :: unify_vars rem1 rem2)
465477 | [] , [] -> []
466478 | (x , _ , _ , _ , _ ) :: _ , [] -> raise (Error (loc, env, Orpat_vars (x, [] )))
@@ -1934,7 +1946,8 @@ let rec type_approx env sexp =
19341946 let ty1 = approx_type env sty in
19351947 (try unify env ty ty1
19361948 with Unify trace ->
1937- raise (Error (sexp.pexp_loc, env, Expr_type_clash (trace, None ))));
1949+ raise_or_continue
1950+ (Error (sexp.pexp_loc, env, Expr_type_clash (trace, None ))));
19381951 ty1
19391952 | Pexp_coerce (e , () , sty2 ) ->
19401953 let approx_ty_opt = function
@@ -1946,7 +1959,8 @@ let rec type_approx env sexp =
19461959 and ty2 = approx_type env sty2 in
19471960 (try unify env ty ty1
19481961 with Unify trace ->
1949- raise (Error (sexp.pexp_loc, env, Expr_type_clash (trace, None ))));
1962+ raise_or_continue
1963+ (Error (sexp.pexp_loc, env, Expr_type_clash (trace, None ))));
19501964 ty2
19511965 | _ -> newvar ()
19521966
@@ -2269,11 +2283,6 @@ and type_expect ?type_clash_context ?in_function ?recarg env sexp ty_expected =
22692283 in
22702284 Cmt_format. set_saved_types
22712285 (Cmt_format. Partial_expression exp :: previous_saved_types);
2272-
2273- (match get_first_delayed_error () with
2274- | None -> ()
2275- | Some e -> raise e);
2276-
22772286 exp
22782287
22792288and type_expect_ ?type_clash_context ?in_function ?(recarg = Rejected ) env sexp
@@ -2953,7 +2962,7 @@ and type_expect_ ?type_clash_context ?in_function ?(recarg = Rejected) env sexp
29532962 let gen = generalizable tv.level arg.exp_type in
29542963 (try unify_var env tv arg.exp_type
29552964 with Unify trace ->
2956- raise
2965+ raise_or_continue
29572966 (Error
29582967 (arg.exp_loc, env, Expr_type_clash (trace, type_clash_context))));
29592968 gen)
@@ -3351,8 +3360,11 @@ and type_label_exp ?type_clash_context create env loc ty_expected
33513360 (* Generalize information merged from ty_expected *)
33523361 generalize_structure ty_arg);
33533362 if label.lbl_private = Private then
3354- if create then raise (Error (loc, env, Private_type ty_expected))
3355- else raise (Error (lid.loc, env, Private_label (lid.txt, ty_expected)));
3363+ if create then
3364+ raise_or_continue (Error (loc, env, Private_type ty_expected))
3365+ else
3366+ raise_or_continue
3367+ (Error (lid.loc, env, Private_label (lid.txt, ty_expected)));
33563368 let arg =
33573369 let snap = if vars = [] then None else Some (Btype. snapshot () ) in
33583370 let arg =
@@ -3565,11 +3577,8 @@ and type_application ?type_clash_context total_app env funct (sargs : sargs) :
35653577 | l , Some f ->
35663578 ( l,
35673579 Some
3568- (if ! Clflags. editor_mode then (
3569- try f ()
3570- with e ->
3571- add_delayed_error e;
3572- tainted () )
3580+ (if ! Clflags. editor_mode then
3581+ try f () with _ -> tainted ()
35733582 else f () ) ))
35743583 (List. rev args),
35753584 instance env (result_type omitted ty_fun) )
0 commit comments