Skip to content

Commit

Permalink
Improve integer literal error messages
Browse files Browse the repository at this point in the history
Signed-off-by: Nathan Rebours <[email protected]>
  • Loading branch information
NathanReb committed Sep 28, 2023
1 parent 0dbe770 commit 8a96b56
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 19 deletions.
29 changes: 18 additions & 11 deletions lib/error.ml
Original file line number Diff line number Diff line change
@@ -1,20 +1,27 @@
open Ppxlib

let unsupported_payload ~loc =
Location.error_extensionf ~loc "ppx_yojson: unsupported payload"
let errorf ~loc message =
Location.error_extensionf ~loc "ppx_yojson: %s" message

let unsupported_record_field ~loc =
Location.error_extensionf ~loc "ppx_yojson: unsupported record field"
let unsupported_payload ~loc = errorf ~loc "unsupported payload"
let unsupported_record_field ~loc = errorf ~loc "unsupported record field"

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

let bad_expr_antiquotation_payload ~loc =
Location.error_extensionf ~loc
"ppx_yojson: bad antiquotation payload, should be a single expression"
errorf ~loc "bad antiquotation payload, should be a single expression"

let bad_pat_antiquotation_payload ~loc =
Location.error_extensionf ~loc
"ppx_yojson: bad antiquotation payload, should be a pattern"
errorf ~loc "bad antiquotation payload, should be a pattern"

let invalid_integer_literal_yojson ~loc =
errorf ~loc
"invalid interger literal. Integer literal should fit within an OCaml int \
or be written in decimal form."

let invalid_integer_literal_ezjsonm ~loc =
errorf ~loc
"invalid interger literal. Integer literal should fit within an OCaml int."
6 changes: 6 additions & 0 deletions lib/error.mli
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,9 @@ val bad_expr_antiquotation_payload : loc:Ppxlib.Location.t -> Ppxlib.extension

val bad_pat_antiquotation_payload : loc:Ppxlib.Location.t -> Ppxlib.extension
(** Use this for bad payload in pattern antiquotation [[%y? ...]]. *)

val invalid_integer_literal_yojson : loc:Ppxlib.Location.t -> Ppxlib.extension
(** Use this for invalid integer literals in the yojson extension *)

val invalid_integer_literal_ezjsonm : loc:Ppxlib.Location.t -> Ppxlib.extension
(** Use this for invalid integer literals in the ezjsonm extension *)
11 changes: 6 additions & 5 deletions lib/expression.ml
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,16 @@ module Ezjsonm_expander : EXPANDER = struct
include Common

let expand_intlit ~loc:_ ~pexp_loc:loc _ =
Ast_builder.Default.pexp_extension ~loc (Error.unsupported_payload ~loc)
Ast_builder.Default.pexp_extension ~loc
(Error.invalid_integer_literal_ezjsonm ~loc)

let expand_int ~loc ~pexp_loc s =
match int_of_string_opt s with
| Some i ->
[%expr `Float [%e Ast_builder.Default.efloat ~loc (string_of_int i)]]
| _ ->
Ast_builder.Default.pexp_extension ~loc:pexp_loc
(Error.unsupported_payload ~loc:pexp_loc)
(Error.invalid_integer_literal_ezjsonm ~loc:pexp_loc)

let expand_list ~loc exprs =
expand_list ~loc (fun e -> [%expr `A [%e e]]) exprs
Expand All @@ -72,13 +73,13 @@ module Yojson_expander : EXPANDER = struct
| Some i -> [%expr `Int [%e Ast_builder.Default.eint ~loc i]]
| None when Integer_const.is_binary s ->
Ast_builder.Default.pexp_extension ~loc:pexp_loc
(Error.unsupported_payload ~loc:pexp_loc)
(Error.invalid_integer_literal_yojson ~loc:pexp_loc)
| None when Integer_const.is_octal s ->
Ast_builder.Default.pexp_extension ~loc:pexp_loc
(Error.unsupported_payload ~loc:pexp_loc)
(Error.invalid_integer_literal_yojson ~loc:pexp_loc)
| None when Integer_const.is_hexadecimal s ->
Ast_builder.Default.pexp_extension ~loc:pexp_loc
(Error.unsupported_payload ~loc:pexp_loc)
(Error.invalid_integer_literal_yojson ~loc:pexp_loc)
| None -> expand_intlit ~loc ~pexp_loc s

let expand_list ~loc exprs =
Expand Down
9 changes: 6 additions & 3 deletions test/rewriter/errors/run.t
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ should trigger errors:
File "test.ml", line 1, characters 35-59:
1 | let invalid_hex_literal = [%yojson 0xffffffffffffffffffffff]
^^^^^^^^^^^^^^^^^^^^^^^^
Error: ppx_yojson: unsupported payload
Error: ppx_yojson: invalid interger literal. Integer literal should fit
within an OCaml int or be written in decimal form.
[1]

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

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

--------------------------------------
Expand Down

0 comments on commit 8a96b56

Please sign in to comment.