Skip to content

Commit d5b3765

Browse files
authored
Merge pull request #3158 from BuckleScript/hex_float
Fix #393 in 4.06
2 parents aa298a4 + 98e750b commit d5b3765

File tree

9 files changed

+57
-2
lines changed

9 files changed

+57
-2
lines changed

jscomp/common/bs_warnings.ml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,12 @@ let warn_missing_primitive loc txt =
8787
Format.pp_print_flush warning_formatter ()
8888
end
8989

90-
90+
let warn_literal_overflow loc =
91+
begin
92+
print_string_warning loc
93+
"Integer literal exceeds the range of representable integers of type int";
94+
Format.pp_print_flush warning_formatter ()
95+
end
9196

9297
let error_unescaped_delimiter loc txt =
9398
raise (Error(loc, Uninterpreted_delimiters txt))

jscomp/common/bs_warnings.mli

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,7 @@ val prerr_bs_ffi_warning : Location.t -> t -> unit
3131

3232
val warn_missing_primitive : Location.t -> string -> unit
3333

34+
val warn_literal_overflow : Location.t -> unit
35+
3436
val error_unescaped_delimiter :
3537
Location.t -> string -> unit

jscomp/syntax/bs_ast_invariant.ml

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ let emit_external_warnings : iterator=
8686
default_iterator with
8787
attribute = (fun _ attr -> warn_unused_attribute attr);
8888
expr = (fun self a ->
89-
match a.Parsetree.pexp_desc with
89+
match a.pexp_desc with
9090
| Pexp_constant (
9191
#if OCAML_VERSION =~ ">4.03.0" then
9292
Pconst_string
@@ -96,6 +96,23 @@ let emit_external_warnings : iterator=
9696
(_, Some s))
9797
when Ast_utf8_string_interp.is_unescaped s ->
9898
Bs_warnings.error_unescaped_delimiter a.pexp_loc s
99+
#if OCAML_VERSION =~ ">4.03.0" then
100+
| Pexp_constant(Pconst_integer(s,None)) ->
101+
(* range check using int32
102+
It is better to give a warning instead of error to avoid make people unhappy.
103+
It also has restrictions in which platform bsc is running on since it will
104+
affect int ranges
105+
*)
106+
(
107+
try
108+
ignore (
109+
if String.length s = 0 || s.[0] = '-' then
110+
Int32.of_string s
111+
else Int32.of_string ("-" ^ s))
112+
with _ ->
113+
Bs_warnings.warn_literal_overflow a.pexp_loc
114+
)
115+
#end
99116
| _ -> default_iterator.expr self a
100117
);
101118
value_description =

jscomp/test/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,7 @@ OTHERS := test_literals a test_ari test_export2 test_internalOO test_obj_simple_
269269
fun_pattern_match\
270270
gpr_3142_test\
271271
gpr_3154_test\
272+
gpr_373_test\
272273
ocaml_typedtree_test
273274
# ocaml_typedtree_test is not cross version due to camlinternalFormat
274275
# bs_uncurry_test

jscomp/test/gpr_373_test.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
'use strict';
2+
3+
4+
var overflow_dec_i32_number = 272872590;
5+
6+
var overflow_dec_i32_number_2 = 0;
7+
8+
var not_overflow_dec_i32_number_3 = -1;
9+
10+
var overflow_hex_i32_number = -1;
11+
12+
exports.overflow_dec_i32_number = overflow_dec_i32_number;
13+
exports.overflow_dec_i32_number_2 = overflow_dec_i32_number_2;
14+
exports.not_overflow_dec_i32_number_3 = not_overflow_dec_i32_number_3;
15+
exports.overflow_hex_i32_number = overflow_hex_i32_number;
16+
/* No side effect */

jscomp/test/gpr_373_test.ml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
2+
3+
let overflow_dec_i32_number = 949_460_645_006
4+
5+
let overflow_dec_i32_number_2 = 4611686018427387904 (* max_int + 1*)
6+
7+
let overflow_dec_i32_number_2 = 4611686018427387904
8+
9+
let not_overflow_dec_i32_number_3 = 0xffff_ffff
10+
let overflow_hex_i32_number = 0xffff_ffffl

jscomp/xwatcher/xwatcher_current.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ if (exit === 1) {
8080
"common",
8181
"super_errors",
8282
".",
83+
"stubs",
8384
"stdlib-406"
8485
]);
8586
exec(/* () */0);

jscomp/xwatcher/xwatcher_current.ml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ let () =
5757
"common";
5858
"super_errors";
5959
".";
60+
"stubs";
61+
6062
"stdlib-406" ; (* evolve *)
6163
|];
6264

scripts/watcher.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,7 @@ if (exit === 1) {
260260
"common",
261261
"super_errors",
262262
".",
263+
"stubs",
263264
"stdlib-406"
264265
]);
265266
exec(/* () */0);

0 commit comments

Comments
 (0)