Skip to content

Commit 0b8ec20

Browse files
committed
Print readable error messages in doc code blocks
When parsing fails inside a `{@ocaml[...]}` block, the catch-all branch formatted the exception with `Exn.pp`, which for `Lexer.Error` produced the raw constructor pattern `Ocamlformat_parser_extended.Lexer.Error(_, _)`. `Lexer.Error` (and most other compiler-libs exceptions) register a printer via `Location.register_error_of_exn`, but that table is separate from the `Printexc` table `Exn.pp` consults, so the printer was never used. Route the catch-all through `Location.error_of_exn` instead: if a printer is registered, format its `main` message; otherwise fall back to `Exn.pp`. This handles `Lexer.Error` and any other registered exception (e.g. parser internals) without per-variant enumeration. The .err diff against the previous commit shows the before/after for the six variants the regression test exercises.
1 parent 94d944b commit 0b8ec20

7 files changed

Lines changed: 39 additions & 25 deletions

File tree

CHANGES.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ profile. This started with version 0.26.0.
88

99
### Fixed
1010

11+
- Print readable messages for errors raised when parsing OCaml code blocks
12+
in documentation comments, instead of the raw exception representation
13+
`Ocamlformat_parser_extended.Lexer.Error(_, _)` (#PR, @hhugo)
14+
1115
- Fix instability on long `if-then-else` with `if-then-else=fit-or-vertical`
1216
(#2797, @MisterDA)
1317

lib/Fmt_ast.ml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5155,7 +5155,14 @@ let fmt_code ~debug =
51555155
Error (`Msg (Format.asprintf "not expecting: %s" x))
51565156
| exception Syntaxerr.Error (Other _) when warn ->
51575157
Error (`Msg (Format.asprintf "invalid toplevel or OCaml syntax"))
5158-
| exception e when warn -> Error (`Msg (Format.asprintf "%a" Exn.pp e))
5158+
| exception e when warn ->
5159+
let msg =
5160+
match Location.error_of_exn e with
5161+
| Some (`Ok report) ->
5162+
Format.asprintf "%a" Format_doc.Doc.format report.main.txt
5163+
| _ -> Format.asprintf "%a" Exn.pp e
5164+
in
5165+
Error (`Msg msg)
51595166
| exception _ -> Error (`Msg "")
51605167
in
51615168
fmt_code

lib/dune

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
-open
2424
Ocamlformat_parser_extended
2525
-open
26+
Ocamlformat_parser_shims
27+
-open
2628
Ocamlformat_stdlib
2729
-open
2830
Ocamlformat_format))
@@ -35,6 +37,7 @@
3537
ocamlformat_ocaml_common
3638
ocamlformat_odoc_parser
3739
ocamlformat_parser_extended
40+
ocamlformat_parser_shims
3841
ocamlformat_parser_standard
3942
ocamlformat_stdlib
4043
ocp-indent.lib
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
Warning: Invalid documentation comment:
22
File "doc_lexer_errors.mld", line 9, character 8 to line 11, character 0:
3-
invalid code block: Ocamlformat_parser_extended.Lexer.Error(1, _)
3+
invalid code block: Illegal empty character literal ''
44
Warning: Invalid documentation comment:
55
File "doc_lexer_errors.mld", line 15, character 8 to line 17, character 0:
6-
invalid code block: Ocamlformat_parser_extended.Lexer.Error(0, _)
6+
invalid code block: String literal not terminated
77
Warning: Invalid documentation comment:
88
File "doc_lexer_errors.mld", line 21, character 8 to line 23, character 0:
9-
invalid code block: Ocamlformat_parser_extended.Lexer.Error(_, _)
9+
invalid code block: Comment not terminated
1010
Warning: Invalid documentation comment:
1111
File "doc_lexer_errors.mld", line 27, character 8 to line 29, character 0:
12-
invalid code block: Ocamlformat_parser_extended.Lexer.Error(_, _)
12+
invalid code block: Invalid literal 0xz
1313
Warning: Invalid documentation comment:
1414
File "doc_lexer_errors.mld", line 33, character 8 to line 35, character 0:
15-
invalid code block: Ocamlformat_parser_extended.Lexer.Error(_, _)
15+
invalid code block: Foo cannot be used as label name, it must start with a lowercase letter
1616
Warning: Invalid documentation comment:
1717
File "doc_lexer_errors.mld", line 39, character 8 to line 41, character 0:
18-
invalid code block: Ocamlformat_parser_extended.Lexer.Error(_, _)
18+
invalid code block: let is a keyword, it cannot be used as label name
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
Warning: Invalid documentation comment:
22
File "doc_lexer_errors.mld", line 9, character 8 to line 11, character 0:
3-
invalid code block: Ocamlformat_parser_extended.Lexer.Error(1, _)
3+
invalid code block: Illegal empty character literal ''
44
Warning: Invalid documentation comment:
55
File "doc_lexer_errors.mld", line 15, character 8 to line 17, character 0:
6-
invalid code block: Ocamlformat_parser_extended.Lexer.Error(0, _)
6+
invalid code block: String literal not terminated
77
Warning: Invalid documentation comment:
88
File "doc_lexer_errors.mld", line 21, character 8 to line 23, character 0:
9-
invalid code block: Ocamlformat_parser_extended.Lexer.Error(_, _)
9+
invalid code block: Comment not terminated
1010
Warning: Invalid documentation comment:
1111
File "doc_lexer_errors.mld", line 27, character 8 to line 29, character 0:
12-
invalid code block: Ocamlformat_parser_extended.Lexer.Error(_, _)
12+
invalid code block: Invalid literal 0xz
1313
Warning: Invalid documentation comment:
1414
File "doc_lexer_errors.mld", line 33, character 8 to line 35, character 0:
15-
invalid code block: Ocamlformat_parser_extended.Lexer.Error(_, _)
15+
invalid code block: Foo cannot be used as label name, it must start with a lowercase letter
1616
Warning: Invalid documentation comment:
1717
File "doc_lexer_errors.mld", line 39, character 8 to line 41, character 0:
18-
invalid code block: Ocamlformat_parser_extended.Lexer.Error(_, _)
18+
invalid code block: let is a keyword, it cannot be used as label name
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
Warning: Invalid documentation comment:
22
File "doc_lexer_errors.mld", line 9, character 8 to line 11, character 0:
3-
invalid code block: Ocamlformat_parser_extended.Lexer.Error(1, _)
3+
invalid code block: Illegal empty character literal ''
44
Warning: Invalid documentation comment:
55
File "doc_lexer_errors.mld", line 15, character 8 to line 17, character 0:
6-
invalid code block: Ocamlformat_parser_extended.Lexer.Error(0, _)
6+
invalid code block: String literal not terminated
77
Warning: Invalid documentation comment:
88
File "doc_lexer_errors.mld", line 21, character 8 to line 23, character 0:
9-
invalid code block: Ocamlformat_parser_extended.Lexer.Error(_, _)
9+
invalid code block: Comment not terminated
1010
Warning: Invalid documentation comment:
1111
File "doc_lexer_errors.mld", line 27, character 8 to line 29, character 0:
12-
invalid code block: Ocamlformat_parser_extended.Lexer.Error(_, _)
12+
invalid code block: Invalid literal 0xz
1313
Warning: Invalid documentation comment:
1414
File "doc_lexer_errors.mld", line 33, character 8 to line 35, character 0:
15-
invalid code block: Ocamlformat_parser_extended.Lexer.Error(_, _)
15+
invalid code block: Foo cannot be used as label name, it must start with a lowercase letter
1616
Warning: Invalid documentation comment:
1717
File "doc_lexer_errors.mld", line 39, character 8 to line 41, character 0:
18-
invalid code block: Ocamlformat_parser_extended.Lexer.Error(_, _)
18+
invalid code block: let is a keyword, it cannot be used as label name
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
Warning: Invalid documentation comment:
22
File "doc_lexer_errors.mld", line 9, character 8 to line 11, character 0:
3-
invalid code block: Ocamlformat_parser_extended.Lexer.Error(1, _)
3+
invalid code block: Illegal empty character literal ''
44
Warning: Invalid documentation comment:
55
File "doc_lexer_errors.mld", line 15, character 8 to line 17, character 0:
6-
invalid code block: Ocamlformat_parser_extended.Lexer.Error(0, _)
6+
invalid code block: String literal not terminated
77
Warning: Invalid documentation comment:
88
File "doc_lexer_errors.mld", line 21, character 8 to line 23, character 0:
9-
invalid code block: Ocamlformat_parser_extended.Lexer.Error(_, _)
9+
invalid code block: Comment not terminated
1010
Warning: Invalid documentation comment:
1111
File "doc_lexer_errors.mld", line 27, character 8 to line 29, character 0:
12-
invalid code block: Ocamlformat_parser_extended.Lexer.Error(_, _)
12+
invalid code block: Invalid literal 0xz
1313
Warning: Invalid documentation comment:
1414
File "doc_lexer_errors.mld", line 33, character 8 to line 35, character 0:
15-
invalid code block: Ocamlformat_parser_extended.Lexer.Error(_, _)
15+
invalid code block: Foo cannot be used as label name, it must start with a lowercase letter
1616
Warning: Invalid documentation comment:
1717
File "doc_lexer_errors.mld", line 39, character 8 to line 41, character 0:
18-
invalid code block: Ocamlformat_parser_extended.Lexer.Error(_, _)
18+
invalid code block: let is a keyword, it cannot be used as label name

0 commit comments

Comments
 (0)