Skip to content

Commit 8a96b56

Browse files
committed
Improve integer literal error messages
Signed-off-by: Nathan Rebours <[email protected]>
1 parent 0dbe770 commit 8a96b56

File tree

4 files changed

+36
-19
lines changed

4 files changed

+36
-19
lines changed

lib/error.ml

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,27 @@
11
open Ppxlib
22

3-
let unsupported_payload ~loc =
4-
Location.error_extensionf ~loc "ppx_yojson: unsupported payload"
3+
let errorf ~loc message =
4+
Location.error_extensionf ~loc "ppx_yojson: %s" message
55

6-
let unsupported_record_field ~loc =
7-
Location.error_extensionf ~loc "ppx_yojson: unsupported record field"
6+
let unsupported_payload ~loc = errorf ~loc "unsupported payload"
7+
let unsupported_record_field ~loc = errorf ~loc "unsupported record field"
88

99
let too_many_fields_in_record_pattern ~loc =
10-
Location.error_extensionf ~loc
11-
"ppx_yojson: record patterns with more than 4 fields aren't supported. \
12-
Consider using ppx_deriving_yojson to handle more complex json objects."
10+
errorf ~loc
11+
"record patterns with more than 4 fields aren't supported. Consider using \
12+
ppx_deriving_yojson to handle more complex json objects."
1313

1414
let bad_expr_antiquotation_payload ~loc =
15-
Location.error_extensionf ~loc
16-
"ppx_yojson: bad antiquotation payload, should be a single expression"
15+
errorf ~loc "bad antiquotation payload, should be a single expression"
1716

1817
let bad_pat_antiquotation_payload ~loc =
19-
Location.error_extensionf ~loc
20-
"ppx_yojson: bad antiquotation payload, should be a pattern"
18+
errorf ~loc "bad antiquotation payload, should be a pattern"
19+
20+
let invalid_integer_literal_yojson ~loc =
21+
errorf ~loc
22+
"invalid interger literal. Integer literal should fit within an OCaml int \
23+
or be written in decimal form."
24+
25+
let invalid_integer_literal_ezjsonm ~loc =
26+
errorf ~loc
27+
"invalid interger literal. Integer literal should fit within an OCaml int."

lib/error.mli

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,9 @@ val bad_expr_antiquotation_payload : loc:Ppxlib.Location.t -> Ppxlib.extension
2020

2121
val bad_pat_antiquotation_payload : loc:Ppxlib.Location.t -> Ppxlib.extension
2222
(** Use this for bad payload in pattern antiquotation [[%y? ...]]. *)
23+
24+
val invalid_integer_literal_yojson : loc:Ppxlib.Location.t -> Ppxlib.extension
25+
(** Use this for invalid integer literals in the yojson extension *)
26+
27+
val invalid_integer_literal_ezjsonm : loc:Ppxlib.Location.t -> Ppxlib.extension
28+
(** Use this for invalid integer literals in the ezjsonm extension *)

lib/expression.ml

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,16 @@ module Ezjsonm_expander : EXPANDER = struct
4444
include Common
4545

4646
let expand_intlit ~loc:_ ~pexp_loc:loc _ =
47-
Ast_builder.Default.pexp_extension ~loc (Error.unsupported_payload ~loc)
47+
Ast_builder.Default.pexp_extension ~loc
48+
(Error.invalid_integer_literal_ezjsonm ~loc)
4849

4950
let expand_int ~loc ~pexp_loc s =
5051
match int_of_string_opt s with
5152
| Some i ->
5253
[%expr `Float [%e Ast_builder.Default.efloat ~loc (string_of_int i)]]
5354
| _ ->
5455
Ast_builder.Default.pexp_extension ~loc:pexp_loc
55-
(Error.unsupported_payload ~loc:pexp_loc)
56+
(Error.invalid_integer_literal_ezjsonm ~loc:pexp_loc)
5657

5758
let expand_list ~loc exprs =
5859
expand_list ~loc (fun e -> [%expr `A [%e e]]) exprs
@@ -72,13 +73,13 @@ module Yojson_expander : EXPANDER = struct
7273
| Some i -> [%expr `Int [%e Ast_builder.Default.eint ~loc i]]
7374
| None when Integer_const.is_binary s ->
7475
Ast_builder.Default.pexp_extension ~loc:pexp_loc
75-
(Error.unsupported_payload ~loc:pexp_loc)
76+
(Error.invalid_integer_literal_yojson ~loc:pexp_loc)
7677
| None when Integer_const.is_octal s ->
7778
Ast_builder.Default.pexp_extension ~loc:pexp_loc
78-
(Error.unsupported_payload ~loc:pexp_loc)
79+
(Error.invalid_integer_literal_yojson ~loc:pexp_loc)
7980
| None when Integer_const.is_hexadecimal s ->
8081
Ast_builder.Default.pexp_extension ~loc:pexp_loc
81-
(Error.unsupported_payload ~loc:pexp_loc)
82+
(Error.invalid_integer_literal_yojson ~loc:pexp_loc)
8283
| None -> expand_intlit ~loc ~pexp_loc s
8384

8485
let expand_list ~loc exprs =

test/rewriter/errors/run.t

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ should trigger errors:
5353
File "test.ml", line 1, characters 35-59:
5454
1 | let invalid_hex_literal = [%yojson 0xffffffffffffffffffffff]
5555
^^^^^^^^^^^^^^^^^^^^^^^^
56-
Error: ppx_yojson: unsupported payload
56+
Error: ppx_yojson: invalid interger literal. Integer literal should fit
57+
within an OCaml int or be written in decimal form.
5758
[1]
5859

5960
---------------------------------------
@@ -79,7 +80,8 @@ should trigger errors:
7980
File "test.ml", line 1, characters 37-79:
8081
1 | let invalid_octal_literal = [%yojson 0o7777777777777777777777777777777777777777]
8182
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
82-
Error: ppx_yojson: unsupported payload
83+
Error: ppx_yojson: invalid interger literal. Integer literal should fit
84+
within an OCaml int or be written in decimal form.
8385
[1]
8486

8587
--------------------------------------
@@ -105,7 +107,8 @@ should trigger errors:
105107
File "test.ml", line 1, characters 35-146:
106108
1 | let invalid_bin_literal = [%yojson 0b1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111]
107109
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
108-
Error: ppx_yojson: unsupported payload
110+
Error: ppx_yojson: invalid interger literal. Integer literal should fit
111+
within an OCaml int or be written in decimal form.
109112
[1]
110113

111114
--------------------------------------

0 commit comments

Comments
 (0)