Skip to content

Commit cc6be0b

Browse files
cknittclaude
andauthored
Represent char range patterns with a typed-tree node
A char range pattern such as `'a' .. 'z'` used to be expanded by the type checker into an or-pattern with one constant per character, so compile time and memory grew with the width of the range. It is now one `Tpat_interval` node. Exhaustiveness and match compilation split each column at the bounds of its ranges into pieces that every range covers fully or not at all, so the work depends on the number of patterns, not on the number of characters. Missing-case warnings show ranges instead of listing every character. Example (rescript): let f = (a, b) => switch (a, b) { | ('a' .. 'z', 'a' .. 'z') => 1 | (_, '\u{0}' .. '`') => 2 } Before (text): You forgot to handle a possible case here, for example: | ('t', '{') | ('u', '{') | ('v', '{') | ('w', '{') | ('x', '{') | ('y', '{') | ('z', '{') After (text): You forgot to handle a possible case here, for example: | ('a' .. 'z', '{') | ('\x00', 'a') Source: tests/build_tests/super_errors/fixtures/warning_8_char_range_example.res, compiled with the parent commit and with this one. Review: Is the generated JavaScript the same? Every committed snapshot is byte-identical. A differential fuzzer (900 random switches over overlapping char ranges, or-patterns, guards, tuples and options) compiled each program with the parent commit and with this one: results equal a reference evaluation for every input, warning numbers and locations are equal, and the JS differs in shape for some programs and is 0.4% smaller in total (296,645 -> 295,495 characters). Review: Why are intervals precompiled like or-patterns? An expanded range used to be an or-pattern, whose row is compiled once in a handler. Doing the same for an interval whose row tests further columns keeps that sharing; dividing such rows by piece instead made the fuzzer's output 16% larger. Review: Why is the action of a range stored as shared? The switch emits it for each value of the range. An expanded range shared it the same way, because its values had equal actions; a copy per value would declare the action's variables twice in one JavaScript scope. Review: Are int ranges such as `1 .. 5` accepted? Not yet; they still report `Invalid_interval`. The node and the algorithms already work on int and char constants, for a follow-up. Area: typechecker lambda analysis tests Output: unchanged, all committed generated JS is byte-identical (char_range_test.mjs changes with its source) Formatting: unchanged Formats: cmt (Caml1999T045 -> Caml1999T046) PPX: unchanged, Ppat_interval already exists in the v0 parsetree Editor: unchanged; analysis visits the new node like a constant Errors: ERROR_VARIANTS.md warnings section lists the two new fixtures Tests: ~tests/tests/src/char_range_test.res +tests/build_tests/super_errors/fixtures/warning_8_char_range_example.res +tests/build_tests/super_errors/fixtures/warning_11_char_range_covered.res Measured: switch on '\u{0}' .. '\u{10FFFF}': 15.75 s and 1.78 GB -> 0.02 s and 19 MB (bsc wall time and max RSS, /usr/bin/time, parent commit vs this one); switch on '\u{0}' .. '\u{FFFF}': 0.55 s and 135 MB -> 0.02 s and 20 MB 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 001621d commit cc6be0b

23 files changed

Lines changed: 680 additions & 143 deletions

‎analysis/src/process_cmt.ml‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -535,7 +535,9 @@ let rec for_structure_item ~(env : Shared_types.Env.t) ~(exported : Exported.t)
535535
|> List.iter (fun {Typedtree.tdp_pattern} ->
536536
handle_pattern [] tdp_pattern)
537537
| Tpat_variant (_, Some p, _) -> handle_pattern [] p
538-
| Tpat_variant (_, None, _) | Tpat_any | Tpat_constant _ -> ()
538+
| Tpat_variant (_, None, _) | Tpat_any | Tpat_constant _ | Tpat_interval _
539+
->
540+
()
539541
in
540542
List.iter
541543
(fun {Typedtree.vb_pat; vb_attributes} ->
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- Char range patterns such as `'\u{0}' .. '\u{10FFFF}'` compile as fast as a single character, and missing-case warnings show ranges instead of listing every character.

‎compiler/common/pattern_printer.ml‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ let untype typed =
6666
| Tpat_or (pa, pb, _) -> mkpat (Ppat_or (loop pa, loop pb))
6767
| Tpat_any | Tpat_var _ -> mkpat Ppat_any
6868
| Tpat_constant c -> mkpat (Ppat_constant (Untypeast.constant c))
69+
| Tpat_interval (c1, c2) ->
70+
mkpat (Ppat_interval (Untypeast.constant c1, Untypeast.constant c2))
6971
| Tpat_alias (p, _, _) -> loop p
7072
| Tpat_tuple lst -> mkpat (Ppat_tuple (List.map loop lst))
7173
| Tpat_construct (cstr_lid, cstr, lst) ->
@@ -109,9 +111,9 @@ let untype typed =
109111
| Tpat_construct (_, cstr, [value]) when cstr.cstr_name = "Some" ->
110112
Some
111113
{pdp_key = tdp_key; pdp_pattern = loop value; pdp_optional = false}
112-
| Tpat_var _ | Tpat_alias _ | Tpat_constant _ | Tpat_tuple _
113-
| Tpat_construct _ | Tpat_variant _ | Tpat_record _ | Tpat_dict _
114-
| Tpat_array _ | Tpat_or _ ->
114+
| Tpat_var _ | Tpat_alias _ | Tpat_constant _ | Tpat_interval _
115+
| Tpat_tuple _ | Tpat_construct _ | Tpat_variant _ | Tpat_record _
116+
| Tpat_dict _ | Tpat_array _ | Tpat_or _ ->
115117
Some
116118
{
117119
pdp_key = tdp_key;

‎compiler/ext/config.ml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ and ast0_impl_magic_number = "Caml1999M022"
1313

1414
and ast0_intf_magic_number = "Caml1999N022"
1515

16-
and cmt_magic_number = "Caml1999T045"
16+
and cmt_magic_number = "Caml1999T046"
1717

1818
let get_load_path () = (Compiler_request_state.current ()).load_path
1919
let set_load_path path = (Compiler_request_state.current ()).load_path <- path

‎compiler/ml/dict_pattern.ml‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ open Typedtree
99
let rec contains_dict p =
1010
match p.pat_desc with
1111
| Tpat_dict _ -> true
12-
| Tpat_any | Tpat_var _ | Tpat_constant _ | Tpat_variant (_, None, _) -> false
12+
| Tpat_any | Tpat_var _ | Tpat_constant _ | Tpat_interval _
13+
| Tpat_variant (_, None, _) ->
14+
false
1315
| Tpat_alias (p, _, _) | Tpat_variant (_, Some p, _) -> contains_dict p
1416
| Tpat_tuple ps | Tpat_construct (_, _, ps) | Tpat_array ps ->
1517
List.exists contains_dict ps

0 commit comments

Comments
 (0)