Skip to content

Commit 2212e4c

Browse files
committed
typing/Cmi_format: more informative magic error messages
This commit changes the exported type Cmi_format.error.
1 parent 47012bb commit 2212e4c

3 files changed

Lines changed: 18 additions & 14 deletions

File tree

typing/cmi_format.ml

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@ type pers_flags =
2020
| Unsafe_string
2121

2222
type error =
23-
Not_an_interface of string
24-
| Wrong_version_interface of string * string
23+
Not_an_interface of string * string option
2524
| Corrupted_interface of string
2625

2726
exception Error of error
@@ -56,11 +55,11 @@ let read_cmi filename =
5655
with
5756
| End_of_file | Failure _ | Parse_error (Corrupted _) ->
5857
fail (Corrupted_interface filename)
59-
| Parse_error (Not_a_magic_number _) | Unexpected (Kind _) ->
60-
fail (Not_an_interface filename)
61-
| Unexpected (Version (_cmi, { actual; expected })) ->
62-
let msg = if actual < expected then "an older" else "a newer" in
63-
fail (Wrong_version_interface (filename, msg))
58+
| Parse_error (Not_a_magic_number _) ->
59+
fail (Not_an_interface (filename, None))
60+
| Unexpected error ->
61+
let explanation = explain error in
62+
fail (Not_an_interface (filename, Some explanation))
6463
end)
6564
(fun () -> close_in ic)
6665

@@ -80,14 +79,20 @@ let output_cmi filename oc cmi =
8079
open Format
8180

8281
let report_error ppf = function
83-
| Not_an_interface filename ->
84-
fprintf ppf "%a@ is not a compiled interface"
85-
Location.print_filename filename
82+
| Not_an_interface (filename, explanation) ->
83+
fprintf ppf "%a@ is not a compiled interface."
84+
Location.print_filename filename;
85+
begin match explanation with
86+
| None -> ()
87+
| Some message -> fprintf ppf "\n%s" message
88+
end
89+
(*
8690
| Wrong_version_interface (filename, older_newer) ->
8791
fprintf ppf
8892
"%a@ is not a compiled interface for this version of OCaml.@.\
8993
It seems to be for %s version of OCaml."
9094
Location.print_filename filename older_newer
95+
*)
9196
| Corrupted_interface filename ->
9297
fprintf ppf "Corrupted compiled interface@ %a"
9398
Location.print_filename filename

typing/cmi_format.mli

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,7 @@ val read_cmi : string -> cmi_infos
3838
(* Error report *)
3939

4040
type error =
41-
Not_an_interface of string
42-
| Wrong_version_interface of string * string
41+
Not_an_interface of string * string option
4342
| Corrupted_interface of string
4443

4544
exception Error of error

typing/cmt_format.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ let read filename =
129129
in
130130
Some cmi, cmt
131131
else
132-
raise(Cmi_format.Error(Cmi_format.Not_an_interface filename))
132+
raise(Cmi_format.Error(Cmi_format.Not_an_interface (filename, None)))
133133
in
134134
close_in ic;
135135
(* Printf.fprintf stderr "Cmt_format.read done\n%!"; *)
@@ -146,7 +146,7 @@ let read_cmt filename =
146146
let read_cmi filename =
147147
match read filename with
148148
None, _ ->
149-
raise (Cmi_format.Error (Cmi_format.Not_an_interface filename))
149+
raise (Cmi_format.Error (Cmi_format.Not_an_interface (filename, None)))
150150
| Some cmi, _ -> cmi
151151

152152
let saved_types = ref []

0 commit comments

Comments
 (0)