Repository navigation
Commit 24a773a
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
File tree
- changes
- compiler/ml
- tests
- build_tests/super_errors
- expected
- fixtures
- tests/src
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1140 | 1140 | | |
1141 | 1141 | | |
1142 | 1142 | | |
1143 | | - | |
1144 | | - | |
1145 | | - | |
1146 | | - | |
1147 | | - | |
| 1143 | + | |
| 1144 | + | |
| 1145 | + | |
| 1146 | + | |
| 1147 | + | |
| 1148 | + | |
| 1149 | + | |
| 1150 | + | |
| 1151 | + | |
1148 | 1152 | | |
1149 | | - | |
1150 | | - | |
1151 | | - | |
1152 | | - | |
| 1153 | + | |
| 1154 | + | |
| 1155 | + | |
| 1156 | + | |
| 1157 | + | |
1153 | 1158 | | |
1154 | | - | |
| 1159 | + | |
| 1160 | + | |
| 1161 | + | |
| 1162 | + | |
| 1163 | + | |
| 1164 | + | |
| 1165 | + | |
1155 | 1166 | | |
1156 | 1167 | | |
1157 | 1168 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1423 | 1423 | | |
1424 | 1424 | | |
1425 | 1425 | | |
1426 | | - | |
1427 | | - | |
1428 | | - | |
1429 | | - | |
1430 | | - | |
1431 | | - | |
1432 | | - | |
1433 | | - | |
1434 | | - | |
1435 | | - | |
1436 | | - | |
1437 | | - | |
1438 | | - | |
| 1426 | + | |
| 1427 | + | |
| 1428 | + | |
| 1429 | + | |
| 1430 | + | |
| 1431 | + | |
| 1432 | + | |
| 1433 | + | |
| 1434 | + | |
| 1435 | + | |
| 1436 | + | |
| 1437 | + | |
| 1438 | + | |
| 1439 | + | |
| 1440 | + | |
| 1441 | + | |
| 1442 | + | |
| 1443 | + | |
| 1444 | + | |
| 1445 | + | |
| 1446 | + | |
| 1447 | + | |
| 1448 | + | |
| 1449 | + | |
| 1450 | + | |
| 1451 | + | |
| 1452 | + | |
| 1453 | + | |
| 1454 | + | |
1439 | 1455 | | |
1440 | 1456 | | |
1441 | 1457 | | |
| |||
5369 | 5385 | | |
5370 | 5386 | | |
5371 | 5387 | | |
5372 | | - | |
| 5388 | + | |
5373 | 5389 | | |
5374 | 5390 | | |
5375 | 5391 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
This file was deleted.
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | | - | |
| 3 | + | |
4 | 4 | | |
5 | 5 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
0 commit comments