Skip to content

Commit b6a2db6

Browse files
SimnkLabz
andauthored
Remove err_depth (#12167)
* remove err_depth * Update macro API to handle sub errors * err_sub is a reversed list of sub errors * Add test * Update test --------- Co-authored-by: Rudy Ges <[email protected]>
1 parent 8a5ddf5 commit b6a2db6

27 files changed

+140
-85
lines changed

src/compiler/compiler.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ module Setup = struct
252252
()
253253
);
254254
com.error_ext <- error_ext ctx;
255-
com.error <- (fun ?(depth = 0) msg p -> com.error_ext (Error.make_error ~depth (Custom msg) p));
255+
com.error <- (fun msg p -> com.error_ext (Error.make_error (Custom msg) p));
256256
let filter_messages = (fun keep_errors predicate -> (List.filter (fun cm ->
257257
(match cm.cm_severity with
258258
| MessageSeverity.Error -> keep_errors;
@@ -447,7 +447,7 @@ with
447447
ctx.has_error <- false;
448448
ctx.messages <- [];
449449
end else begin
450-
let sub = List.map (fun p -> Error.make_error ~depth:1 (Error.Custom (Error.compl_msg "referenced here")) p) pl in
450+
let sub = List.map (fun p -> Error.make_error (Error.Custom (Error.compl_msg "referenced here")) p) pl in
451451
error_ext ctx (Error.make_error (Error.Custom (Printf.sprintf "You cannot access the %s package while %s (for %s)" pack (if pf = "macro" then "in a macro" else "targeting " ^ pf) (s_type_path m))) ~sub p)
452452
end
453453
| Error.Error err ->

src/compiler/messageReporting.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ let get_formatter defines def default =
340340

341341
let print_error (err : Error.error) =
342342
let ret = ref "" in
343-
Error.recurse_error (fun depth err ->
343+
Error.recurse_error (fun _ err ->
344344
ret := !ret ^ (Lexer.get_error_pos (Printf.sprintf "%s:%d: ") err.err_pos) ^ (Error.error_msg err.err_message) ^ "\n"
345345
) err;
346346
!ret

src/context/common.ml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
*)
1919
open Ast
2020
open Type
21+
open Error
2122
open Globals
2223
open Lookup
2324
open Define
@@ -773,7 +774,7 @@ let create timer_ctx compilation_step cs version args display_mode =
773774
info = (fun ?depth ?from_macro _ _ -> die "" __LOC__);
774775
warning = (fun ?depth ?from_macro _ _ _ -> die "" __LOC__);
775776
warning_options = [List.map (fun w -> {wo_warning = w;wo_mode = WMDisable}) WarningList.disabled_warnings];
776-
error = (fun ?depth _ _ -> die "" __LOC__);
777+
error = (fun _ _ -> die "" __LOC__);
777778
error_ext = (fun _ -> die "" __LOC__);
778779
get_messages = (fun() -> []);
779780
filter_messages = (fun _ -> ());
@@ -1092,8 +1093,8 @@ let display_error_ext com err =
10921093
end else
10931094
com.error_ext err
10941095

1095-
let display_error com ?(depth = 0) msg p =
1096-
display_error_ext com (Error.make_error ~depth (Custom msg) p)
1096+
let display_error com ?(sub:macro_error list = []) msg pos =
1097+
display_error_ext com (convert_error {msg; pos; sub})
10971098

10981099
let adapt_defines_to_macro_context defines =
10991100
let to_remove = "java" :: List.map Globals.platform_name Globals.platforms in

src/context/typecore.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -384,8 +384,8 @@ let make_static_field_access c cf t p =
384384
let ethis = Texpr.Builder.make_static_this c p in
385385
mk (TField (ethis,(FStatic (c,cf)))) t p
386386

387-
let raise_with_type_error ?(depth = 0) msg p =
388-
raise (WithTypeError (make_error ~depth (Custom msg) p))
387+
let raise_with_type_error msg p =
388+
raise (WithTypeError (make_error (Custom msg) p))
389389

390390
let raise_or_display ctx l p =
391391
if ctx.f.untyped then ()

src/core/error.ml

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,30 +28,39 @@ and type_not_found_reason =
2828
type error = {
2929
err_message : error_msg;
3030
err_pos : pos;
31-
(* TODO Should probably be deprecated at some point and be derived from err_sub *)
32-
err_depth : int;
3331
(* Reverse list of sub errors. Use Error.recurse_error to handle an error and its sub errors with depth. *)
3432
err_sub : error list;
3533
err_from_macro : bool;
3634
}
3735

38-
let make_error ?(depth = 0) ?(from_macro = false) ?(sub = []) msg p = {
36+
type macro_error = {
37+
msg : string;
38+
pos : pos;
39+
sub : macro_error list;
40+
}
41+
42+
let make_error ?(from_macro = false) ?(sub = []) msg p = {
3943
err_message = msg;
4044
err_pos = p;
41-
err_depth = depth;
4245
err_from_macro = from_macro;
4346
err_sub = sub;
4447
}
4548

46-
let rec recurse_error ?(depth = 0) cb err =
47-
let depth = if depth > 0 then depth else err.err_depth in
48-
cb depth err;
49-
List.iter (recurse_error ~depth:(depth+1) cb) (List.rev err.err_sub);
49+
let rec convert_error (err:macro_error) =
50+
let sub = List.rev_map convert_error err.sub in
51+
make_error ~sub (Custom err.msg) err.pos
52+
53+
let recurse_error cb err =
54+
let rec loop depth err =
55+
cb depth err;
56+
List.iter (loop (depth+1)) (List.rev err.err_sub)
57+
in
58+
loop 0 err
5059

5160
exception Fatal_error of error
5261
exception Error of error
5362

54-
let abort ?(depth = 0) msg p = raise (Fatal_error (make_error ~depth (Custom msg) p))
63+
let abort msg p = raise (Fatal_error (make_error (Custom msg) p))
5564

5665
let string_source t = match follow t with
5766
| TInst(c,tl) -> PMap.foldi (fun s _ acc -> s :: acc) (TClass.get_all_fields c tl) []
@@ -320,10 +329,10 @@ and s_call_error = function
320329

321330
(* Global error helpers *)
322331
let raise_error err = raise (Error err)
323-
let raise_error_msg ?(depth = 0) msg p = raise_error (make_error ~depth msg p)
324-
let raise_msg ?(depth = 0) msg p = raise_error_msg ~depth (Custom msg) p
332+
let raise_error_msg msg p = raise_error (make_error msg p)
333+
let raise_msg msg p = raise_error_msg (Custom msg) p
325334

326-
let raise_typing_error ?(depth = 0) msg p = raise_msg ~depth msg p
335+
let raise_typing_error msg p = raise_msg msg p
327336
let raise_typing_error_ext err = raise_error err
328337

329338
let raise_std_not_found () =

src/filters/safe/localStatic.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ let promote_local_static lsctx run v eo =
1414
begin try
1515
let cf = PMap.find name c.cl_statics in
1616
raise_typing_error_ext (make_error (Custom (Printf.sprintf "The expanded name of this local (%s) conflicts with another static field" name)) ~sub:[
17-
make_error ~depth:1 (Custom "Conflicting field was found here") cf.cf_name_pos
17+
make_error (Custom "Conflicting field was found here") cf.cf_name_pos
1818
] v.v_pos);
1919
with Not_found ->
2020
let cf = mk_field name ~static:true v.v_type v.v_pos v.v_pos in

src/generators/gctx.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ type context_main = {
99
}
1010

1111
type warning_function = ?depth:int -> ?from_macro:bool -> warning -> warning_option list list -> string -> pos -> unit
12-
type error_function = ?depth:int -> string -> pos -> unit
12+
type error_function = string -> pos -> unit
1313

1414
type t = {
1515
platform : platform;
@@ -118,4 +118,4 @@ let get_es_version defines =
118118
let map_source_header defines f =
119119
match Define.defined_value_safe defines Define.SourceHeader with
120120
| "" -> ()
121-
| s -> f s
121+
| s -> f s

src/macro/eval/evalMain.ml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -423,10 +423,8 @@ let compiler_error (err : Error.error) =
423423

424424
| _ ->
425425
let stack = ref [] in
426-
let depth = err.err_depth + 1 in
427-
428426
List.iter (fun err ->
429-
Error.recurse_error ~depth (fun depth err ->
427+
Error.recurse_error (fun depth err ->
430428
(* TODO indent child errors depending on depth *)
431429
stack := make_runtime_error (Error.error_msg err.err_message) err.err_pos :: !stack;
432430
) err;

src/macro/macroApi.ml

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ open Ast
22
open DisplayTypes.DisplayMode
33
open Type
44
open Common
5+
open Error
56
open PlatformConfig
67
open DefineList
78
open MetaList
@@ -65,15 +66,14 @@ type 'value compiler_api = {
6566
decode_type : 'value -> t;
6667
info : ?depth:int -> string -> pos -> unit;
6768
warning : ?depth:int -> Warning.warning -> string -> pos -> unit;
68-
display_error : ?depth:int -> (string -> pos -> unit);
69+
display_error : ?sub:macro_error list -> string -> pos -> unit;
6970
with_imports : 'a . import list -> placed_name list list -> (unit -> 'a) -> 'a;
7071
with_options : 'a . compiler_options -> (unit -> 'a) -> 'a;
7172
exc_string : 'a . string -> 'a;
7273
get_hxb_writer_config : unit -> 'value;
7374
set_hxb_writer_config : 'value -> unit;
7475
}
7576

76-
7777
type enum_type =
7878
| IExpr
7979
| IEFieldKind
@@ -747,6 +747,15 @@ let decode_placed_name vp v =
747747
let decode_opt_array f v =
748748
if v = vnull then [] else List.map f (decode_array v)
749749

750+
let decode_sub_errors sub =
751+
let rec decode_sub o =
752+
let msg = decode_string (field o "msg") in
753+
let pos = decode_pos (field o "pos") in
754+
let sub = decode_opt_array decode_sub (field o "sub") in
755+
{msg; pos; sub}
756+
in
757+
decode_opt_array decode_sub sub
758+
750759
(* Ast.placed_type_path *)
751760
let rec decode_ast_path t =
752761
let pack = List.map decode_string (decode_array (field t "pack"))
@@ -1800,24 +1809,24 @@ let macro_api ccom get_api =
18001809
"init_macros_done", vfun0 (fun () ->
18011810
vbool ((get_api()).init_macros_done ())
18021811
);
1803-
"error", vfun3 (fun msg p depth ->
1812+
"error", vfun3 (fun msg p sub ->
18041813
let msg = decode_string msg in
18051814
let p = decode_pos p in
1806-
let depth = decode_int depth in
1807-
(get_api()).display_error ~depth msg p;
1815+
let sub = decode_sub_errors sub in
1816+
(get_api()).display_error ~sub msg p;
18081817
raise Abort
18091818
);
1810-
"fatal_error", vfun3 (fun msg p depth ->
1819+
"fatal_error", vfun3 (fun msg p sub ->
18111820
let msg = decode_string msg in
1812-
let p = decode_pos p in
1813-
let depth = decode_int depth in
1814-
raise (Error.Fatal_error (Error.make_error ~depth (Custom msg) p))
1821+
let pos = decode_pos p in
1822+
let sub = decode_sub_errors sub in
1823+
raise (Error.Fatal_error (Error.convert_error {msg; pos; sub}))
18151824
);
1816-
"report_error", vfun3 (fun msg p depth ->
1825+
"report_error", vfun3 (fun msg p sub ->
18171826
let msg = decode_string msg in
18181827
let p = decode_pos p in
1819-
let depth = decode_int depth in
1820-
(get_api()).display_error ~depth msg p;
1828+
let sub = decode_sub_errors sub in
1829+
(get_api()).display_error ~sub msg p;
18211830
vnull
18221831
);
18231832
"warning", vfun3 (fun msg p depth ->

src/optimization/analyzerTexpr.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1257,7 +1257,7 @@ module Purity = struct
12571257
apply_to_class c
12581258
with Purity_conflict(impure,p) ->
12591259
Error.raise_typing_error_ext (Error.make_error (Custom "Impure field overrides/implements field which was explicitly marked as @:pure") ~sub:[
1260-
Error.make_error ~depth:1 (Custom (Error.compl_msg "Pure field is here")) p
1260+
Error.make_error (Custom (Error.compl_msg "Pure field is here")) p
12611261
] impure.pn_field.cf_pos)
12621262
end
12631263
| _ -> ()

0 commit comments

Comments
 (0)