From 54ab8075cc6ce57b1b5e477e691af5ce6dbd1075 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20V=C3=A1zquez=20Acosta?= Date: Fri, 2 Oct 2026 15:20:40 +0200 Subject: [PATCH 1/2] Fix signed range check in integer patterns on non-variables MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Matching `{status: 502 | 503 | 504}` compiled to `(error.status - 502 | 0) <= 2`, which is also true for any status below 502. The fallback case of `Js_exp_make.is_out` must compare the offset value as unsigned; the `to_uint32` conversion was lost when the Caml runtimes were dropped in 6d78c5832781165685b45bb2092d8edf170b75ae. Fixes https://github.com/rescript-lang/rescript/issues/8713 Co-Authored-By: Claude Opus 5.5 (1M context) Signed-off-by: Manuel Vázquez Acosta --- compiler/core/js_exp_make.ml | 2 +- tests/tests/src/or_pattern_int_range_test.mjs | 35 +++++++++++++++++++ tests/tests/src/or_pattern_int_range_test.res | 20 +++++++++++ 3 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 tests/tests/src/or_pattern_int_range_test.mjs create mode 100644 tests/tests/src/or_pattern_int_range_test.res diff --git a/compiler/core/js_exp_make.ml b/compiler/core/js_exp_make.ml index 210c0a58dd9..1f708e5ebfc 100644 --- a/compiler/core/js_exp_make.ml +++ b/compiler/core/js_exp_make.ml @@ -1512,7 +1512,7 @@ let rec is_out ?comment (e : t) (range : t) : t = {expression_desc = Number (Int {i = 0l}); _} ) ) -> (* TODO: check correctness *) is_out ?comment e range - | _, _ -> int_comp ?comment Cgt e range + | _, _ -> int_comp ?comment Cgt (int32_lsr e zero_int_literal) range let rec float_add ?comment (e1 : t) (e2 : t) = match (e1.expression_desc, e2.expression_desc) with diff --git a/tests/tests/src/or_pattern_int_range_test.mjs b/tests/tests/src/or_pattern_int_range_test.mjs new file mode 100644 index 00000000000..c06a8b24313 --- /dev/null +++ b/tests/tests/src/or_pattern_int_range_test.mjs @@ -0,0 +1,35 @@ +// Generated by ReScript, PLEASE EDIT WITH CARE + +import * as Mocha from "mocha"; +import * as Test_utils from "./test_utils.mjs"; + +function isRetriable(error) { + return (error.status - 502 >>> 0) <= 2; +} + +Mocha.describe("Or_pattern_int_range_test", () => { + Mocha.test("int range on record field", () => Test_utils.eq("File \"or_pattern_int_range_test.res\", line 15, characters 6-13", [ + 404, + 500, + 501, + 502, + 503, + 504, + 505 + ].map(status => isRetriable({ + status: status + })), [ + false, + false, + false, + true, + true, + true, + false + ])); +}); + +export { + isRetriable, +} +/* Not a pure module */ diff --git a/tests/tests/src/or_pattern_int_range_test.res b/tests/tests/src/or_pattern_int_range_test.res new file mode 100644 index 00000000000..90735ac135e --- /dev/null +++ b/tests/tests/src/or_pattern_int_range_test.res @@ -0,0 +1,20 @@ +open Mocha +open Test_utils + +type response = {status: int} + +let isRetriable = error => + switch error { + | {status: 502 | 503 | 504} => true + | _ => false + } + +describe(__MODULE__, () => { + test("int range on record field", () => { + eq( + __LOC__, + [404, 500, 501, 502, 503, 504, 505]->Array.map(status => isRetriable({status: status})), + [false, false, false, true, true, true, false], + ) + }) +}) From 238a808b508e2ee4c7678f0fd854c032c4240e69 Mon Sep 17 00:00:00 2001 From: Florian Hammerschmidt Date: Fri, 2 Oct 2026 17:55:34 +0200 Subject: [PATCH 2/2] Document integer range pattern fix in changelog Signed-off-by: Florian Hammerschmidt --- CHANGELOG.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 80bece99bd1..2bbb28a09bc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,12 @@ > - :nail_care: [Polish] > - :house: [Internal] +# 12.3.2 (Unreleased) + +#### :bug: Bug fix + +- Fix integer range patterns, including nested record and exception patterns, incorrectly matching values below the range. https://github.com/rescript-lang/rescript/pull/8714 + # 12.3.1 #### :bug: Bug fix