Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 376 lines (332 sloc) 16.111 kb
fccc685 Initial open-source release
MLstate authored
1 (*
2 Copyright © 2011 MLstate
3
4 This file is part of OPA.
5
6 OPA is free software: you can redistribute it and/or modify it under the
7 terms of the GNU Affero General Public License, version 3, as published by
8 the Free Software Foundation.
9
10 OPA is distributed in the hope that it will be useful, but WITHOUT ANY
11 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for
13 more details.
14
15 You should have received a copy of the GNU Affero General Public License
16 along with OPA. If not, see <http://www.gnu.org/licenses/>.
17 *)
18
19 (* ************************************************************** *)
20 (** {b Descr}: This module provides pretty-printing functions for
21 [QmlTyperException.t] information embedded in an exception
22 [QmlTyperException.Exception]. *)
23 (* ************************************************************** *)
24
25
26
27
28 (* depends *)
29 module Format = Base.Format
30
31 (* alias *)
32
33 (* shorthand *)
34 module Q = QmlAst
35 module TExc = QmlTyperException
36
37
38 (* -- *)
39
40
41
42 (* ************************************************************************** *)
43 (** {b Descr}: See .mli file.
44 Prints an ending \n.
45 {b Visibility}: Exported outside this module. *)
46 (* ************************************************************************** *)
47 let pp_typer_error ~type_printer ?(highlight_printer = Base.identity)
48 ?(ident_printer = QmlPrint.pp#ident) ppf (_loc, e) =
49 let print_ty ty = highlight_printer type_printer ty in
50 match e with
51 | TExc.InvalidType (t, kind) -> (
52 Format.fprintf ppf "The@ type@ %a @ is@ invalid" print_ty t ;
53 match kind with
54 | `duplicate_field ->
55 (* E.g. type a = {b : int; b: int}. *)
56 Format.fprintf ppf
57 (",@ because@ two@ fields@ in@ the@ record@ have@ the@ same@ " ^^
58 "name.@\n")
59 | `duplicate_field_with_diff_ty_in_sum_cases ->
60 (* E.g. type t = {a : float} / {a : int}. *)
61 Format.fprintf ppf
62 (",@ because@ two@ records@ in@ the@ union@ have@ a@ same@ " ^^
63 "field@ with@ different@ types.@\n")
64 | `not_a_record ->
65 Format.fprintf ppf
66 ",@ because@ a@ part@ of@ the@ union@ is@ not@ a@ record.@\n"
67 | `record_not_closed ->
68 Format.fprintf ppf
69 ",@ because@ a@ part@ of@ the@ union@ contains@ row@ variables.@\n"
70 | `abstract_in_ty_annotation ->
71 Format.fprintf ppf
72 (",@ because@ type@ expression@ contains@ \"external\"@ which@ " ^^
73 "is@ only@ legal@ in@ type@ definitions.@\n")
74 | `other -> Format.fprintf ppf "@\n"
75 )
76 | TExc.InvalidTypeDefinition (typedef, ty) ->
77 Format.fprintf ppf
78 "The@ definition@ of@ type@ %a @ as@ %a @ is@ invalid.@\n"
79 print_ty typedef print_ty ty
80 | TExc.InvalidTypeUsage (name, vl, vr) ->
81 Format.fprintf ppf
82 ("The@ type@ instanciation@ for@ @{<red>%a@} @ is@ invalid.@ " ^^
83 "According@ to@ the@ definition@ of@ this@ type,@ it@ should@ " ^^
84 "have@ @{<green>%d@}@ argument(s).@ However,@ here,@ it@ is@ " ^^
85 "applied@ to@ @{<red>%d@}@ argument(s).@\n")
86 QmlPrint.pp#typeident name (List.length vl) (List.length vr)
87 | TExc.IdentifierNotFound (eid, _) ->
88 Format.fprintf ppf
89 "The value@ @{<red>%a@} @ is@ not@ defined.@\n" ident_printer eid
90 | TExc.TypeIdentNotFound tid ->
91 Format.fprintf ppf
92 "The type@ @{<red>%a@} @ is@ not@ defined.@\n"
5242af3 @BourgerieQuentin [enhance] compiler: Add compiler packages (Import packages that not plac...
BourgerieQuentin authored
93 QmlPrint.pp_base#typeident tid
fccc685 Initial open-source release
MLstate authored
94 | TExc.DuplicateTypeDefinitions s ->
95 Format.fprintf ppf
96 "There@ are@ duplicate@ definitions@ for@ type@ @{<red>%s@} .@\n" s
97
98
99
100 (* ************************************************************************** *)
101 (** {b Descr}: Find strings among the list [l] with spelling close to the
102 string [typo]. Spelling-based neighbours are returned in a list of strings
103 sorted by increasing distance, i.e. with string closest from [typo] first.
104 {b Visibility}: Not exported outside this module. *)
105 (* ************************************************************************** *)
106 let get_closest_names typo l =
107 let distances =
108 List.map (fun name -> (name, Sed.sed Sed.qwerty_distance name typo)) l in
109 List.sort compare distances
110
111
112
113 (* This function assumes that we are rid of [TypeIdent]. *)
ed8052e @fpessaux [cleanup] Typer exceptions: Removed no more raised exceptions and relate...
fpessaux authored
114 let rec pp_advice ppf = function
fccc685 Initial open-source release
MLstate authored
115 | QmlTyperException.InvalidTypeDefinition _ ->
116 Format.fprintf ppf
117 ("@[Diagnosis:@ possibly,@ a@ type@ variable@ appears@ in@ the@ " ^^
118 "body@ of@ the@ definition@ but@ not@ in@ the@ list@ of@ " ^^
119 "parameters,@ or@ the@ definition@ is@ trivially@ cyclic@ like@ " ^^
120 "in@ @{<red>type t = t@},@ or@ it@ contains@ several@ times@ a@ " ^^
121 "same@ variable,@ or@ the@ definition@ is@ a@ sum@ containing@ " ^^
122 "several@ cases@ having@ the@ same@ labels@ like@ " ^^
123 "@{<red>{ l } / { l }@}@ or@ " ^^
124 "@{<red>{ l : bool } / { l : int }@}.@]@\n")
125 | QmlTyperException.IdentifierNotFound (name, bound_in_scope) ->
126 let name = Ident.original_name name in
127 let bound_in_scope =
128 List.map Ident.original_name bound_in_scope in
129 (match get_closest_names name bound_in_scope with
130 | [] -> Format.fprintf ppf "@\n" (* No idea. *)
131 | [(name, _)] ->
132 Format.fprintf ppf
133 ("@[Diagnosis:@ This@ identifier@ doesn't@ exist.@ Perhaps@ " ^^
134 "you@ meant@ @{<red>%s@}?@]@\n")
135 name
136 | (name_1, _) :: (name_2, _) :: _ ->
137 Format.fprintf ppf
138 ("@[Diagnosis:@ This@ identifier@ doesn't@ exist.@ Perhaps@ " ^^
139 "you@ meant@ @{<red>%s@}@ or@ @{<red>%s@}?@]@\n")
140 name_1 name_2)
141 | _ -> ()
142
143
144
145 (* ************************************************************************** *)
146 (** {b Descr}: Prints an error message describing the typechecking error kind
147 and an advice to solve it is some is available.
148 Prints an ending \n
149 {b Visibility}: Not exported outside this module. *)
150 (* ************************************************************************** *)
ed8052e @fpessaux [cleanup] Typer exceptions: Removed no more raised exceptions and relate...
fpessaux authored
151 let pp_description_and_advice ppf err_descr =
fccc685 Initial open-source release
MLstate authored
152 (* Printf the core of the error message description. *)
153 Format.fprintf ppf "@[Description:@ " ;
154 pp_typer_error
155 ~ident_printer:
156 (fun ppf i -> Format.fprintf ppf "%s" (Ident.original_name i))
157 (* Create a new printer for each exception so that the same scope of
158 typevars in used throughout the message, but is not the same as in
159 other messages. *)
160 ~type_printer: (new QmlPrint.opa_printer)#ty
161 ~highlight_printer: (fun pr ppf v -> Format.fprintf ppf "@{<red>%a@}" pr v)
162 ppf err_descr ;
163 Format.fprintf ppf "@]" ;
164 (* Print the more detailled message (advice) if some can be guessed. This
165 always prints an ending if a more detailled message is printed\n. *)
ed8052e @fpessaux [cleanup] Typer exceptions: Removed no more raised exceptions and relate...
fpessaux authored
166 pp_advice ppf (snd err_descr)
fccc685 Initial open-source release
MLstate authored
167
168
169
170 (* Doesn't print ending \n. *)
171 let pp_position_or_code ppf at_location =
172 match at_location with
173 | `Expr_loc e ->
174 (*
175 (* FPE says: what's this garbage ? To be inspected later ! *)
176 let clean s =
177 Base.String.map
178 (fun c ->
179 if (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
180 (c >= '0' && c <= '9')
181 then c
182 else '_')
183 s in
184 (
185 match e.Q.e with
186 | Q.LetRecIn ( [], _) | Q.LetRecIn ( [_], _) -> ()
187 | Q.LetRecIn (bindings, _) ->
188 prerr_endline
189 "\nyou can show the dependencies graph with the following dot script :\n just copy and paste the following digraph in a new file, and run :\ndisplay file.dot.";
190 prerr_endline "digraph a {";
191 let set = IdentSet.from_list (List.map fst bindings) in
192 List.iter
193 (fun (ident, expr) ->
194 QmlAstWalk.Expr.iter_down
195 (fun e ->
196 match e.Q.e with
197 | Q.Ident i ->
198 if IdentSet.mem i set then
199 prerr_endline
200 (Format.sprintf
201 "%s -> %s"
202 (clean (Ident.original_name ident))
203 (clean (Ident.original_name i)))
204 | _ -> ())
205 expr
206 )
207 bindings ;
208 prerr_endline "}\n"
209 | _ -> ()
210 ) ;
211 *)
212 let pos = Q.Pos.expr e in
213 (* If no position available, then print the code instead. *)
214 if not (FilePos.is_empty pos) then
215 Format.fprintf ppf "%s" (FilePos.to_string pos)
216 else
217 Format.fprintf ppf
218 " inside expression:@\n <<@\n %a@\n >>" QmlPrint.pp#expr e
219 | `Pat_loc pat ->
220 let pos = Q.Pos.pat pat in
221 if not (FilePos.is_empty pos) then
222 Format.fprintf ppf "%s" (FilePos.to_string pos)
223 else
224 Format.fprintf ppf
225 " inside pattern:@\n <<@\n %a@\n >>" QmlPrint.pp#pat pat
226 | `Ty_loc t ->
227 (* Always print the code since no location are stored in annotmap for
228 types. *)
229 Format.fprintf ppf " inside type expression:@\n <<@\n %a@\n >>"
230 QmlPrint.pp#ty t
231 | `No_loc -> Format.fprintf ppf "No position available"
232
233
234
235 (* ************************************************************************** *)
236 (** {b Descr}: Global error message printing routine for typechecking errors.
237 It prints the complete error message by invoking the sub-routines dealing
238 with each sub-part of the message.
239 The complete message includes error's location, error's kind description
240 and an advice to solve if some is available.
241 Prints an ending \n
242 {b Visibility} : Not exported outside this module. *)
243 (* ************************************************************************** *)
ed8052e @fpessaux [cleanup] Typer exceptions: Removed no more raised exceptions and relate...
fpessaux authored
244 let pp_typer_exception_error ppf err_descr =
fccc685 Initial open-source release
MLstate authored
245 let ((at_location, loc_set), _) = err_descr in
246 (* Print the location taken from the expression's one, then print the message
247 describing the typer error and its possible advice. *)
248 Format.fprintf ppf
249 "@[%a@\n%a@]"
ed8052e @fpessaux [cleanup] Typer exceptions: Removed no more raised exceptions and relate...
fpessaux authored
250 pp_position_or_code at_location pp_description_and_advice err_descr ;
fccc685 Initial open-source release
MLstate authored
251 (* Get extra location that are not "unknown" to eventually print them. *)
252 let extra_locations =
253 QmlTyperException.LocSet.fold
254 (fun loc acc ->
255 match loc with
256 | `Expr_loc _ | `Pat_loc _ | `Ty_loc _ -> loc :: acc
257 | `No_loc -> acc)
258 loc_set [] in
259 match extra_locations with
260 | [] -> () (* We have no better hint. *)
261 | l ->
262 Format.fprintf ppf "@[The@ problem@ seems@ to@ originate@ from@\n" ;
263 List.iter
264 (fun extra_loc ->
265 Format.fprintf ppf
266 "- %a@\n" pp_position_or_code extra_loc)
267 l ;
268 Format.fprintf ppf ".@]@\n"
269
270
271
272 (* ************************************************************************** *)
273 (** {b Descr}: Function that checks in the list of exceptions raised hence
274 issuing the coming error message if there is a precise source code location
275 available, i.e. something locatign in term of line and column number in a
276 source file.
277 This is a bit heavy and time-consuming processing, but this is only called
278 in case of error. And, in such a case, we are not anymore worried of
279 efficiency since the compilation will end.
280 We need this to tray as far as we can to give accurate error position even
281 when an exception doesn't embedd a code element having a location. This is
282 especially the case for type definitions in which there is no available
283 location in the core exception describing the error. The most precise
284 location is on the definition itself (which is forgotten when we treat the
285 exception that finely describes the error). To avoid losing the location,
286 at the moment where we still have one, if it appears that there is no more
287 location in the nested and deeper exceptions, then we print the one we
288 have. Otherwise, we keep the oen of the nested and deeper exceptions.
289 This permits to have as far as we can locations and to avoid repeatition if
290 it appears that we have some in the nested exceptions. Thsi makes messages
291 more precise without making them too verbose.
292 REMARK: This is not very satisfactory, but until a deeper refactoring of
293 exceptions and errors raising, that better than nothing !
294 {b Visibility} : Exported outside this module. *)
295 (* ************************************************************************** *)
296 let rec exist_precise_main_location annotmap = function
297 | [] -> false
298 | h :: q -> (
299 match h with
300 | QmlTypes.Exception (QmlTypes.TyperError (code_elt, (e, excn))) ->
301 let ctxt = QmlError.Context.code_elt code_elt in
302 let loc = QmlError.Context.get_pos ctxt in
303 (not (FilePos.is_empty loc)) ||
304 (exist_precise_main_location annotmap (e :: excn)) ||
305 (exist_precise_main_location annotmap q)
306 | QmlTyperException.Exception ((at_location, _), _) ->
307 let loc_is_empty =
308 (match at_location with
309 | `Expr_loc e ->
310 FilePos.is_empty (Q.Pos.expr e)
311 | `Pat_loc pat ->
312 FilePos.is_empty (Q.Pos.pat pat)
313 | _ -> true) in
314 (not loc_is_empty) ||
315 (exist_precise_main_location annotmap q)
316 | _ -> false
317 )
318
319
320
321 (* ************************************************************************** *)
322 (** {b Descr}: See .mli file.
323 Prints an ending \n.
324 {b Visibility} : Exported outside this module. *)
325 (* ************************************************************************** *)
ed8052e @fpessaux [cleanup] Typer exceptions: Removed no more raised exceptions and relate...
fpessaux authored
326 let rec pp_report_from_typer_exception annotmap ppf = function
fccc685 Initial open-source release
MLstate authored
327 | QmlTypes.Exception (QmlTypes.TyperError (code_elt, (e, excn))) ->
328 let enqued_excs = e :: excn in
329 (* Hack especially rotten to handle source location in type definitions.
330 In fact, currently and until deeper refactoring, [`Ty_loc _] do not
331 permit to access accurate position in the annotation maps since there
332 is not annot key available. This results in poor location in error
333 messages. So, just for these case, instead of throwing the
334 [code_elt] embedded in the present [QmlTypes.Exception], we print its
335 location.
336 Otherwise, we let the regular processing acting. *)
337 if not (exist_precise_main_location annotmap enqued_excs) then (
338 match code_elt with
339 | Q.NewType (_, _) ->
340 let ctxt = QmlError.Context.code_elt code_elt in
341 let loc = QmlError.Context.get_pos ctxt in
342 if not (FilePos.is_empty loc) then
343 Format.fprintf ppf "%s@\n" (FilePos.to_string loc)
344 | _ -> ()) ;
ed8052e @fpessaux [cleanup] Typer exceptions: Removed no more raised exceptions and relate...
fpessaux authored
345 List.iter (pp_report_from_typer_exception annotmap ppf) enqued_excs
fccc685 Initial open-source release
MLstate authored
346 | QmlTyperException.Exception err_descr ->
ed8052e @fpessaux [cleanup] Typer exceptions: Removed no more raised exceptions and relate...
fpessaux authored
347 pp_typer_exception_error ppf err_descr
fccc685 Initial open-source release
MLstate authored
348 | e -> Format.fprintf ppf "%s@\n" (Printexc.to_string e)
349
350
351
352 let typechecking_exception_handler env exn =
353 match exn with
354 | QmlTypes.Exception _ ->
355 OManager.error "%a"
ed8052e @fpessaux [cleanup] Typer exceptions: Removed no more raised exceptions and relate...
fpessaux authored
356 (pp_report_from_typer_exception env.QmlTypes.annotmap) exn
fccc685 Initial open-source release
MLstate authored
357 | _ -> raise exn
358
359
360
361 (* As a last resort, register a printer to still get some info from uncaught
362 typer exceptions. Do not call [Printexc.to_string] with the current uncaught
363 exception to build the final message string otherwise the register mechanism
364 will make us stupidly looping ! *)
365 let _ =
366 Printexc.register_printer
367 (function
368 | QmlTyperException.Exception ((loc, _), e) ->
369 pp_typer_error
370 ~type_printer: (new QmlPrint.opa_printer)#ty Format.str_formatter
371 (loc, e) ;
372 Some
373 ("\nPlease report. Uncaught QmlTyperException.Exception: " ^
374 Format.flush_str_formatter ())
375 | _ -> None)
Something went wrong with that request. Please try again.