diff --git a/CHANGELOG.md b/CHANGELOG.md index 5868d208c03..85c15705c10 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -72,6 +72,7 @@ These are only breaking changes for unformatted code. - Fix location issue for the treatment of `async` functions where hovering on the body with a type error would show `'a => promise<'a>` everywhere https://github.com/rescript-lang/rescript-compiler/pull/6012 - Fix formatting of `switch` expressions that contain brace `cases` inside https://github.com/rescript-lang/rescript-compiler/pull/6015 - Support `@gentype.import` as an alias to `@genType.import` in the compiler https://github.com/rescript-lang/rescript-compiler/pull/6020 +- Fix issue with integer overflow check https://github.com/rescript-lang/rescript-compiler/pull/6028 #### :nail_care: Polish diff --git a/jscomp/build_tests/super_errors/expected/intoverflow.res.expected b/jscomp/build_tests/super_errors/expected/intoverflow.res.expected new file mode 100644 index 00000000000..abf0e9f4490 --- /dev/null +++ b/jscomp/build_tests/super_errors/expected/intoverflow.res.expected @@ -0,0 +1,57 @@ + + Warning number 107 + /.../fixtures/intoverflow.res:2:15-27 + + 1 │ let v1: int = 2_147_483_647 // max int + 2 │ let v2: int = 2_147_483_648 // overflow + 3 │ let v3: int = 2_147_483_649 // overflow + 4 │ let v4: int = -2_147_483_647 + + Integer literal exceeds the range of representable integers of type int + + + Warning number 107 + /.../fixtures/intoverflow.res:3:15-27 + + 1 │ let v1: int = 2_147_483_647 // max int + 2 │ let v2: int = 2_147_483_648 // overflow + 3 │ let v3: int = 2_147_483_649 // overflow + 4 │ let v4: int = -2_147_483_647 + 5 │ let v5: int = -2_147_483_648 // min int + + Integer literal exceeds the range of representable integers of type int + + + Warning number 107 + /.../fixtures/intoverflow.res:6:16-28 + + 4 │ let v4: int = -2_147_483_647 + 5 │ let v5: int = -2_147_483_648 // min int + 6 │ let v6: int = -2_147_483_649 // underflow + 7 │ + 8 │ // hex + + Integer literal exceeds the range of representable integers of type int + + + Warning number 107 + /.../fixtures/intoverflow.res:11:14-26 + + 9 │ let v7: int = 0xFFFF_FFFF // -1 + 10 │ let v8: int = -0xFFFF_FFFF // 1 + 11 │ let v9:int = 0x1_0000_0000 // overflow + 12 │ let v10:int = -0x1_0000_0000 // underflow + 13 │ + + Integer literal exceeds the range of representable integers of type int + + + Warning number 107 + /.../fixtures/intoverflow.res:12:16-28 + + 10 │ let v8: int = -0xFFFF_FFFF // 1 + 11 │ let v9:int = 0x1_0000_0000 // overflow + 12 │ let v10:int = -0x1_0000_0000 // underflow + 13 │ + + Integer literal exceeds the range of representable integers of type int \ No newline at end of file diff --git a/jscomp/build_tests/super_errors/fixtures/intoverflow.res b/jscomp/build_tests/super_errors/fixtures/intoverflow.res new file mode 100644 index 00000000000..b1b3981e8a6 --- /dev/null +++ b/jscomp/build_tests/super_errors/fixtures/intoverflow.res @@ -0,0 +1,12 @@ +let v1: int = 2_147_483_647 // max int +let v2: int = 2_147_483_648 // overflow +let v3: int = 2_147_483_649 // overflow +let v4: int = -2_147_483_647 +let v5: int = -2_147_483_648 // min int +let v6: int = -2_147_483_649 // underflow + +// hex +let v7: int = 0xFFFF_FFFF // -1 +let v8: int = -0xFFFF_FFFF // 1 +let v9:int = 0x1_0000_0000 // overflow +let v10:int = -0x1_0000_0000 // underflow diff --git a/jscomp/frontend/bs_ast_invariant.ml b/jscomp/frontend/bs_ast_invariant.ml index 9ac4f0b63dd..2e2b3ac658d 100644 --- a/jscomp/frontend/bs_ast_invariant.ml +++ b/jscomp/frontend/bs_ast_invariant.ml @@ -89,9 +89,7 @@ let check_constant loc kind (const : Parsetree.constant) = affect int ranges *) try - ignore - (if String.length s = 0 || s.[0] = '-' then Int32.of_string s - else Int32.of_string ("-" ^ s)) + ignore @@ Int32.of_string s with _ -> Bs_warnings.warn_literal_overflow loc) | Pconst_integer (_, Some 'n') -> Location.raise_errorf ~loc "literal with `n` suffix is not supported"