Skip to content

Commit f12e8e7

Browse files
cknittclaude
andcommitted
Cover payloads by a bool literal only when they are bools
Why: The exhaustiveness check let a bool literal case cover the equal value of any boolean-kind payload, including a `@typeOf("boolean")` type that is not `bool`. The synthesized `true` pattern did not fit that type, so a match missing the payload was taken as complete. Only a payload whose type expands to `bool` gets the synthesized pattern. Diagnostic added (a @typeof("boolean") payload): Example: @typeof("boolean") type jsBool @unboxed type t = | @as(true) Yes | Value(jsBool) let f = (v: t) => switch v { | Yes => 1 } Before (diagnostic): none After (diagnostic): Warning number 8 You forgot to handle a possible case here, for example: | Value(_) 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 c8c663e commit f12e8e7

4 files changed

Lines changed: 40 additions & 5 deletions

File tree

‎compiler/ml/UntaggedVariants.md‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,10 @@ never as S". Values are compared as `===` compares them (rule 6), so
349349
`BI(02n)` beside `@as(2n) Two` and `P(#Foo)` beside `@as("Foo") F` are
350350
reported too, and an or-pattern also counts when it is named with `as`. For the
351351
exhaustiveness check a literal case covers the payload values equal to it, so
352-
`Yes | Ans(false)` is a complete match of `@as(true) Yes | Ans(bool)`.
352+
`Yes | Ans(false)` is a complete match of `@as(true) Yes | Ans(bool)`. This
353+
needs a payload of type `bool`, also through an abbreviation: a
354+
`@typeOf("boolean")` type has no `true` and `false` patterns, so `Yes` alone
355+
does not cover it.
353356

354357
- Implementation: `lam_compile.ml:compile_switch`;
355358
`parmatch.ml` (shadowed payload patterns, exhaustiveness).
@@ -360,6 +363,7 @@ exhaustiveness check a literal case covers the payload values equal to it, so
360363
`warning_11_untagged_literal_shadows_payload_kinds.res`,
361364
`warning_11_untagged_literal_shadows_bigint.res`,
362365
`warning_11_untagged_bool_payload_overlap.res`,
366+
`warning_8_untagged_boolean_kind_payload.res`,
363367
`warning_11_untagged_literal_shadows_polyvariant.res`,
364368
`warning_12_untagged_literal_shadows_or_pattern.res`,
365369
`warning_12_untagged_literal_shadows_aliased_or_pattern.res`.

‎compiler/ml/parmatch.ml‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2558,13 +2558,16 @@ let rec with_bool_payload_overlaps p =
25582558
| Constant tag -> (
25592559
match tag.literal with
25602560
| Bool b -> (
2561+
(* The payload type must be [bool] itself: a [@typeOf("boolean")]
2562+
type has boolean values, but no [true] and [false] patterns *)
25612563
let is_bool_payload cd =
2562-
match cd.cstr_kind with
2563-
| Ordinary_constructor r -> (
2564+
match (cd.cstr_kind, cd.cstr_args) with
2565+
| Ordinary_constructor r, [arg] -> (
25642566
match Variant_runtime.representation r with
2565-
| Block (Untagged {block_type = BooleanType}) -> true
2567+
| Block (Untagged {block_type = BooleanType}) ->
2568+
is_type_path Predef.path_bool (Ctype.expand_head p.pat_env arg)
25662569
| Constant _ | Block _ -> false)
2567-
| Extension_constructor _ -> false
2570+
| Ordinary_constructor _, _ | Extension_constructor _, _ -> false
25682571
in
25692572
match
25702573
List.find_opt is_bool_payload
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_untagged_boolean_kind_payload.res:4:3-6:3
4+
5+
2 │ @unboxed type t = | @as(true) Yes | Value(jsBool)
6+
3 │ let f = (v: t) =>
7+
4 │ switch v {
8+
5 │  | Yes => 1
9+
6 │  }
10+
7 │
11+
8 │ type alias = bool
12+
13+
You forgot to handle a possible case here, for example:
14+
| Value(_)
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
@typeOf("boolean") type jsBool
2+
@unboxed type t = | @as(true) Yes | Value(jsBool)
3+
let f = (v: t) =>
4+
switch v {
5+
| Yes => 1
6+
}
7+
8+
type alias = bool
9+
@unboxed type u = | @as(true) Yes2 | Value2(alias)
10+
let g = (v: u) =>
11+
switch v {
12+
| Yes2 => 1
13+
| Value2(false) => 2
14+
}

0 commit comments

Comments
 (0)