Skip to content

Commit b2254f9

Browse files
cknittclaude
andcommitted
Report shadowed polymorphic variant and aliased or-pattern payloads
Why: A polymorphic variant payload equal to a literal case and the alternatives of an or-pattern named with `as` got no warning, though they can never match. A polymorphic variant without payload stands for its name, or the number its name spells, as in `Lambda.const_polyvar`, and an alias is looked through when collecting or-pattern alternatives. Diagnostic added (a shadowed polymorphic variant payload): Example: @unboxed type t = | @as("Foo") F | P([#Foo | #Bar]) let f = x => switch x { | F => 1 | P(#Foo) => 2 | P(_) => 3 } Before (diagnostic): none After (diagnostic): Warning number 11 this match case is unused. "Foo" is the runtime value of constructor F, so it is always matched as F, never as P. Diagnostic added (an aliased or-pattern): Example: @unboxed type t = | @as("a") A | S(string) let f = x => switch x { | S(("a" | "b") as s) => s | S(_) | A => "other" } Before (diagnostic): none After (diagnostic): Warning number 12 this sub-pattern is unused. "a" is the runtime value of constructor A, so it is always matched as A, never as S. 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 54d153d commit b2254f9

6 files changed

Lines changed: 72 additions & 3 deletions

‎compiler/ml/UntaggedVariants.md‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,8 @@ 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,
351351
never as S". Values are compared as `===` compares them (rule 6), so
352-
`BI(02n)` beside `@as(2n) Two` is reported too. For the
352+
`BI(02n)` beside `@as(2n) Two` and `P(#Foo)` beside `@as("Foo") F` are
353+
reported too, and an or-pattern also counts when it is named with `as`. For the
353354
exhaustiveness check a literal case covers the payload values equal to it, so
354355
`Yes | Ans(false)` is a complete match of `@as(true) Yes | Ans(bool)`.
355356

@@ -364,7 +365,9 @@ exhaustiveness check a literal case covers the payload values equal to it, so
364365
`warning_11_untagged_literal_shadows_payload_kinds.res`,
365366
`warning_11_untagged_literal_shadows_bigint.res`,
366367
`warning_11_untagged_bool_payload_overlap.res`,
367-
`warning_12_untagged_literal_shadows_or_pattern.res`.
368+
`warning_11_untagged_literal_shadows_polyvariant.res`,
369+
`warning_12_untagged_literal_shadows_or_pattern.res`,
370+
`warning_12_untagged_literal_shadows_aliased_or_pattern.res`.
368371
- Shaped by #6125, #7128, #7135, #8806.
369372

370373
### 8. Payload cases

‎compiler/ml/parmatch.ml‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2294,6 +2294,12 @@ let rec pattern_literal p : Variant_runtime.literal_tag option =
22942294
| Tpat_constant (Const_bigint (sign, digits)) ->
22952295
Some (BigInt (Bigint_utils.to_string sign digits))
22962296
| Tpat_alias (p, _, _) -> pattern_literal p
2297+
| Tpat_variant (label, None, _) ->
2298+
(* A polymorphic variant is its name, or the number its name spells, as
2299+
in [Lambda.const_polyvar] *)
2300+
if Ext_string.is_valid_hash_number label then
2301+
Some (Int (Int32.to_int (Ext_string.hash_number_as_i32_exn label)))
2302+
else Some (String label)
22972303
| Tpat_construct (_, {cstr_name; cstr_res}, [])
22982304
when is_type_path Predef.path_bool cstr_res ->
22992305
Some (Bool (cstr_name = "true"))
@@ -2310,7 +2316,8 @@ let rec pattern_literal p : Variant_runtime.literal_tag option =
23102316
pattern_literal arg
23112317
| (Constant _ | Block _), _ -> None)
23122318
| Tpat_construct (_, {cstr_kind = Extension_constructor _}, _)
2313-
| Tpat_any | Tpat_var _ | Tpat_interval _ | Tpat_tuple _ | Tpat_variant _
2319+
| Tpat_any | Tpat_var _ | Tpat_interval _ | Tpat_tuple _
2320+
| Tpat_variant (_, Some _, _)
23142321
| Tpat_record _ | Tpat_dict _ | Tpat_array _ | Tpat_or _ ->
23152322
None
23162323

@@ -2367,6 +2374,8 @@ and shadowed_argument_liveness ~payload_constructor ~literal_cases arg =
23672374
or_liveness
23682375
(shadowed_argument_liveness ~payload_constructor ~literal_cases)
23692376
p1 p2
2377+
| Tpat_alias (p, _, _) ->
2378+
shadowed_argument_liveness ~payload_constructor ~literal_cases p
23702379
| _ -> (
23712380
let shadowing_case =
23722381
match pattern_literal arg with
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Warning number 11
3+
/.../fixtures/warning_11_untagged_literal_shadows_polyvariant.res:9:5-11
4+
5+
7 ┆ | F => 1
6+
8 ┆ | One => 2
7+
9 ┆ | P(#Foo) => 3
8+
10 ┆ | P(#1) => 4
9+
11 ┆ | P(_) => 5
10+
11+
this match case is unused.
12+
"Foo" is the runtime value of constructor F, so it is always matched as F, never as P.
13+
14+
15+
Warning number 11
16+
/.../fixtures/warning_11_untagged_literal_shadows_polyvariant.res:10:5-9
17+
18+
8 ┆ | One => 2
19+
9 ┆ | P(#Foo) => 3
20+
10 ┆ | P(#1) => 4
21+
11 ┆ | P(_) => 5
22+
12 ┆ }
23+
24+
this match case is unused.
25+
1 is the runtime value of constructor One, so it is always matched as One, never as P.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
2+
Warning number 12
3+
/.../fixtures/warning_12_untagged_literal_shadows_aliased_or_pattern.res:6:8-10
4+
5+
4 │ let f = x =>
6+
5 │ switch x {
7+
6 │ | S(("a" | "b") as s) => s
8+
7 │ | S(_) | A => "other"
9+
8 │ }
10+
11+
this sub-pattern is unused.
12+
"a" is the runtime value of constructor A, so it is always matched as A, never as S.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// A polymorphic variant payload is its name at run time, so #Foo is always
2+
// matched as F, and #1 as One.
3+
@unboxed type t = | @as("Foo") F | @as(1) One | P([#Foo | #Bar | #1])
4+
5+
let f = x =>
6+
switch x {
7+
| F => 1
8+
| One => 2
9+
| P(#Foo) => 3
10+
| P(#1) => 4
11+
| P(_) => 5
12+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// The "a" alternative of an aliased or-pattern can never match either.
2+
@unboxed type t = | @as("a") A | S(string)
3+
4+
let f = x =>
5+
switch x {
6+
| S(("a" | "b") as s) => s
7+
| S(_) | A => "other"
8+
}

0 commit comments

Comments
 (0)