Skip to content

Commit 8cb50ef

Browse files
cknittclaude
andcommitted
Keep outer literal cases when looking into nested untagged payloads
Why: Below a second untagged constructor, an or-pattern was checked against the inner variant's literal cases only, although the outer variant's literal cases are tested first on the same value. The literal cases that take precedence are carried down through untagged payloads, outermost first, each with the payload constructor it shadows. Diagnostic added (an or-pattern below a nested untagged payload): Example: @unboxed type inner = | @as("x") X | N(int) | S(string) @unboxed type outer = | @as(1) One | W(inner) let f = v => switch v { | One => "one" | W(N(1 | 2)) => "small" | W(_) => "other" } Before (diagnostic): none After (diagnostic): Warning number 12 this sub-pattern is unused. 1 is the runtime value of constructor One, so it is always matched as One, never as W. 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 7c05b9a commit 8cb50ef

4 files changed

Lines changed: 112 additions & 27 deletions

File tree

‎compiler/ml/UntaggedVariants.md‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,9 @@ match, and is reported: warning 11 for an arm, such as `S("a")` beside
347347
"\"a\" is the runtime value of constructor A, so it is always matched as A,
348348
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
350-
reported too, and an or-pattern also counts when it is named with `as`. For the
350+
reported too, and an or-pattern also counts when it is named with `as`. An
351+
untagged payload inside another is the value itself, so the outer literal
352+
cases also shadow it: `W(N(1 | 2))` reports `1` beside `@as(1) One | W(inner)`. For the
351353
exhaustiveness check a literal case covers the payload values equal to it, so
352354
`Yes | Ans(false)` is a complete match of `@as(true) Yes | Ans(bool)`. This
353355
needs a payload of type `bool`, also through an abbreviation: a
@@ -366,7 +368,8 @@ does not cover it.
366368
`warning_8_untagged_boolean_kind_payload.res`,
367369
`warning_11_untagged_literal_shadows_polyvariant.res`,
368370
`warning_12_untagged_literal_shadows_or_pattern.res`,
369-
`warning_12_untagged_literal_shadows_aliased_or_pattern.res`.
371+
`warning_12_untagged_literal_shadows_aliased_or_pattern.res`,
372+
`warning_12_untagged_literal_shadows_nested_payload.res`.
370373
- Shaped by #6125, #7128, #7135, #8806.
371374

372375
### 8. Payload cases

‎compiler/ml/parmatch.ml‎

Lines changed: 39 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2321,21 +2321,32 @@ let rec pattern_literal p : Variant_runtime.literal_tag option =
23212321
| Tpat_record _ | Tpat_dict _ | Tpat_array _ | Tpat_or _ ->
23222322
None
23232323

2324-
(* The literal cases that take precedence over an untagged payload constructor,
2325-
as runtime values and constructor names *)
2326-
let shadowing_literal_cases cd =
2324+
let is_untagged_payload cd =
23272325
match cd.cstr_kind with
23282326
| Ordinary_constructor r -> (
23292327
match Variant_runtime.representation r with
2330-
| Block (Untagged _) ->
2331-
let layout = Variant_runtime.get_layout r.variant in
2332-
List.init (Variant_runtime.length layout) Fun.id
2333-
|> List.filter_map (fun position ->
2334-
match Variant_runtime.constructor_at layout position with
2335-
| Constant tag -> Some (tag.literal, tag.name)
2336-
| Block _ -> None)
2337-
| Constant _ | Block (Tagged _) -> [])
2338-
| Extension_constructor _ -> []
2328+
| Block (Untagged _) -> true
2329+
| Constant _ | Block (Tagged _) -> false)
2330+
| Extension_constructor _ -> false
2331+
2332+
(* The literal cases that take precedence over an untagged payload
2333+
constructor *)
2334+
let shadowing_literal_cases cd =
2335+
match cd.cstr_kind with
2336+
| Ordinary_constructor r when is_untagged_payload cd ->
2337+
let layout = Variant_runtime.get_layout r.variant in
2338+
List.init (Variant_runtime.length layout) Fun.id
2339+
|> List.filter_map (fun position ->
2340+
match Variant_runtime.constructor_at layout position with
2341+
| Constant tag ->
2342+
Some
2343+
{
2344+
payload_constructor = cd.cstr_name;
2345+
value = tag.literal;
2346+
literal_constructor = tag.name;
2347+
}
2348+
| Block _ -> None)
2349+
| Ordinary_constructor _ | Extension_constructor _ -> []
23392350

23402351
let or_liveness liveness p1 p2 =
23412352
match (liveness p1, liveness p2) with
@@ -2350,9 +2361,7 @@ let rec payload_liveness p =
23502361
| Tpat_construct (_, cd, [arg]) -> (
23512362
match shadowing_literal_cases cd with
23522363
| [] -> subpatterns_liveness p
2353-
| literal_cases ->
2354-
shadowed_argument_liveness ~payload_constructor:cd.cstr_name
2355-
~literal_cases arg)
2364+
| shadowing -> shadowed_argument_liveness ~shadowing arg)
23562365
| _ -> subpatterns_liveness p
23572366

23582367
(* A pattern that is not an or-pattern matches nothing as soon as one of its
@@ -2368,26 +2377,31 @@ and subpatterns_liveness p =
23682377
p.pat_desc;
23692378
!result
23702379

2371-
and shadowed_argument_liveness ~payload_constructor ~literal_cases arg =
2380+
(* [shadowing] lists the literal cases that take precedence over the value
2381+
[arg] matches, outermost first *)
2382+
and shadowed_argument_liveness ~shadowing arg =
23722383
match arg.pat_desc with
23732384
| Tpat_or (p1, p2, _) ->
2374-
or_liveness
2375-
(shadowed_argument_liveness ~payload_constructor ~literal_cases)
2376-
p1 p2
2377-
| Tpat_alias (p, _, _) ->
2378-
shadowed_argument_liveness ~payload_constructor ~literal_cases p
2385+
or_liveness (shadowed_argument_liveness ~shadowing) p1 p2
2386+
| Tpat_alias (p, _, _) -> shadowed_argument_liveness ~shadowing p
2387+
| Tpat_construct (_, cd, [inner]) when is_untagged_payload cd ->
2388+
(* An untagged payload is the value itself, so the literal cases around
2389+
it still take precedence, before its own *)
2390+
shadowed_argument_liveness
2391+
~shadowing:(shadowing @ shadowing_literal_cases cd)
2392+
inner
23792393
| _ -> (
23802394
let shadowing_case =
23812395
match pattern_literal arg with
23822396
| Some value ->
23832397
List.find_opt
2384-
(fun (literal, _) -> Variant_runtime.same_literal_value value literal)
2385-
literal_cases
2398+
(fun {value = literal} ->
2399+
Variant_runtime.same_literal_value value literal)
2400+
shadowing
23862401
| None -> None
23872402
in
23882403
match shadowing_case with
2389-
| Some (value, literal_constructor) ->
2390-
Never_matches [{payload_constructor; value; literal_constructor}]
2404+
| Some shadowed -> Never_matches [shadowed]
23912405
| None -> payload_liveness arg)
23922406

23932407
let literal_to_string : Variant_runtime.literal_tag -> string = function
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
2+
Warning number 4
3+
/.../fixtures/warning_12_untagged_literal_shadows_nested_payload.res:8:3-14:3
4+
5+
6 │
6+
7 │ let f = v =>
7+
8 │ switch v {
8+
9 │  | One => "one"
9+
. │ ...
10+
13 │  | W(_) => "other"
11+
14 │  }
12+
15 │
13+
14+
this pattern-matching is fragile.
15+
It will remain exhaustive when constructors are added to type inner.
16+
17+
18+
Warning number 12
19+
/.../fixtures/warning_12_untagged_literal_shadows_nested_payload.res:10:9
20+
21+
8 ┆ switch v {
22+
9 ┆ | One => "one"
23+
10 ┆ | W(N(1 | 2)) => "small"
24+
11 ┆ | W(S("x" | "y")) => "letter"
25+
12 ┆ | W(N(1)) => "dead"
26+
27+
this sub-pattern is unused.
28+
1 is the runtime value of constructor One, so it is always matched as One, never as W.
29+
30+
31+
Warning number 12
32+
/.../fixtures/warning_12_untagged_literal_shadows_nested_payload.res:11:9-11
33+
34+
9 ┆ | One => "one"
35+
10 ┆ | W(N(1 | 2)) => "small"
36+
11 ┆ | W(S("x" | "y")) => "letter"
37+
12 ┆ | W(N(1)) => "dead"
38+
13 ┆ | W(_) => "other"
39+
40+
this sub-pattern is unused.
41+
"x" is the runtime value of constructor X, so it is always matched as X, never as S.
42+
43+
44+
Warning number 11
45+
/.../fixtures/warning_12_untagged_literal_shadows_nested_payload.res:12:5-11
46+
47+
10 ┆ | W(N(1 | 2)) => "small"
48+
11 ┆ | W(S("x" | "y")) => "letter"
49+
12 ┆ | W(N(1)) => "dead"
50+
13 ┆ | W(_) => "other"
51+
14 ┆ }
52+
53+
this match case is unused.
54+
1 is the runtime value of constructor One, so it is always matched as One, never as W.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// An untagged payload inside another is the value itself, so the outer
2+
// literal cases take precedence over it: 1 is always matched as One, and "x"
3+
// as X inside W.
4+
@unboxed type inner = | @as("x") X | N(int) | S(string)
5+
@unboxed type outer = | @as(1) One | W(inner)
6+
7+
let f = v =>
8+
switch v {
9+
| One => "one"
10+
| W(N(1 | 2)) => "small"
11+
| W(S("x" | "y")) => "letter"
12+
| W(N(1)) => "dead"
13+
| W(_) => "other"
14+
}

0 commit comments

Comments
 (0)