From a64fdc8b1be70de43afe35ff788ba3240318daac Mon Sep 17 00:00:00 2001 From: Escherichia Date: Tue, 12 Mar 2024 15:02:17 +0100 Subject: WIP Beginning working on better errors: began replacing raise (Failure) and assert by craise and cassert. Does not compile yet, still need to propagate the meta variable where it's relevant --- compiler/Errors.ml | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 compiler/Errors.ml (limited to 'compiler/Errors.ml') diff --git a/compiler/Errors.ml b/compiler/Errors.ml new file mode 100644 index 00000000..65c2cbb0 --- /dev/null +++ b/compiler/Errors.ml @@ -0,0 +1,27 @@ +let meta_to_string (span : Meta.span ) = + let file = match span.file with Virtual s | Local s -> s in + let loc_to_string (l : Meta.loc) : string = + string_of_int l.line ^ ":" ^ string_of_int l.col + in + "Source: '" ^ file ^ "', lines " ^ loc_to_string span.beg_loc ^ "-" + ^ loc_to_string span.end_loc + +let format_error_message (meta : Meta.meta) msg = + msg ^ ":" ^ meta_to_string meta.span + +exception CFailure of string + + +let error_list : (Meta.meta * string) list ref = ref [] +let save_error (meta : Meta.meta ) (msg : string) = error_list := (meta, msg)::(!error_list) + +let craise (meta : Meta.meta) (msg : string) = + if !Config.fail_hard then + raise (Failure (format_error_message meta msg)) + else + let () = save_error meta msg in + raise (CFailure msg) + +let cassert (b : bool) (meta : Meta.meta) (msg : string) = + if b then + craise meta msg \ No newline at end of file -- cgit v1.2.3 From 5209cea7012cfa3b39a5a289e65e2ea5e166d730 Mon Sep 17 00:00:00 2001 From: Escherichia Date: Thu, 21 Mar 2024 12:34:40 +0100 Subject: WIP: translate.ml and extract.ml do not compile. Some assert left to do and we need to see how translate_crate can give meta to the functions it calls --- compiler/Errors.ml | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) (limited to 'compiler/Errors.ml') diff --git a/compiler/Errors.ml b/compiler/Errors.ml index 65c2cbb0..8fb65bc1 100644 --- a/compiler/Errors.ml +++ b/compiler/Errors.ml @@ -12,16 +12,31 @@ let format_error_message (meta : Meta.meta) msg = exception CFailure of string -let error_list : (Meta.meta * string) list ref = ref [] -let save_error (meta : Meta.meta ) (msg : string) = error_list := (meta, msg)::(!error_list) +let error_list : (Meta.meta option * string) list ref = ref [] +let save_error (meta : Meta.meta option) (msg : string) = error_list := (meta, msg)::(!error_list) let craise (meta : Meta.meta) (msg : string) = if !Config.fail_hard then raise (Failure (format_error_message meta msg)) else - let () = save_error meta msg in + let () = save_error (Some meta) msg in raise (CFailure msg) let cassert (b : bool) (meta : Meta.meta) (msg : string) = if b then - craise meta msg \ No newline at end of file + craise meta msg + +let craise_opt_meta (meta : Meta.meta option) (msg : string) = + match meta with + | Some m -> craise m msg + | None -> + let () = save_error (None) msg in + raise (CFailure msg) + +let cassert_opt_meta (b : bool) (meta : Meta.meta option) (msg : string) = + match meta with + | Some m -> cassert b m msg + | None -> + if b then + let () = save_error (None) msg in + raise (CFailure msg) \ No newline at end of file -- cgit v1.2.3 From 0f0082c81db8852dff23cd4691af19c434c8be78 Mon Sep 17 00:00:00 2001 From: Escherichia Date: Wed, 27 Mar 2024 10:22:06 +0100 Subject: Added sanity_check and sanity_check_opt_meta helpers and changed sanity checks related cassert to these helpers to have a proper error message --- compiler/Errors.ml | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) (limited to 'compiler/Errors.ml') diff --git a/compiler/Errors.ml b/compiler/Errors.ml index 8fb65bc1..4c51720f 100644 --- a/compiler/Errors.ml +++ b/compiler/Errors.ml @@ -39,4 +39,10 @@ let cassert_opt_meta (b : bool) (meta : Meta.meta option) (msg : string) = | None -> if b then let () = save_error (None) msg in - raise (CFailure msg) \ No newline at end of file + raise (CFailure msg) + +let sanity_check b meta = cassert b meta "Internal error, please file an issue" +let sanity_check_opt_meta b meta = cassert_opt_meta b meta "Internal error, please file an issue" + +let exec_raise = craise +let exec_assert = cassert \ No newline at end of file -- cgit v1.2.3 From 5ad671a0960692af1c00609fa6864c6f44ca299c Mon Sep 17 00:00:00 2001 From: Escherichia Date: Thu, 28 Mar 2024 13:56:31 +0100 Subject: Should answer all comments, there are still some TODO: error message left --- compiler/Errors.ml | 50 ++++++++++++++++++++++++++++++-------------------- 1 file changed, 30 insertions(+), 20 deletions(-) (limited to 'compiler/Errors.ml') diff --git a/compiler/Errors.ml b/compiler/Errors.ml index 4c51720f..aff62022 100644 --- a/compiler/Errors.ml +++ b/compiler/Errors.ml @@ -11,38 +11,48 @@ let format_error_message (meta : Meta.meta) msg = exception CFailure of string - let error_list : (Meta.meta option * string) list ref = ref [] -let save_error (meta : Meta.meta option) (msg : string) = error_list := (meta, msg)::(!error_list) - -let craise (meta : Meta.meta) (msg : string) = - if !Config.fail_hard then - raise (Failure (format_error_message meta msg)) - else - let () = save_error (Some meta) msg in - raise (CFailure msg) +let push_error (meta : Meta.meta option) (msg : string) = error_list := (meta, msg)::(!error_list) -let cassert (b : bool) (meta : Meta.meta) (msg : string) = - if b then - craise meta msg +let save_error ?(b : bool = true) (meta : Meta.meta option) (msg : string) = + push_error meta msg; + match meta with + | Some m -> + if !Config.fail_hard && b then + raise (Failure (format_error_message m msg)) + | None -> + if !Config.fail_hard && b then + raise (Failure msg) let craise_opt_meta (meta : Meta.meta option) (msg : string) = match meta with - | Some m -> craise m msg + | Some m -> + if !Config.fail_hard then + raise (Failure (format_error_message m msg)) + else + let () = push_error (Some m) msg in + raise (CFailure msg) | None -> - let () = save_error (None) msg in + if !Config.fail_hard then + raise (Failure msg) + else + let () = push_error None msg in raise (CFailure msg) +let craise (meta : Meta.meta) (msg : string) = + craise_opt_meta (Some meta) msg + let cassert_opt_meta (b : bool) (meta : Meta.meta option) (msg : string) = - match meta with - | Some m -> cassert b m msg - | None -> - if b then - let () = save_error (None) msg in - raise (CFailure msg) + if b then + craise_opt_meta meta msg + +let cassert (b : bool) (meta : Meta.meta) (msg : string) = + cassert_opt_meta b (Some meta) msg let sanity_check b meta = cassert b meta "Internal error, please file an issue" let sanity_check_opt_meta b meta = cassert_opt_meta b meta "Internal error, please file an issue" +let internal_error meta = craise meta "Internal error, please report an issue" + let exec_raise = craise let exec_assert = cassert \ No newline at end of file -- cgit v1.2.3 From 64666edb3c10cd42e15937ac4038b83def630e35 Mon Sep 17 00:00:00 2001 From: Escherichia Date: Thu, 28 Mar 2024 17:14:27 +0100 Subject: formatting --- compiler/Errors.ml | 59 ++++++++++++++++++++++++++---------------------------- 1 file changed, 28 insertions(+), 31 deletions(-) (limited to 'compiler/Errors.ml') diff --git a/compiler/Errors.ml b/compiler/Errors.ml index aff62022..3b34397a 100644 --- a/compiler/Errors.ml +++ b/compiler/Errors.ml @@ -1,10 +1,10 @@ -let meta_to_string (span : Meta.span ) = +let meta_to_string (span : Meta.span) = let file = match span.file with Virtual s | Local s -> s in let loc_to_string (l : Meta.loc) : string = string_of_int l.line ^ ":" ^ string_of_int l.col in - "Source: '" ^ file ^ "', lines " ^ loc_to_string span.beg_loc ^ "-" - ^ loc_to_string span.end_loc + "Source: '" ^ file ^ "', lines " ^ loc_to_string span.beg_loc ^ "-" + ^ loc_to_string span.end_loc let format_error_message (meta : Meta.meta) msg = msg ^ ":" ^ meta_to_string meta.span @@ -12,47 +12,44 @@ let format_error_message (meta : Meta.meta) msg = exception CFailure of string let error_list : (Meta.meta option * string) list ref = ref [] -let push_error (meta : Meta.meta option) (msg : string) = error_list := (meta, msg)::(!error_list) -let save_error ?(b : bool = true) (meta : Meta.meta option) (msg : string) = +let push_error (meta : Meta.meta option) (msg : string) = + error_list := (meta, msg) :: !error_list + +let save_error ?(b : bool = true) (meta : Meta.meta option) (msg : string) = push_error meta msg; - match meta with + match meta with | Some m -> - if !Config.fail_hard && b then - raise (Failure (format_error_message m msg)) - | None -> - if !Config.fail_hard && b then - raise (Failure msg) + if !Config.fail_hard && b then + raise (Failure (format_error_message m msg)) + | None -> if !Config.fail_hard && b then raise (Failure msg) let craise_opt_meta (meta : Meta.meta option) (msg : string) = - match meta with + match meta with | Some m -> - if !Config.fail_hard then - raise (Failure (format_error_message m msg)) - else - let () = push_error (Some m) msg in - raise (CFailure msg) - | None -> - if !Config.fail_hard then - raise (Failure msg) - else - let () = push_error None msg in - raise (CFailure msg) - -let craise (meta : Meta.meta) (msg : string) = - craise_opt_meta (Some meta) msg + if !Config.fail_hard then raise (Failure (format_error_message m msg)) + else + let () = push_error (Some m) msg in + raise (CFailure msg) + | None -> + if !Config.fail_hard then raise (Failure msg) + else + let () = push_error None msg in + raise (CFailure msg) + +let craise (meta : Meta.meta) (msg : string) = craise_opt_meta (Some meta) msg let cassert_opt_meta (b : bool) (meta : Meta.meta option) (msg : string) = - if b then - craise_opt_meta meta msg + if b then craise_opt_meta meta msg let cassert (b : bool) (meta : Meta.meta) (msg : string) = cassert_opt_meta b (Some meta) msg let sanity_check b meta = cassert b meta "Internal error, please file an issue" -let sanity_check_opt_meta b meta = cassert_opt_meta b meta "Internal error, please file an issue" -let internal_error meta = craise meta "Internal error, please report an issue" +let sanity_check_opt_meta b meta = + cassert_opt_meta b meta "Internal error, please file an issue" +let internal_error meta = craise meta "Internal error, please report an issue" let exec_raise = craise -let exec_assert = cassert \ No newline at end of file +let exec_assert = cassert -- cgit v1.2.3 From c2069900aa534d49b6c07eea8f5ab2fb70a26aa2 Mon Sep 17 00:00:00 2001 From: Son Ho Date: Thu, 28 Mar 2024 17:25:54 +0100 Subject: Fix an issue --- compiler/Errors.ml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'compiler/Errors.ml') diff --git a/compiler/Errors.ml b/compiler/Errors.ml index 3b34397a..6057362e 100644 --- a/compiler/Errors.ml +++ b/compiler/Errors.ml @@ -40,7 +40,7 @@ let craise_opt_meta (meta : Meta.meta option) (msg : string) = let craise (meta : Meta.meta) (msg : string) = craise_opt_meta (Some meta) msg let cassert_opt_meta (b : bool) (meta : Meta.meta option) (msg : string) = - if b then craise_opt_meta meta msg + if not b then craise_opt_meta meta msg let cassert (b : bool) (meta : Meta.meta) (msg : string) = cassert_opt_meta b (Some meta) msg -- cgit v1.2.3 From 6f4833f84dd3ec17311b5e6ca9f5c1ad94ff7564 Mon Sep 17 00:00:00 2001 From: Son Ho Date: Fri, 29 Mar 2024 11:31:47 +0100 Subject: Improve the error messages --- compiler/Errors.ml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'compiler/Errors.ml') diff --git a/compiler/Errors.ml b/compiler/Errors.ml index 6057362e..68073ef7 100644 --- a/compiler/Errors.ml +++ b/compiler/Errors.ml @@ -7,7 +7,7 @@ let meta_to_string (span : Meta.span) = ^ loc_to_string span.end_loc let format_error_message (meta : Meta.meta) msg = - msg ^ ":" ^ meta_to_string meta.span + msg ^ "\n" ^ meta_to_string meta.span exception CFailure of string -- cgit v1.2.3 From 786c54c01ea98580374638c0ed92d19dfae19b1f Mon Sep 17 00:00:00 2001 From: Escherichia Date: Fri, 29 Mar 2024 13:21:08 +0100 Subject: added file and line arg to craise and cassert --- compiler/Errors.ml | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) (limited to 'compiler/Errors.ml') diff --git a/compiler/Errors.ml b/compiler/Errors.ml index 68073ef7..bfdf5796 100644 --- a/compiler/Errors.ml +++ b/compiler/Errors.ml @@ -13,43 +13,43 @@ exception CFailure of string let error_list : (Meta.meta option * string) list ref = ref [] -let push_error (meta : Meta.meta option) (msg : string) = - error_list := (meta, msg) :: !error_list +let push_error (file : string) (line : int) (meta : Meta.meta option) (msg : string) = + error_list := (meta, msg ^ "\n In file:" ^ file ^ "\n Line:" ^ string_of_int line) :: !error_list -let save_error ?(b : bool = true) (meta : Meta.meta option) (msg : string) = - push_error meta msg; +let save_error (file : string) (line : int) ?(b : bool = true) (meta : Meta.meta option) (msg : string) = + push_error file line meta msg; match meta with | Some m -> if !Config.fail_hard && b then - raise (Failure (format_error_message m msg)) + raise (Failure (format_error_message m (msg ^ "\n In file:" ^ file ^ "\n Line:" ^ string_of_int line))) | None -> if !Config.fail_hard && b then raise (Failure msg) -let craise_opt_meta (meta : Meta.meta option) (msg : string) = +let craise_opt_meta (file : string) (line : int) (meta : Meta.meta option) (msg : string) = match meta with | Some m -> - if !Config.fail_hard then raise (Failure (format_error_message m msg)) + if !Config.fail_hard then raise (Failure (format_error_message m (msg ^ "\n In file:" ^ file ^ "\n Line:" ^ string_of_int line))) else - let () = push_error (Some m) msg in + let () = push_error file line (Some m) msg in raise (CFailure msg) | None -> - if !Config.fail_hard then raise (Failure msg) + if !Config.fail_hard then raise (Failure (msg ^ "\n In file:" ^ file ^ "\n Line:" ^ string_of_int line)) else - let () = push_error None msg in + let () = push_error file line None msg in raise (CFailure msg) -let craise (meta : Meta.meta) (msg : string) = craise_opt_meta (Some meta) msg +let craise (file : string) (line : int) (meta : Meta.meta) (msg : string) = craise_opt_meta file line (Some meta) msg -let cassert_opt_meta (b : bool) (meta : Meta.meta option) (msg : string) = - if not b then craise_opt_meta meta msg +let cassert_opt_meta (file : string) (line : int) (b : bool) (meta : Meta.meta option) (msg : string) = + if not b then craise_opt_meta file line meta msg -let cassert (b : bool) (meta : Meta.meta) (msg : string) = - cassert_opt_meta b (Some meta) msg +let cassert (file : string) (line : int) (b : bool) (meta : Meta.meta) (msg : string) = + cassert_opt_meta file line b (Some meta) msg -let sanity_check b meta = cassert b meta "Internal error, please file an issue" +let sanity_check (file : string) (line : int) b meta = cassert file line b meta "Internal error, please file an issue" -let sanity_check_opt_meta b meta = - cassert_opt_meta b meta "Internal error, please file an issue" +let sanity_check_opt_meta (file : string) (line : int) b meta = + cassert_opt_meta file line b meta "Internal error, please file an issue" -let internal_error meta = craise meta "Internal error, please report an issue" +let internal_error (file : string) (line : int) meta = craise file line meta "Internal error, please report an issue" let exec_raise = craise let exec_assert = cassert -- cgit v1.2.3 From 8f969634f3f192a9282a21a1ca2a1b6a676984ca Mon Sep 17 00:00:00 2001 From: Escherichia Date: Fri, 29 Mar 2024 14:07:36 +0100 Subject: formatting and changed save_error condition for failing from b to not b --- compiler/Errors.ml | 48 ++++++++++++++++++++++++++++++++++-------------- 1 file changed, 34 insertions(+), 14 deletions(-) (limited to 'compiler/Errors.ml') diff --git a/compiler/Errors.ml b/compiler/Errors.ml index bfdf5796..41e841f0 100644 --- a/compiler/Errors.ml +++ b/compiler/Errors.ml @@ -13,43 +13,63 @@ exception CFailure of string let error_list : (Meta.meta option * string) list ref = ref [] -let push_error (file : string) (line : int) (meta : Meta.meta option) (msg : string) = - error_list := (meta, msg ^ "\n In file:" ^ file ^ "\n Line:" ^ string_of_int line) :: !error_list +let push_error (file : string) (line : int) (meta : Meta.meta option) + (msg : string) = + error_list := + (meta, msg ^ "\n In file:" ^ file ^ "\n Line:" ^ string_of_int line) + :: !error_list -let save_error (file : string) (line : int) ?(b : bool = true) (meta : Meta.meta option) (msg : string) = +let save_error (file : string) (line : int) ?(b : bool = true) + (meta : Meta.meta option) (msg : string) = push_error file line meta msg; match meta with | Some m -> - if !Config.fail_hard && b then - raise (Failure (format_error_message m (msg ^ "\n In file:" ^ file ^ "\n Line:" ^ string_of_int line))) - | None -> if !Config.fail_hard && b then raise (Failure msg) + if !Config.fail_hard && not b then + raise + (Failure + (format_error_message m + (msg ^ "\n In file:" ^ file ^ "\n Line:" ^ string_of_int line))) + | None -> if !Config.fail_hard && not b then raise (Failure msg) -let craise_opt_meta (file : string) (line : int) (meta : Meta.meta option) (msg : string) = +let craise_opt_meta (file : string) (line : int) (meta : Meta.meta option) + (msg : string) = match meta with | Some m -> - if !Config.fail_hard then raise (Failure (format_error_message m (msg ^ "\n In file:" ^ file ^ "\n Line:" ^ string_of_int line))) + if !Config.fail_hard then + raise + (Failure + (format_error_message m + (msg ^ "\n In file:" ^ file ^ "\n Line:" ^ string_of_int line))) else let () = push_error file line (Some m) msg in raise (CFailure msg) | None -> - if !Config.fail_hard then raise (Failure (msg ^ "\n In file:" ^ file ^ "\n Line:" ^ string_of_int line)) + if !Config.fail_hard then + raise + (Failure (msg ^ "\n In file:" ^ file ^ "\n Line:" ^ string_of_int line)) else let () = push_error file line None msg in raise (CFailure msg) -let craise (file : string) (line : int) (meta : Meta.meta) (msg : string) = craise_opt_meta file line (Some meta) msg +let craise (file : string) (line : int) (meta : Meta.meta) (msg : string) = + craise_opt_meta file line (Some meta) msg -let cassert_opt_meta (file : string) (line : int) (b : bool) (meta : Meta.meta option) (msg : string) = +let cassert_opt_meta (file : string) (line : int) (b : bool) + (meta : Meta.meta option) (msg : string) = if not b then craise_opt_meta file line meta msg -let cassert (file : string) (line : int) (b : bool) (meta : Meta.meta) (msg : string) = +let cassert (file : string) (line : int) (b : bool) (meta : Meta.meta) + (msg : string) = cassert_opt_meta file line b (Some meta) msg -let sanity_check (file : string) (line : int) b meta = cassert file line b meta "Internal error, please file an issue" +let sanity_check (file : string) (line : int) b meta = + cassert file line b meta "Internal error, please file an issue" let sanity_check_opt_meta (file : string) (line : int) b meta = cassert_opt_meta file line b meta "Internal error, please file an issue" -let internal_error (file : string) (line : int) meta = craise file line meta "Internal error, please report an issue" +let internal_error (file : string) (line : int) meta = + craise file line meta "Internal error, please report an issue" + let exec_raise = craise let exec_assert = cassert -- cgit v1.2.3 From ea086d3391f6086573750f989256119e5d2e7d5c Mon Sep 17 00:00:00 2001 From: Son Ho Date: Fri, 29 Mar 2024 15:36:18 +0100 Subject: Cleanup a bit --- compiler/Errors.ml | 60 ++++++++++++++++++++++-------------------------------- 1 file changed, 24 insertions(+), 36 deletions(-) (limited to 'compiler/Errors.ml') diff --git a/compiler/Errors.ml b/compiler/Errors.ml index 41e841f0..04fd708b 100644 --- a/compiler/Errors.ml +++ b/compiler/Errors.ml @@ -6,50 +6,38 @@ let meta_to_string (span : Meta.span) = "Source: '" ^ file ^ "', lines " ^ loc_to_string span.beg_loc ^ "-" ^ loc_to_string span.end_loc -let format_error_message (meta : Meta.meta) msg = - msg ^ "\n" ^ meta_to_string meta.span +let format_error_message (meta : Meta.meta option) (msg : string) = + let meta = + match meta with None -> "" | Some meta -> "\n" ^ meta_to_string meta.span + in + msg ^ meta + +let format_error_message_with_file_line (file : string) (line : int) + (meta : Meta.meta option) (msg : string) = + "In file:" ^ file ^ ", line:" ^ string_of_int line ^ "\n" + ^ format_error_message meta msg -exception CFailure of string +exception CFailure of (Meta.meta option * string) let error_list : (Meta.meta option * string) list ref = ref [] -let push_error (file : string) (line : int) (meta : Meta.meta option) - (msg : string) = - error_list := - (meta, msg ^ "\n In file:" ^ file ^ "\n Line:" ^ string_of_int line) - :: !error_list +let push_error (meta : Meta.meta option) (msg : string) = + error_list := (meta, msg) :: !error_list -let save_error (file : string) (line : int) ?(b : bool = true) +(** Register an error, and throw an exception if [throw] is true *) +let save_error (file : string) (line : int) ?(throw : bool = false) (meta : Meta.meta option) (msg : string) = - push_error file line meta msg; - match meta with - | Some m -> - if !Config.fail_hard && not b then - raise - (Failure - (format_error_message m - (msg ^ "\n In file:" ^ file ^ "\n Line:" ^ string_of_int line))) - | None -> if !Config.fail_hard && not b then raise (Failure msg) + push_error meta msg; + if !Config.fail_hard && throw then + raise (Failure (format_error_message_with_file_line file line meta msg)) let craise_opt_meta (file : string) (line : int) (meta : Meta.meta option) (msg : string) = - match meta with - | Some m -> - if !Config.fail_hard then - raise - (Failure - (format_error_message m - (msg ^ "\n In file:" ^ file ^ "\n Line:" ^ string_of_int line))) - else - let () = push_error file line (Some m) msg in - raise (CFailure msg) - | None -> - if !Config.fail_hard then - raise - (Failure (msg ^ "\n In file:" ^ file ^ "\n Line:" ^ string_of_int line)) - else - let () = push_error file line None msg in - raise (CFailure msg) + if !Config.fail_hard then + raise (Failure (format_error_message_with_file_line file line meta msg)) + else + let () = push_error meta msg in + raise (CFailure (meta, msg)) let craise (file : string) (line : int) (meta : Meta.meta) (msg : string) = craise_opt_meta file line (Some meta) msg @@ -69,7 +57,7 @@ let sanity_check_opt_meta (file : string) (line : int) b meta = cassert_opt_meta file line b meta "Internal error, please file an issue" let internal_error (file : string) (line : int) meta = - craise file line meta "Internal error, please report an issue" + craise file line meta "Internal error, please file an issue" let exec_raise = craise let exec_assert = cassert -- cgit v1.2.3 From 9403920e1e46157089f78bc42c553eec38181fa9 Mon Sep 17 00:00:00 2001 From: Son Ho Date: Fri, 29 Mar 2024 15:42:56 +0100 Subject: Improve the error messages --- compiler/Errors.ml | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) (limited to 'compiler/Errors.ml') diff --git a/compiler/Errors.ml b/compiler/Errors.ml index 04fd708b..53e56c44 100644 --- a/compiler/Errors.ml +++ b/compiler/Errors.ml @@ -1,3 +1,5 @@ +let log = Logging.errors_log + let meta_to_string (span : Meta.span) = let file = match span.file with Virtual s | Local s -> s in let loc_to_string (l : Meta.loc) : string = @@ -14,7 +16,7 @@ let format_error_message (meta : Meta.meta option) (msg : string) = let format_error_message_with_file_line (file : string) (line : int) (meta : Meta.meta option) (msg : string) = - "In file:" ^ file ^ ", line:" ^ string_of_int line ^ "\n" + "In file " ^ file ^ ", line " ^ string_of_int line ^ ":\n" ^ format_error_message meta msg exception CFailure of (Meta.meta option * string) @@ -28,13 +30,17 @@ let push_error (meta : Meta.meta option) (msg : string) = let save_error (file : string) (line : int) ?(throw : bool = false) (meta : Meta.meta option) (msg : string) = push_error meta msg; - if !Config.fail_hard && throw then - raise (Failure (format_error_message_with_file_line file line meta msg)) + if !Config.fail_hard && throw then ( + let msg = format_error_message_with_file_line file line meta msg in + log#serror (msg ^ "\n"); + raise (Failure msg)) let craise_opt_meta (file : string) (line : int) (meta : Meta.meta option) (msg : string) = - if !Config.fail_hard then - raise (Failure (format_error_message_with_file_line file line meta msg)) + if !Config.fail_hard then ( + let msg = format_error_message_with_file_line file line meta msg in + log#serror (msg ^ "\n"); + raise (Failure (format_error_message_with_file_line file line meta msg))) else let () = push_error meta msg in raise (CFailure (meta, msg)) -- cgit v1.2.3