Skip to content

Commit 065defe

Browse files
authored
Clean up super errors in syntax (#6246)
1 parent 382988a commit 065defe

File tree

5 files changed

+26
-403
lines changed

5 files changed

+26
-403
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
#### :rocket: Main New Feature
1616
- Make uncurried mode opt-out: by default, every project is now in uncurried mode, unless `"uncurried": false` is specified in the project config. https://github.com/rescript-lang/rescript-compiler/pull/6249
1717

18+
#### :nail_care: Polish
19+
- Removed duplicate Super_error implementation in syntax https://github.com/rescript-lang/rescript-compiler/pull/6246
20+
1821
# 11.0.0-alpha.6
1922

2023
#### :boom: Breaking Change

jscomp/ml/location.ml

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ let print_loc ppf (loc : t) =
156156
fprintf ppf "@{<filename>%a@}%a" print_filename loc.loc_start.pos_fname dim_loc normalized_range
157157
;;
158158

159-
let print ~message_kind intro ppf (loc : t) =
159+
let print ?(src = None) ~message_kind intro ppf (loc : t) =
160160
begin match message_kind with
161161
| `warning -> fprintf ppf "@[@{<info>%s@}@]@," intro
162162
| `warning_as_error -> fprintf ppf "@[@{<error>%s@} (configured as error) @]@," intro
@@ -193,7 +193,12 @@ let print ~message_kind intro ppf (loc : t) =
193193
| None -> ()
194194
| Some _ -> begin
195195
try
196-
let src = Ext_io.load_file file in
196+
(* Print a syntax error that is a list of Res_diagnostics.t.
197+
Instead of reading file for every error, it uses the source that the parser already has. *)
198+
let src = match src with
199+
| Some src -> src
200+
| None -> Ext_io.load_file file
201+
in
197202
(* we're putting the line break `@,` here rather than above, because this
198203
branch might not be reached (aka no inline file content display) so
199204
we don't wanna end up with two line breaks in the the consequent *)
@@ -329,17 +334,22 @@ let error_of_exn exn =
329334

330335
(* taken from https://github.com/rescript-lang/ocaml/blob/d4144647d1bf9bc7dc3aadc24c25a7efa3a67915/parsing/location.ml#L380 *)
331336
(* This is the error report entry point. We'll replace the default reporter with this one. *)
332-
let rec default_error_reporter ppf ({loc; msg; sub}) =
337+
let rec default_error_reporter ?(src = None) ppf ({loc; msg; sub}) =
333338
setup_colors ();
334339
(* open a vertical box. Everything in our message is indented 2 spaces *)
335-
Format.fprintf ppf "@[<v>@, %a@, %s@,@]" (print ~message_kind:`error "We've found a bug for you!") loc msg;
336-
List.iter (Format.fprintf ppf "@,@[%a@]" default_error_reporter) sub
340+
(* If src is given, it will display a syntax error after parsing. *)
341+
let intro = match src with
342+
| Some _ -> "Syntax error!"
343+
| None -> "We've found a bug for you!"
344+
in
345+
Format.fprintf ppf "@[<v>@, %a@, %s@,@]" (print ~src ~message_kind:`error intro) loc msg;
346+
List.iter (Format.fprintf ppf "@,@[%a@]" (default_error_reporter ~src)) sub
337347
(* no need to flush here; location's report_exception (which uses this ultimately) flushes *)
338348

339349
let error_reporter = ref default_error_reporter
340350

341-
let report_error ppf err =
342-
!error_reporter ppf err
351+
let report_error ?(src = None) ppf err =
352+
!error_reporter ~src ppf err
343353
;;
344354

345355
let error_of_printer loc print x =
@@ -375,7 +385,7 @@ let rec report_exception_rec n ppf exn =
375385
match error_of_exn exn with
376386
| None -> reraise exn
377387
| Some `Already_displayed -> ()
378-
| Some (`Ok err) -> fprintf ppf "@[%a@]@." report_error err
388+
| Some (`Ok err) -> fprintf ppf "@[%a@]@." (report_error ~src:None) err
379389
with exn when n > 0 -> report_exception_rec (n-1) ppf exn
380390

381391
let report_exception ppf exn = report_exception_rec 5 ppf exn

jscomp/ml/location.mli

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ type 'a loc = {
7878
val mknoloc : 'a -> 'a loc
7979
val mkloc : 'a -> t -> 'a loc
8080

81-
val print: message_kind:[< `error | `warning | `warning_as_error > `warning] -> string -> formatter -> t -> unit
81+
val print: ?src:string option -> message_kind:[< `error | `warning | `warning_as_error > `warning] -> string -> formatter -> t -> unit
8282
val print_compact: formatter -> t -> unit
8383
val print_filename: formatter -> string -> unit
8484

@@ -131,12 +131,12 @@ val register_error_of_exn: (exn -> error option) -> unit
131131
a location, a message, and optionally sub-messages (each of them
132132
being located as well). *)
133133

134-
val report_error: formatter -> error -> unit
134+
val report_error: ?src:string option -> formatter -> error -> unit
135135

136-
val error_reporter : (formatter -> error -> unit) ref
136+
val error_reporter : (?src:string option -> formatter -> error -> unit) ref
137137
(** Hook for intercepting error reports. *)
138138

139-
val default_error_reporter : formatter -> error -> unit
139+
val default_error_reporter : ?src:string option -> formatter -> error -> unit
140140
(** Original error reporter for use in hooks. *)
141141

142142
val report_exception: formatter -> exn -> unit

jscomp/syntax/src/res_diagnostics.ml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,7 @@ let printReport diagnostics src =
142142
match diagnostics with
143143
| [] -> ()
144144
| d :: rest ->
145-
Res_diagnostics_printing_utils.Super_location.super_error_reporter
146-
Format.err_formatter src
145+
Location.report_error ~src:(Some src) Format.err_formatter
147146
Location.
148147
{
149148
loc = {loc_start = d.startPos; loc_end = d.endPos; loc_ghost = false};

0 commit comments

Comments
 (0)