Skip to content

Remove err_depth #12167

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jun 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/compiler/compiler.ml
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ module Setup = struct
()
);
com.error_ext <- error_ext ctx;
com.error <- (fun ?(depth = 0) msg p -> com.error_ext (Error.make_error ~depth (Custom msg) p));
com.error <- (fun msg p -> com.error_ext (Error.make_error (Custom msg) p));
let filter_messages = (fun keep_errors predicate -> (List.filter (fun cm ->
(match cm.cm_severity with
| MessageSeverity.Error -> keep_errors;
Expand Down Expand Up @@ -447,7 +447,7 @@ with
ctx.has_error <- false;
ctx.messages <- [];
end else begin
let sub = List.map (fun p -> Error.make_error ~depth:1 (Error.Custom (Error.compl_msg "referenced here")) p) pl in
let sub = List.map (fun p -> Error.make_error (Error.Custom (Error.compl_msg "referenced here")) p) pl in
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)
end
| Error.Error err ->
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/messageReporting.ml
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ let get_formatter defines def default =

let print_error (err : Error.error) =
let ret = ref "" in
Error.recurse_error (fun depth err ->
Error.recurse_error (fun _ err ->
ret := !ret ^ (Lexer.get_error_pos (Printf.sprintf "%s:%d: ") err.err_pos) ^ (Error.error_msg err.err_message) ^ "\n"
) err;
!ret
Expand Down
7 changes: 4 additions & 3 deletions src/context/common.ml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*)
open Ast
open Type
open Error
open Globals
open Lookup
open Define
Expand Down Expand Up @@ -773,7 +774,7 @@ let create timer_ctx compilation_step cs version args display_mode =
info = (fun ?depth ?from_macro _ _ -> die "" __LOC__);
warning = (fun ?depth ?from_macro _ _ _ -> die "" __LOC__);
warning_options = [List.map (fun w -> {wo_warning = w;wo_mode = WMDisable}) WarningList.disabled_warnings];
error = (fun ?depth _ _ -> die "" __LOC__);
error = (fun _ _ -> die "" __LOC__);
error_ext = (fun _ -> die "" __LOC__);
get_messages = (fun() -> []);
filter_messages = (fun _ -> ());
Expand Down Expand Up @@ -1092,8 +1093,8 @@ let display_error_ext com err =
end else
com.error_ext err

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

let adapt_defines_to_macro_context defines =
let to_remove = "java" :: List.map Globals.platform_name Globals.platforms in
Expand Down
4 changes: 2 additions & 2 deletions src/context/typecore.ml
Original file line number Diff line number Diff line change
Expand Up @@ -384,8 +384,8 @@ let make_static_field_access c cf t p =
let ethis = Texpr.Builder.make_static_this c p in
mk (TField (ethis,(FStatic (c,cf)))) t p

let raise_with_type_error ?(depth = 0) msg p =
raise (WithTypeError (make_error ~depth (Custom msg) p))
let raise_with_type_error msg p =
raise (WithTypeError (make_error (Custom msg) p))

let raise_or_display ctx l p =
if ctx.f.untyped then ()
Expand Down
33 changes: 21 additions & 12 deletions src/core/error.ml
Original file line number Diff line number Diff line change
Expand Up @@ -28,30 +28,39 @@ and type_not_found_reason =
type error = {
err_message : error_msg;
err_pos : pos;
(* TODO Should probably be deprecated at some point and be derived from err_sub *)
err_depth : int;
(* Reverse list of sub errors. Use Error.recurse_error to handle an error and its sub errors with depth. *)
err_sub : error list;
err_from_macro : bool;
}

let make_error ?(depth = 0) ?(from_macro = false) ?(sub = []) msg p = {
type macro_error = {
msg : string;
pos : pos;
sub : macro_error list;
}

let make_error ?(from_macro = false) ?(sub = []) msg p = {
err_message = msg;
err_pos = p;
err_depth = depth;
err_from_macro = from_macro;
err_sub = sub;
}

let rec recurse_error ?(depth = 0) cb err =
let depth = if depth > 0 then depth else err.err_depth in
cb depth err;
List.iter (recurse_error ~depth:(depth+1) cb) (List.rev err.err_sub);
let rec convert_error (err:macro_error) =
let sub = List.rev_map convert_error err.sub in
make_error ~sub (Custom err.msg) err.pos

let recurse_error cb err =
let rec loop depth err =
cb depth err;
List.iter (loop (depth+1)) (List.rev err.err_sub)
in
loop 0 err

exception Fatal_error of error
exception Error of error

let abort ?(depth = 0) msg p = raise (Fatal_error (make_error ~depth (Custom msg) p))
let abort msg p = raise (Fatal_error (make_error (Custom msg) p))

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

(* Global error helpers *)
let raise_error err = raise (Error err)
let raise_error_msg ?(depth = 0) msg p = raise_error (make_error ~depth msg p)
let raise_msg ?(depth = 0) msg p = raise_error_msg ~depth (Custom msg) p
let raise_error_msg msg p = raise_error (make_error msg p)
let raise_msg msg p = raise_error_msg (Custom msg) p

let raise_typing_error ?(depth = 0) msg p = raise_msg ~depth msg p
let raise_typing_error msg p = raise_msg msg p
let raise_typing_error_ext err = raise_error err

let raise_std_not_found () =
Expand Down
2 changes: 1 addition & 1 deletion src/filters/safe/localStatic.ml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ let promote_local_static lsctx run v eo =
begin try
let cf = PMap.find name c.cl_statics in
raise_typing_error_ext (make_error (Custom (Printf.sprintf "The expanded name of this local (%s) conflicts with another static field" name)) ~sub:[
make_error ~depth:1 (Custom "Conflicting field was found here") cf.cf_name_pos
make_error (Custom "Conflicting field was found here") cf.cf_name_pos
] v.v_pos);
with Not_found ->
let cf = mk_field name ~static:true v.v_type v.v_pos v.v_pos in
Expand Down
4 changes: 2 additions & 2 deletions src/generators/gctx.ml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ type context_main = {
}

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

type t = {
platform : platform;
Expand Down Expand Up @@ -118,4 +118,4 @@ let get_es_version defines =
let map_source_header defines f =
match Define.defined_value_safe defines Define.SourceHeader with
| "" -> ()
| s -> f s
| s -> f s
4 changes: 1 addition & 3 deletions src/macro/eval/evalMain.ml
Original file line number Diff line number Diff line change
Expand Up @@ -423,10 +423,8 @@ let compiler_error (err : Error.error) =

| _ ->
let stack = ref [] in
let depth = err.err_depth + 1 in

List.iter (fun err ->
Error.recurse_error ~depth (fun depth err ->
Error.recurse_error (fun depth err ->
(* TODO indent child errors depending on depth *)
stack := make_runtime_error (Error.error_msg err.err_message) err.err_pos :: !stack;
) err;
Expand Down
33 changes: 21 additions & 12 deletions src/macro/macroApi.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ open Ast
open DisplayTypes.DisplayMode
open Type
open Common
open Error
open PlatformConfig
open DefineList
open MetaList
Expand Down Expand Up @@ -65,15 +66,14 @@ type 'value compiler_api = {
decode_type : 'value -> t;
info : ?depth:int -> string -> pos -> unit;
warning : ?depth:int -> Warning.warning -> string -> pos -> unit;
display_error : ?depth:int -> (string -> pos -> unit);
display_error : ?sub:macro_error list -> string -> pos -> unit;
with_imports : 'a . import list -> placed_name list list -> (unit -> 'a) -> 'a;
with_options : 'a . compiler_options -> (unit -> 'a) -> 'a;
exc_string : 'a . string -> 'a;
get_hxb_writer_config : unit -> 'value;
set_hxb_writer_config : 'value -> unit;
}


type enum_type =
| IExpr
| IEFieldKind
Expand Down Expand Up @@ -747,6 +747,15 @@ let decode_placed_name vp v =
let decode_opt_array f v =
if v = vnull then [] else List.map f (decode_array v)

let decode_sub_errors sub =
let rec decode_sub o =
let msg = decode_string (field o "msg") in
let pos = decode_pos (field o "pos") in
let sub = decode_opt_array decode_sub (field o "sub") in
{msg; pos; sub}
in
decode_opt_array decode_sub sub

(* Ast.placed_type_path *)
let rec decode_ast_path t =
let pack = List.map decode_string (decode_array (field t "pack"))
Expand Down Expand Up @@ -1800,24 +1809,24 @@ let macro_api ccom get_api =
"init_macros_done", vfun0 (fun () ->
vbool ((get_api()).init_macros_done ())
);
"error", vfun3 (fun msg p depth ->
"error", vfun3 (fun msg p sub ->
let msg = decode_string msg in
let p = decode_pos p in
let depth = decode_int depth in
(get_api()).display_error ~depth msg p;
let sub = decode_sub_errors sub in
(get_api()).display_error ~sub msg p;
raise Abort
);
"fatal_error", vfun3 (fun msg p depth ->
"fatal_error", vfun3 (fun msg p sub ->
let msg = decode_string msg in
let p = decode_pos p in
let depth = decode_int depth in
raise (Error.Fatal_error (Error.make_error ~depth (Custom msg) p))
let pos = decode_pos p in
let sub = decode_sub_errors sub in
raise (Error.Fatal_error (Error.convert_error {msg; pos; sub}))
);
"report_error", vfun3 (fun msg p depth ->
"report_error", vfun3 (fun msg p sub ->
let msg = decode_string msg in
let p = decode_pos p in
let depth = decode_int depth in
(get_api()).display_error ~depth msg p;
let sub = decode_sub_errors sub in
(get_api()).display_error ~sub msg p;
vnull
);
"warning", vfun3 (fun msg p depth ->
Expand Down
2 changes: 1 addition & 1 deletion src/optimization/analyzerTexpr.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1257,7 +1257,7 @@ module Purity = struct
apply_to_class c
with Purity_conflict(impure,p) ->
Error.raise_typing_error_ext (Error.make_error (Custom "Impure field overrides/implements field which was explicitly marked as @:pure") ~sub:[
Error.make_error ~depth:1 (Custom (Error.compl_msg "Pure field is here")) p
Error.make_error (Custom (Error.compl_msg "Pure field is here")) p
] impure.pn_field.cf_pos)
end
| _ -> ()
Expand Down
2 changes: 1 addition & 1 deletion src/optimization/inlineConstructors.ml
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ let inline_constructors (scom : SafeCom.t) original_e =
List.iter (fun v -> if v.v_id < 0 then cancel_v v p) io.io_dependent_vars;
if ioc.ioc_forced then begin
SafeCom.add_error scom (make_error (Custom "Forced inline constructor could not be inlined") ~sub:([
(make_error ~depth:1 (Custom (compl_msg "Cancellation happened here")) p)
(make_error (Custom (compl_msg "Cancellation happened here")) p)
]) io.io_pos);
end
| _ -> ()
Expand Down
7 changes: 3 additions & 4 deletions src/typing/callUnification.ml
Original file line number Diff line number Diff line change
Expand Up @@ -407,13 +407,12 @@ let unify_field_call ctx fa el_typed el p inline =
| None ->
let sub = List.fold_left (fun acc (cf,err) ->
(make_error
~depth:1 (* pretty much optional here *)
~sub:[err]
(Custom ("Overload resolution failed for " ^ (s_type (print_context()) cf.cf_type)))
p
) :: acc
) [] failures in
let sub = (make_error ~depth:1 (Custom "End of overload failure reasons") p) :: sub in
let sub = (make_error (Custom "End of overload failure reasons") p) :: sub in
raise_typing_error_ext (make_error ~sub (Custom "Could not find a suitable overload, reasons follow") p)
| Some err ->
raise_typing_error_ext err
Expand All @@ -428,7 +427,7 @@ let unify_field_call ctx fa el_typed el p inline =
| fcc :: l ->
let st = s_type (print_context()) in
let sub = List.map (fun fcc ->
make_error ~depth:1 (Custom (compl_msg (st fcc.fc_type))) fcc.fc_field.cf_name_pos
make_error (Custom (compl_msg (st fcc.fc_type))) fcc.fc_field.cf_name_pos
) (fcc :: l) in
display_error_ext ctx.com (make_error (Custom "Ambiguous overload, candidates follow") ~sub:(List.rev sub) p);
commit_delayed_display fcc
Expand Down Expand Up @@ -512,7 +511,7 @@ object(self)
if ep = null_pos then
old { err with err_pos = p }
else
old { err with err_sub = (make_error ~depth:(err.err_depth+1) (Custom (compl_msg "Called from macro here")) p) :: err.err_sub }
old { err with err_sub = (make_error (Custom (compl_msg "Called from macro here")) p) :: err.err_sub }
end else
old err;
);
Expand Down
2 changes: 1 addition & 1 deletion src/typing/fields.ml
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ let field_access ctx mode f fh e pfield =
(match e.eexpr with TLocal _ when Common.defined ctx.com Define.Haxe3Compat -> warning ctx WTemp "Field set has changed here in Haxe 4: call setter explicitly to keep Haxe 3.x behaviour" pfield | _ -> ());
if not (is_physical_field f) then begin
display_error_ext ctx.com (make_error (Custom "This field cannot be accessed because it is not a real variable") ~sub:[
make_error ~depth:1 (Custom "Add @:isVar here to enable it") f.cf_pos
make_error (Custom "Add @:isVar here to enable it") f.cf_pos
] pfield);
end;
normal false
Expand Down
2 changes: 1 addition & 1 deletion src/typing/forLoop.ml
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ module IterationKind = struct
| Some e -> e
| None ->
if resume then raise Not_found;
display_error_ext ctx.com (make_error ~depth:err.err_depth ~sub:[err] (Custom "Field iterator has an invalid type") acc_expr.epos);
display_error_ext ctx.com (make_error ~sub:[err] (Custom "Field iterator has an invalid type") acc_expr.epos);
mk (TConst TNull) t_dynamic p
)
in
Expand Down
13 changes: 6 additions & 7 deletions src/typing/generic.ml
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ let build_generic_class ctx c p tl =
begin match cf_old.cf_kind with
| Method _ when not (has_class_flag c CInterface) && not (has_class_flag c CExtern) && not (has_class_field_flag cf_old CfAbstract) ->
display_error_ext ctx.com (make_error (Custom (Printf.sprintf "Field %s has no expression (possible typing order issue)" cf_new.cf_name)) ~sub:([
(make_error ~depth:1 (Custom (compl_msg (Printf.sprintf "While building %s" (s_type_path cg.cl_path)))) p)
(make_error (Custom (compl_msg (Printf.sprintf "While building %s" (s_type_path cg.cl_path)))) p)
]) cf_new.cf_pos);
| _ ->
()
Expand Down Expand Up @@ -459,10 +459,9 @@ let type_generic_function ctx fa fcc with_type p =
let params = extract_type_parameters monos in
let unify_existing_field tcf pcf = try
unify_raise tcf fc_type p
with Error ({ err_message = Unify _; err_depth = depth } as err) ->
with Error ({ err_message = Unify _ } as err) ->
raise (Error { err with err_sub = (make_error
~depth
~sub:[make_error ~depth:(depth+1) (Custom (compl_msg "Conflicting field was defined here")) pcf]
~sub:[make_error (Custom (compl_msg "Conflicting field was defined here")) pcf]
(Custom ("Cannot create field " ^ name ^ " due to type mismatch"))
p
) :: err.err_sub })
Expand Down Expand Up @@ -492,7 +491,7 @@ let type_generic_function ctx fa fcc with_type p =
let rec check e = match e.eexpr with
| TNew({cl_kind = KTypeParameter _} as c,_,_) when not (TypeloadCheck.is_generic_parameter ctx c) ->
display_error_ext ctx.com (make_error (Custom "Only generic type parameters can be constructed") ~sub:([
(make_error ~depth:1 (Custom (compl_msg "While specializing this call")) p)
(make_error (Custom (compl_msg "While specializing this call")) p)
]) e.epos);
| _ ->
Type.iter check e
Expand All @@ -504,8 +503,8 @@ let type_generic_function ctx fa fcc with_type p =
Printf.sprintf "%s = %s" ttp.ttp_name (st t)
) cf.cf_params monos in
let sub = [
(Error.make_error ~depth:1 (Custom (Printf.sprintf "Mapping: %s" (String.concat ", " mappings))) p);
(Error.make_error ~depth:1 (Custom (Printf.sprintf "For function %s.%s" (s_type_path c.cl_path) cf.cf_name)) p);
(Error.make_error (Custom (Printf.sprintf "Mapping: %s" (String.concat ", " mappings))) p);
(Error.make_error (Custom (Printf.sprintf "For function %s.%s" (s_type_path c.cl_path) cf.cf_name)) p);
] in
display_error_ext ctx.com (Error.make_error ~sub (Custom "Recursive @:generic function") p); None;
| Some e ->
Expand Down
4 changes: 2 additions & 2 deletions src/typing/macroContext.ml
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,7 @@ let make_macro_api ctx mctx p =
let m = ctx.com.module_lut#find mpath in
let pos = { pfile = (Path.UniqueKey.lazy_path m.m_extra.m_file); pmin = 0; pmax = 0 } in
Interp.compiler_error (make_error ~sub:[
make_error ~depth:1 (Custom "Previously defined here") pos
make_error (Custom "Previously defined here") pos
] (Custom (Printf.sprintf "Cannot redefine module %s" (s_type_path mpath))) p);
with Not_found ->
ctx.com.cs#taint_module mpath DefineType;
Expand Down Expand Up @@ -506,7 +506,7 @@ let make_macro_api ctx mctx p =
if m != ctx.m.curmod then begin
let pos = { pfile = (Path.UniqueKey.lazy_path m.m_extra.m_file); pmin = 0; pmax = 0 } in
Interp.compiler_error (make_error ~sub:[
make_error ~depth:1 (Custom "Previously defined here") pos
make_error (Custom "Previously defined here") pos
] (Custom (Printf.sprintf "Cannot redefine module %s" (s_type_path mpath))) p);
end else
ignore(TypeloadModule.type_types_into_module ctx.com ctx.g ctx.m.curmod types pos)
Expand Down
4 changes: 2 additions & 2 deletions src/typing/operators.ml
Original file line number Diff line number Diff line change
Expand Up @@ -421,13 +421,13 @@ let find_abstract_binop_overload ctx op e1 e2 a c tl left is_assign_op p =
let t_expected = BinopResult.get_type result in
begin try
unify_raise tret t_expected p
with Error { err_message = Unify _; err_depth = depth } ->
with Error { err_message = Unify _ } ->
match follow tret with
| TAbstract(a,tl) when type_iseq (Abstract.get_underlying_type a tl) t_expected ->
()
| _ ->
let st = s_type (print_context()) in
raise_typing_error ~depth (Printf.sprintf "The result of this operation (%s) is not compatible with declared return type %s" (st t_expected) (st tret)) p
raise_typing_error (Printf.sprintf "The result of this operation (%s) is not compatible with declared return type %s" (st t_expected) (st tret)) p
end;
end;
(*
Expand Down
Loading
Loading