Skip to content

Commit 24a773a

Browse files
cknittclaude
andcommitted
Support int range patterns
`switch` now accepts int ranges such as `200 .. 299`, and bounds that mix ints and chars, since `char` is an alias of `int`. They use the `Tpat_interval` node char ranges compile to, so a range costs the same however wide it is. Ranges of other constants, such as `1.0 .. 5.0`, still report `Invalid_interval`, whose message now names int and char intervals. Int switches are never exhaustive, as before: a missing-case example is the least uncovered value from 0 up, else the greatest one below 0. Example (rescript): let status = code => switch code { | 200 .. 299 => "success" | 301 | 302 | 307 | 308 => "redirect" | 400 .. 499 => "client error" | 500 .. 599 => "server error" | _ => "unknown" } After (js): function status(code) { if (code >= 309) { if (code >= 500) { if (code >= 600) { return "unknown"; } else { return "server error"; } } else if (code >= 400) { return "client error"; } else { return "unknown"; } } if (code < 200) { return "unknown"; } if (code < 300) { return "success"; } switch (code) { case 300 : case 303 : case 304 : case 305 : case 306 : return "unknown"; case 301 : case 302 : case 307 : case 308 : return "redirect"; } } Source: tests/tests/src/int_range_test.mjs in the diff. Before this commit the example reported "Only character intervals are supported in patterns." Review: Does a switch covering all ints count as exhaustive? No. Ints are never matched exhaustively, so warning 8 still reports `_` for a switch on `-0x80000000 .. -1 | 0 .. 0x7FFFFFFF`, as it does for constants. Review: Can a mixed range give an invalid example? Codex found that a range from a char to an int covering all non-negative ints suggested a char for -1. Examples are written as chars only when they are valid code points, a fix in the parent commit; warning_8_mixed_range_example.res covers this case. Review: How was it checked? A fuzzer generated 600 switches over int, char and mixed ranges with negative bounds, or-patterns, guards, tuples and options; the results equal a reference evaluation for every input. Three programs hit `Fatal error: Matching.comp_exit`; each reduces to `| _ => ... | p1 | p2 if g() => ...`, which crashes the parent commits the same way with char patterns. Area: typechecker tests Output: unchanged, existing programs compile the same; int ranges are new Formatting: unchanged Formats: none PPX: unchanged Editor: unchanged Tests: +tests/tests/src/int_range_test.res +tests/build_tests/super_errors/fixtures/warning_8_int_range_example.res +tests/build_tests/super_errors/fixtures/warning_8_mixed_range_example.res +tests/build_tests/super_errors/fixtures/pattern_interval_float.res -tests/build_tests/super_errors/fixtures/pattern_interval_non_char.res Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01JALXUyHhkDqzcB3WuW9Svo Signed-off-by: Christoph Knittel <ck@cca.io>
1 parent 02d5ac2 commit 24a773a

12 files changed

Lines changed: 345 additions & 36 deletions
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- Int range patterns such as `200 .. 299` are supported in `switch`, and their bounds may mix ints and chars.

‎compiler/ml/parmatch.ml‎

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1140,18 +1140,29 @@ let build_other ext env : Typedtree.pattern =
11401140
_ )
11411141
:: _ ->
11421142
(* Int and char constants and intervals can share a column; the example is
1143-
the least value from 0 up that no range covers, of the kind of the
1144-
first pattern. Ranges sorted by lower bound are skipped in one pass. *)
1145-
let ranges =
1146-
List.filter_map (fun (q, _) -> int_range q) env
1147-
|> List.sort (fun (lo1, _) (lo2, _) -> compare lo1 lo2)
1143+
the least value from 0 up that no range covers, else the greatest one
1144+
below 0, of the kind of the first pattern. Ranges sorted by a bound are
1145+
skipped in one pass. *)
1146+
let ranges = List.filter_map (fun (q, _) -> int_range q) env in
1147+
let up =
1148+
List.sort (fun (lo1, _) (lo2, _) -> compare lo1 lo2) ranges
1149+
|> List.fold_left
1150+
(fun i (lo, hi) -> if lo <= i && i <= hi then hi + 1 else i)
1151+
0
11481152
in
1149-
let other =
1150-
List.fold_left
1151-
(fun i (lo, hi) -> if lo <= i && i <= hi then hi + 1 else i)
1152-
0 ranges
1153+
let down () =
1154+
List.sort (fun (_, hi1) (_, hi2) -> compare hi2 hi1) ranges
1155+
|> List.fold_left
1156+
(fun i (lo, hi) -> if lo <= i && i <= hi then lo - 1 else i)
1157+
(-1)
11531158
in
1154-
make_int_range p other other
1159+
let int32_max = Int32.to_int Int32.max_int
1160+
and int32_min = Int32.to_int Int32.min_int in
1161+
if up <= int32_max then make_int_range p up up
1162+
else
1163+
let other = down () in
1164+
(* Every int is covered; ints are never matched exhaustively *)
1165+
if other < int32_min then omega else make_int_range p other other
11551166
| (({pat_desc = Tpat_constant (Const_bigint _)} as p), _) :: _ ->
11561167
build_other_constant
11571168
(function

‎compiler/ml/typecore.ml‎

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1423,19 +1423,35 @@ and type_pat_aux ~constrs ~labels ~no_existentials ~mode ~explode ~env sp
14231423
pat_attributes = sp.ppat_attributes;
14241424
pat_env = !env;
14251425
}
1426-
| Ppat_interval (Pconst_char {semantic = c1}, Pconst_char {semantic = c2}) ->
1427-
let lo = Const_char (min c1 c2) and hi = Const_char (max c1 c2) in
1428-
unify_pat_types loc !env (type_constant lo) expected_ty;
1429-
rp k
1430-
{
1431-
pat_desc = (if c1 = c2 then Tpat_constant lo else Tpat_interval (lo, hi));
1432-
pat_loc = loc;
1433-
pat_extra = [];
1434-
pat_type = expected_ty;
1435-
pat_attributes = sp.ppat_attributes;
1436-
pat_env = !env;
1437-
}
1438-
| Ppat_interval _ -> raise (Error (loc, !env, Invalid_interval))
1426+
| Ppat_interval (c1, c2) -> (
1427+
(* [char] is an alias of [int], so bounds may mix both *)
1428+
let bound c =
1429+
match constant_or_raise !env loc c with
1430+
| (Const_int i | Const_char i) as c -> Some (i, c)
1431+
| Const_string _ | Const_float _ | Const_bigint _ -> None
1432+
in
1433+
match (bound c1, bound c2) with
1434+
| Some b1, Some b2 ->
1435+
let (i1, lo), (i2, hi) =
1436+
if fst b1 <= fst b2 then (b1, b2) else (b2, b1)
1437+
in
1438+
let ty =
1439+
match (lo, hi) with
1440+
| Const_char _, Const_char _ -> type_constant lo
1441+
| _ -> instance_def (Predef.type_int ())
1442+
in
1443+
unify_pat_types loc !env ty expected_ty;
1444+
rp k
1445+
{
1446+
pat_desc =
1447+
(if i1 = i2 then Tpat_constant lo else Tpat_interval (lo, hi));
1448+
pat_loc = loc;
1449+
pat_extra = [];
1450+
pat_type = expected_ty;
1451+
pat_attributes = sp.ppat_attributes;
1452+
pat_env = !env;
1453+
}
1454+
| _ -> raise (Error (loc, !env, Invalid_interval)))
14391455
| Ppat_tuple spl ->
14401456
assert (List.length spl >= 2);
14411457
let spl_ann = List.map (fun p -> (p, newvar ())) spl in
@@ -5369,7 +5385,7 @@ let report_error env loc ppf error =
53695385
prefix in this pattern.@]"
53705386
name Printtyp.path tpath
53715387
| Invalid_interval ->
5372-
fprintf ppf "@[Only character intervals are supported in patterns.@]"
5388+
fprintf ppf "@[Only int and char intervals are supported in patterns.@]"
53735389
| Invalid_for_loop_index ->
53745390
fprintf ppf "@[Invalid for-loop index: only variables and _ are allowed.@]"
53755391
| No_value_clauses ->
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
2+
We've found a bug for you!
3+
/.../fixtures/pattern_interval_float.res:3:5-14
4+
5+
1 │ let f = x =>
6+
2 │ switch x {
7+
3 │ | 1.0 .. 5.0 => "low"
8+
4 │ | _ => "other"
9+
5 │ }
10+
11+
Only int and char intervals are supported in patterns.

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

Lines changed: 0 additions & 11 deletions
This file was deleted.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
2+
Warning number 8
3+
/.../fixtures/warning_8_int_range_example.res:3:3-6:3
4+
5+
1 │ // All non-negative ints are covered, so the example is negative.
6+
2 │ let f = x =>
7+
3 │ switch x {
8+
4 │  | 0 .. 0x7FFFFFFF => true
9+
5 │  | -10 .. -1 => false
10+
6 │  }
11+
7 │
12+
13+
You forgot to handle a possible case here, for example:
14+
| -11
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
2+
Warning number 8
3+
/.../fixtures/warning_8_mixed_range_example.res:4:3-6:3
4+
5+
2 │ // example is a negative int, not a char.
6+
3 │ let f = x =>
7+
4 │ switch x {
8+
5 │  | '\x00' .. 0x7FFFFFFF => 1
9+
6 │  }
10+
7 │
11+
12+
You forgot to handle a possible case here, for example:
13+
| -1

tests/build_tests/super_errors/fixtures/pattern_interval_non_char.res renamed to tests/build_tests/super_errors/fixtures/pattern_interval_float.res

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
let f = x =>
22
switch x {
3-
| 1 .. 5 => "low"
3+
| 1.0 .. 5.0 => "low"
44
| _ => "other"
55
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// All non-negative ints are covered, so the example is negative.
2+
let f = x =>
3+
switch x {
4+
| 0 .. 0x7FFFFFFF => true
5+
| -10 .. -1 => false
6+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// A range from a char to an int covers all non-negative ints, so the
2+
// example is a negative int, not a char.
3+
let f = x =>
4+
switch x {
5+
| '\x00' .. 0x7FFFFFFF => 1
6+
}

0 commit comments

Comments
 (0)