Skip to content

Commit 54d153d

Browse files
cknittclaude
andcommitted
Compare bigint patterns and constants by their values
Why: A bigint payload pattern equal to a bigint literal case, such as `BI(2n)` beside `@as(2n) Two`, got no warning although it can never match, and a match on a bigint constant was never folded: both treated bigints as never comparable, from before literals were compared by their normalized values. Bigint pattern constants and bigint constants have their literal, so the shadowed-payload warnings and constant folding compare them as `===` does: `02n` is `2n`. Diagnostic added (shadowed bigint payload): Example: @unboxed type t = | @as(2n) Two | BI(bigint) | S(string) let f = (v: t) => switch v { | BI(02n) => "dead" | BI(_) => "bigint" | _ => "other" } Before (diagnostic): none After (diagnostic): this match case is unused. 2n is the runtime value of constructor Two, so it is always matched as Two, never as BI. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01CMkrYaktctdKc4M41WF36t Signed-off-by: Christoph Knittel <ck@cca.io>
1 parent 3e94b37 commit 54d153d

8 files changed

Lines changed: 154 additions & 87 deletions

File tree

‎compiler/ml/UntaggedVariants.md‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,8 @@ A payload pattern whose every value is a literal case's can therefore never
348348
match, and is reported: warning 11 for an arm, such as `S("a")` beside
349349
`@as("a") A`, and warning 12 for an alternative of an or-pattern, both with
350350
"\"a\" is the runtime value of constructor A, so it is always matched as A,
351-
never as S". Values are compared as `===` compares them (rule 6). For the
351+
never as S". Values are compared as `===` compares them (rule 6), so
352+
`BI(02n)` beside `@as(2n) Two` is reported too. For the
352353
exhaustiveness check a literal case covers the payload values equal to it, so
353354
`Yes | Ans(false)` is a complete match of `@as(true) Yes | Ans(bool)`.
354355

@@ -357,9 +358,12 @@ exhaustiveness check a literal case covers the payload values equal to it, so
357358
- Tests: `spec_test` rule 7; `tests/tests/src/untagged_variants_heuristic.res`
358359
(literals that skip the `switch`, and `caseBesideCases`,
359360
`nullBesideCases`, which get a `case` next to other literals);
360-
`unboxed_variant_fold_test.res` (`answerName2`); fixtures
361+
`unboxed_variant_fold_test.res` (`answerName2`, `bigintOrPayloadName`);
362+
fixtures
361363
`warning_11_untagged_literal_shadows_payload.res`,
362364
`warning_11_untagged_literal_shadows_payload_kinds.res`,
365+
`warning_11_untagged_literal_shadows_bigint.res`,
366+
`warning_11_untagged_bool_payload_overlap.res`,
363367
`warning_12_untagged_literal_shadows_or_pattern.res`.
364368
- Shaped by #6125, #7128, #7135, #8806.
365369

‎compiler/ml/lambda.ml‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -825,8 +825,9 @@ let runtime_value_kind (c : structured_constant) =
825825
| Const_js_null -> Is_literal Null
826826
| Const_js_undefined _ -> Is_literal Undefined
827827
| Const_polyvar name -> Is_literal (String name)
828-
| Const_constructor {literal = BigInt _} -> Unknown_value
829828
| Const_constructor {literal} -> Is_literal literal
829+
| Const_bigint (sign, digits) ->
830+
Is_literal (BigInt (Bigint_utils.to_string sign digits))
830831
| Const_block (Blk_tuple, _) -> Is_array
831832
| Const_block (Blk_record {fields}, _) ->
832833
if record_fields_are_array fields then Is_array else Is_object
@@ -836,13 +837,11 @@ let runtime_value_kind (c : structured_constant) =
836837
),
837838
_ ) ->
838839
Is_object
839-
| Const_char _ | Const_bigint _ | Const_some _ | Const_module_alias
840-
| Const_assertfalse ->
840+
| Const_char _ | Const_some _ | Const_module_alias | Const_assertfalse ->
841841
Unknown_value
842842

843843
(* Runtime equality, rather than equality of tags: [@as(1)] and a payload
844-
[1.0] are the same JavaScript number. Bigint spellings are not compared;
845-
both bigint payloads and bigint constructors are classified as unknown.
844+
[1.0] are the same JavaScript number, and [1_0n] and [10n] the same bigint.
846845
Integer tags are emitted through [Int32.of_int], just like [Const_int]. *)
847846
let literal_denotes_same = Variant_runtime.same_literal_value
848847

‎compiler/ml/parmatch.ml‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2291,9 +2291,8 @@ let rec pattern_literal p : Variant_runtime.literal_tag option =
22912291
| Tpat_constant (Const_string s) -> Some (String s)
22922292
| Tpat_constant (Const_int i | Const_char i) -> Some (Int i)
22932293
| Tpat_constant (Const_float f) -> Some (Float f)
2294-
| Tpat_constant (Const_bigint _) ->
2295-
(* Bigints are never compared, see [Variant_runtime.same_literal_value] *)
2296-
None
2294+
| Tpat_constant (Const_bigint (sign, digits)) ->
2295+
Some (BigInt (Bigint_utils.to_string sign digits))
22972296
| Tpat_alias (p, _, _) -> pattern_literal p
22982297
| Tpat_construct (_, {cstr_name; cstr_res}, [])
22992298
when is_type_path Predef.path_bool cstr_res ->
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
2+
Warning number 4
3+
/.../fixtures/warning_11_untagged_literal_shadows_bigint.res:3:3-7:3
4+
5+
1 │ @unboxed type t = | @as(2n) Two | BI(bigint) | S(string)
6+
2 │ let f = (v: t) =>
7+
3 │ switch v {
8+
4 │  | BI(02n) => "dead"
9+
5 │  | BI(_) => "bigint"
10+
6 │  | _ => "other"
11+
7 │  }
12+
8 │
13+
14+
this pattern-matching is fragile.
15+
It will remain exhaustive when constructors are added to type t.
16+
17+
18+
Warning number 11
19+
/.../fixtures/warning_11_untagged_literal_shadows_bigint.res:4:5-11
20+
21+
2 │ let f = (v: t) =>
22+
3 │ switch v {
23+
4 │ | BI(02n) => "dead"
24+
5 │ | BI(_) => "bigint"
25+
6 │ | _ => "other"
26+
27+
this match case is unused.
28+
2n is the runtime value of constructor Two, so it is always matched as Two, never as BI.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
@unboxed type t = | @as(2n) Two | BI(bigint) | S(string)
2+
let f = (v: t) =>
3+
switch v {
4+
| BI(02n) => "dead"
5+
| BI(_) => "bigint"
6+
| _ => "other"
7+
}

‎tests/ounit_tests/ounit_lambda_constant_tests.ml‎

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -166,13 +166,22 @@ let switch_tests =
166166
~cases:[(one, "one")]
167167
~default:(Some "default")
168168
in
169-
List.iter (assert_deferred sw)
170-
[
171-
Const_bigint (false, "1");
172-
Const_constructor {name = "DecimalTen"; literal = BigInt "1_0"};
173-
Const_char 65;
174-
Const_some (Const_int 1l);
175-
] );
169+
List.iter (assert_deferred sw) [Const_char 65; Const_some (Const_int 1l)]
170+
);
171+
( "bigints compare by value" >:: fun _ ->
172+
let one = literal "One" (BigInt "1") in
173+
let value = block "Value" UnknownType in
174+
let sw =
175+
variant_switch ~constructors:[one; value]
176+
~cases:[(one, "one")]
177+
~default:(Some "default")
178+
in
179+
assert_fold sw (Const_bigint (true, "1")) "one";
180+
assert_fold sw (Const_bigint (true, "01")) "one";
181+
assert_fold sw (Const_bigint (false, "1")) "default";
182+
assert_fold sw
183+
(Const_constructor {name = "DecimalTen"; literal = BigInt "1_0"})
184+
"default" );
176185
( "tagged switch still folds by constructor" >:: fun _ ->
177186
let runtime = runtime "Color" in
178187
let case = Variant_runtime.Block (Tagged runtime) in

0 commit comments

Comments
 (0)