Skip to content

Commit bac24c7

Browse files
committed
return multiple errors in editor mode
1 parent d11b0ff commit bac24c7

File tree

4 files changed

+40
-24
lines changed

4 files changed

+40
-24
lines changed

compiler/bsc/rescript_compiler_main.ml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,9 @@ let _ : unit =
456456
| Bsc_args.Bad msg ->
457457
Format.eprintf "%s@." msg;
458458
exit 2
459+
| Typecore.Errors exns ->
460+
exns |> List.rev |> List.iter (Location.report_exception ppf);
461+
exit 2
459462
| x ->
460463
(*
461464
Ext_obj.bt ();

compiler/core/js_implementation.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ let after_parsing_impl ppf outputprefix (ast : Parsetree.structure) =
138138
?check_exists:(if !Js_config.force_cmi then None else Some ())
139139
!Location.input_name outputprefix modulename env ast
140140
in
141+
if !Clflags.editor_mode then Typecore.raise_delayed_error_if_exists ();
141142
let typedtree_coercion = (typedtree, coercion) in
142143
print_if ppf Clflags.dump_typedtree
143144
Printtyped.implementation_with_coercion typedtree_coercion;

compiler/ml/typecore.ml

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ type error =
8080
| Type_params_not_supported of Longident.t
8181
| Field_access_on_dict_type
8282
exception Error of Location.t * Env.t * error
83+
exception Errors of exn list
8384
exception 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 []
8990
let 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

95101
let type_module =
96102
ref
@@ -319,15 +325,18 @@ let check_optional_attr env ld optional loc =
319325
(* unification inside type_pat*)
320326
let unify_pat_types loc env ty ty' =
321327
try unify env ty ty' with
322-
| Unify trace -> raise (Error (loc, env, Pattern_type_clash trace))
328+
| Unify trace ->
329+
raise_or_continue (Error (loc, env, Pattern_type_clash trace))
323330
| Tags (l1, l2) ->
324-
raise (Typetexp.Error (loc, env, Typetexp.Variant_tags (l1, l2)))
331+
raise_or_continue
332+
(Typetexp.Error (loc, env, Typetexp.Variant_tags (l1, l2)))
325333

326334
(* unification inside type_exp and type_expect *)
327335
let unify_exp_types ?type_clash_context loc env ty expected_ty =
328336
try unify env ty expected_ty with
329337
| Unify trace ->
330-
raise (Error (loc, env, Expr_type_clash (trace, type_clash_context)))
338+
raise_or_continue
339+
(Error (loc, env, Expr_type_clash (trace, type_clash_context)))
331340
| Tags (l1, l2) ->
332341
raise (Typetexp.Error (loc, env, Typetexp.Variant_tags (l1, l2)))
333342

@@ -345,11 +354,13 @@ let unify_pat_types_gadt loc env ty ty' =
345354
| Some x -> x
346355
in
347356
try unify_gadt ~newtype_level env ty ty' with
348-
| Unify trace -> raise (Error (loc, !env, Pattern_type_clash trace))
357+
| Unify trace ->
358+
raise_or_continue (Error (loc, !env, Pattern_type_clash trace))
349359
| Tags (l1, l2) ->
350-
raise (Typetexp.Error (loc, !env, Typetexp.Variant_tags (l1, l2)))
360+
raise_or_continue
361+
(Typetexp.Error (loc, !env, Typetexp.Variant_tags (l1, l2)))
351362
| Unification_recursive_abbrev trace ->
352-
raise (Error (loc, !env, Recursive_local_constraint trace))
363+
raise_or_continue (Error (loc, !env, Recursive_local_constraint trace))
353364

354365
(* Creating new conjunctive types is not allowed when typing patterns *)
355366

@@ -457,7 +468,8 @@ let enter_orpat_variables loc env p1_vs p2_vs =
457468
else (
458469
(try unify env t1 t2
459470
with Unify trace ->
460-
raise (Error (loc, env, Or_pattern_type_clash (x1, trace))));
471+
raise_or_continue
472+
(Error (loc, env, Or_pattern_type_clash (x1, trace))));
461473
(x2, x1) :: unify_vars rem1 rem2)
462474
| [], [] -> []
463475
| (x, _, _, _, _) :: _, [] -> raise (Error (loc, env, Orpat_vars (x, [])))
@@ -1931,7 +1943,8 @@ let rec type_approx env sexp =
19311943
let ty1 = approx_type env sty in
19321944
(try unify env ty ty1
19331945
with Unify trace ->
1934-
raise (Error (sexp.pexp_loc, env, Expr_type_clash (trace, None))));
1946+
raise_or_continue
1947+
(Error (sexp.pexp_loc, env, Expr_type_clash (trace, None))));
19351948
ty1
19361949
| Pexp_coerce (e, (), sty2) ->
19371950
let approx_ty_opt = function
@@ -1943,7 +1956,8 @@ let rec type_approx env sexp =
19431956
and ty2 = approx_type env sty2 in
19441957
(try unify env ty ty1
19451958
with Unify trace ->
1946-
raise (Error (sexp.pexp_loc, env, Expr_type_clash (trace, None))));
1959+
raise_or_continue
1960+
(Error (sexp.pexp_loc, env, Expr_type_clash (trace, None))));
19471961
ty2
19481962
| _ -> newvar ()
19491963
@@ -2266,11 +2280,6 @@ and type_expect ?type_clash_context ?in_function ?recarg env sexp ty_expected =
22662280
in
22672281
Cmt_format.set_saved_types
22682282
(Cmt_format.Partial_expression exp :: previous_saved_types);
2269-
2270-
(match get_first_delayed_error () with
2271-
| None -> ()
2272-
| Some e -> raise e);
2273-
22742283
exp
22752284
22762285
and type_expect_ ?type_clash_context ?in_function ?(recarg = Rejected) env sexp
@@ -2950,7 +2959,7 @@ and type_expect_ ?type_clash_context ?in_function ?(recarg = Rejected) env sexp
29502959
let gen = generalizable tv.level arg.exp_type in
29512960
(try unify_var env tv arg.exp_type
29522961
with Unify trace ->
2953-
raise
2962+
raise_or_continue
29542963
(Error
29552964
(arg.exp_loc, env, Expr_type_clash (trace, type_clash_context))));
29562965
gen)
@@ -3345,8 +3354,11 @@ and type_label_exp ?type_clash_context create env loc ty_expected
33453354
(* Generalize information merged from ty_expected *)
33463355
generalize_structure ty_arg);
33473356
if label.lbl_private = Private then
3348-
if create then raise (Error (loc, env, Private_type ty_expected))
3349-
else raise (Error (lid.loc, env, Private_label (lid.txt, ty_expected)));
3357+
if create then
3358+
raise_or_continue (Error (loc, env, Private_type ty_expected))
3359+
else
3360+
raise_or_continue
3361+
(Error (lid.loc, env, Private_label (lid.txt, ty_expected)));
33503362
let arg =
33513363
let snap = if vars = [] then None else Some (Btype.snapshot ()) in
33523364
let arg =
@@ -3559,11 +3571,8 @@ and type_application ?type_clash_context total_app env funct (sargs : sargs) :
35593571
| l, Some f ->
35603572
( l,
35613573
Some
3562-
(if !Clflags.editor_mode then (
3563-
try f ()
3564-
with e ->
3565-
add_delayed_error e;
3566-
tainted ())
3574+
(if !Clflags.editor_mode then
3575+
try f () with _ -> tainted ()
35673576
else f ()) ))
35683577
(List.rev args),
35693578
instance env (result_type omitted ty_fun) )

compiler/ml/typecore.mli

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ open Asttypes
1919
open Types
2020
open Format
2121

22+
val raise_delayed_error_if_exists : unit -> unit
23+
2224
val is_nonexpansive : Typedtree.expression -> bool
2325

2426
val type_binding :
@@ -105,6 +107,7 @@ type error =
105107
| Type_params_not_supported of Longident.t
106108
| Field_access_on_dict_type
107109
exception Error of Location.t * Env.t * error
110+
exception Errors of exn list
108111
exception Error_forward of Location.error
109112

110113
val report_error : Env.t -> formatter -> error -> unit

0 commit comments

Comments
 (0)