Skip to content

Commit c8aff0a

Browse files
[12.x] Fix integer range patterns at int32 boundaries (#8858)
* Backport integer range boundary fixes to v12 Backport #8717 to maintenance/12.x. Adapt matching calls and integer constant unit tests to the v12 Lambda representation, and regenerate the runtime snapshot with the v12 compiler. (cherry picked from commit 52783c8) Signed-off-by: Florian Hammerschmidt <florianh89@gmail.com> * Use legacy v12 changelog for the integer boundary backport Signed-off-by: Florian Hammerschmidt <florianh89@gmail.com> --------- Signed-off-by: Florian Hammerschmidt <florianh89@gmail.com>
1 parent 985bf35 commit c8aff0a

9 files changed

Lines changed: 688 additions & 6 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
#### :bug: Bug fix
1616

1717
- Fix integer range patterns, including nested record and exception patterns, incorrectly matching values below the range. https://github.com/rescript-lang/rescript/pull/8714
18+
- Fix integer range patterns at int32 boundaries and wrapped nondecimal patterns matching incorrect values. https://github.com/rescript-lang/rescript/pull/8717 https://github.com/rescript-lang/rescript/pull/8858
19+
- Integer patterns that alias the same int32 value (e.g. `0xFFFF_FFFF` and `-1`) may now produce unused-case warnings. https://github.com/rescript-lang/rescript/pull/8717 https://github.com/rescript-lang/rescript/pull/8858
1820

1921
#### :nail_care: Polish
2022

‎compiler/ml/matching.ml‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ open Printf
2525

2626
let dbg = false
2727

28+
let int32_low = Int32.to_int Int32.min_int
29+
let int32_high = Int32.to_int Int32.max_int
30+
2831
(* See Peyton-Jones, ``The Implementation of functional programming
2932
languages'', chapter 5. *)
3033
(*
@@ -2032,7 +2035,10 @@ let combine_constant names loc arg cst partial ctx def
20322035
| _ -> assert false)
20332036
const_lambda_list
20342037
in
2035-
call_switcher loc fail arg min_int max_int int_lambda_list names
2038+
(* Match intervals must use ReScript's int32 bounds, not the host's.
2039+
Otherwise the upper fail interval starts at 2147483648, which wraps
2040+
when converted to a Lambda integer constant. *)
2041+
call_switcher loc fail arg int32_low int32_high int_lambda_list names
20362042
| Const_char _ ->
20372043
let int_lambda_list =
20382044
List.map
@@ -2212,13 +2218,13 @@ let combine_constructor sw_names loc arg ex_pat cstr partial ctx def
22122218

22132219
let make_test_sequence_variant_constant fail arg int_lambda_list =
22142220
let _, (cases, actions) =
2215-
as_interval fail min_int max_int
2221+
as_interval fail int32_low int32_high
22162222
(List.map (fun (a, (_, c)) -> (a, c)) int_lambda_list)
22172223
in
22182224
Switcher.test_sequence arg cases actions
22192225

22202226
let call_switcher_variant_constant loc fail arg int_lambda_list names =
2221-
call_switcher loc fail arg min_int max_int
2227+
call_switcher loc fail arg int32_low int32_high
22222228
(List.map (fun (a, (_, c)) -> (a, c)) int_lambda_list)
22232229
names
22242230

@@ -2229,7 +2235,7 @@ let call_switcher_variant_constr loc fail arg int_lambda_list names =
22292235
Pgenval,
22302236
v,
22312237
Lprim (Pfield (0, Fld_poly_var_tag), [arg], loc),
2232-
call_switcher loc fail (Lvar v) min_int max_int
2238+
call_switcher loc fail (Lvar v) int32_low int32_high
22332239
(List.map (fun (a, (_, c)) -> (a, c)) int_lambda_list)
22342240
names )
22352241

‎compiler/ml/typecore.ml‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,12 @@ let type_constant = function
252252
let constant : Parsetree.constant -> (Asttypes.constant, error) result =
253253
function
254254
| Pconst_integer (i, None) -> (
255-
try Ok (Const_int (Misc.Int_literal_converter.int i))
255+
try
256+
(* After the existing host-dependent literal parsing, pattern grouping
257+
and intervals must see Lambda's int32 value, so -0xFFFF_FFFF and 1
258+
are the same key. This does not change overflow diagnostics. *)
259+
let value = Misc.Int_literal_converter.int i in
260+
Ok (Const_int (Int32.to_int (Int32.of_int value)))
256261
with Failure _ -> Error (Literal_overflow "int"))
257262
| Pconst_integer (i, Some 'n') ->
258263
let sign, i = Bigint_utils.parse_bigint i in

‎tests/build_tests/super_errors/expected/intoverflow.res.expected‎

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,5 +53,42 @@
5353
11 │ let v9: int = 0x1_0000_0000 // overflow
5454
12 │ let v10: int = -0x1_0000_0000 // underflow
5555
13 │
56+
14 │ // Wrapped aliases are redundant after int32 normalization.
5657

57-
Integer literal exceeds the range of representable integers of type int
58+
Integer literal exceeds the range of representable integers of type int
59+
60+
61+
Warning number 107
62+
/.../fixtures/intoverflow.res:17:5-14
63+
64+
15 │ let wrappedAlias = value =>
65+
16 │ switch value {
66+
17 │ | 4294967295 => 1
67+
18 │ | -1 => 2
68+
19 │ | _ => 0
69+
70+
Integer literal exceeds the range of representable integers of type int
71+
72+
73+
Warning number 11
74+
/.../fixtures/intoverflow.res:18:5-6
75+
76+
16 ┆ switch value {
77+
17 ┆ | 4294967295 => 1
78+
18 ┆ | -1 => 2
79+
19 ┆ | _ => 0
80+
20 ┆ }
81+
82+
this match case is unused.
83+
84+
85+
Warning number 11
86+
/.../fixtures/intoverflow.res:25:5
87+
88+
23 ┆ switch value {
89+
24 ┆ | -0xFFFF_FFFF => 1
90+
25 ┆ | 1 => 2
91+
26 ┆ | _ => 0
92+
27 ┆ }
93+
94+
this match case is unused.

‎tests/build_tests/super_errors/fixtures/intoverflow.res‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,18 @@ let v7: int = 0xFFFF_FFFF // -1
1010
let v8: int = -0xFFFF_FFFF // 1
1111
let v9: int = 0x1_0000_0000 // overflow
1212
let v10: int = -0x1_0000_0000 // underflow
13+
14+
// Wrapped aliases are redundant after int32 normalization.
15+
let wrappedAlias = value =>
16+
switch value {
17+
| 4294967295 => 1
18+
| -1 => 2
19+
| _ => 0
20+
}
21+
22+
let nondecimalAlias = value =>
23+
switch value {
24+
| -0xFFFF_FFFF => 1
25+
| 1 => 2
26+
| _ => 0
27+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
open OUnit
2+
3+
let suites =
4+
__FILE__
5+
>::: [
6+
( "integer pattern constants agree with Lambda" >:: fun _ ->
7+
List.iter
8+
(fun (literal, expected) ->
9+
match Typecore.constant (Pconst_integer (literal, None)) with
10+
| Ok (Asttypes.Const_int value as typed) ->
11+
assert_equal expected value;
12+
assert_equal
13+
(Lam_constant.Const_int
14+
{i = Int32.of_int expected; comment = None})
15+
(Lam_constant_convert.convert_constant
16+
(Lambda.Const_base typed))
17+
| _ -> assert_failure ("integer literal was rejected: " ^ literal))
18+
[
19+
("-0xFFFF_FFFF", 1);
20+
("0xFFFF_FFFF", -1);
21+
("2147483647", 2147483647);
22+
("-2147483648", -2147483648);
23+
] );
24+
( "overflow parsing keeps its existing host limits" >:: fun _ ->
25+
let result =
26+
Typecore.constant (Pconst_integer ("2147483649", None))
27+
in
28+
assert_equal
29+
(if Sys.int_size > 32 then Ok (Asttypes.Const_int (-2147483647))
30+
else Error (Typecore.Literal_overflow "int"))
31+
result;
32+
assert_equal (Error (Typecore.Literal_overflow "int"))
33+
(Typecore.constant
34+
(Pconst_integer ("999999999999999999999999999", None))) );
35+
]

‎tests/ounit_tests/ounit_tests_main.ml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ let suites =
2121
Ounit_bsb_regex_tests.suites;
2222
Ounit_bsb_pkg_tests.suites;
2323
Ounit_util_tests.suites;
24+
Ounit_integer_constant_tests.suites;
2425
Ounit_jsx_loc_tests.suites;
2526
Ounit_analysis_references_tests.suites;
2627
]

0 commit comments

Comments
 (0)