Skip to content

Commit c8ed365

Browse files
cknittclaude
andcommitted
Report shadowed payload alternatives when only warning 12 is on
The check for untagged payload patterns shadowed by literal cases ran only when warning 11 was active, so with `-w -11+12` or `@warning("-11")` a dead or-pattern alternative got no warning 12. It now runs when either warning is active; the usefulness analysis is still gated on warning 11. Example (rescript): @unboxed type t = | @as("a") A | S(string) @warning("-11") let g = x => switch x { | S("a") => 1 | S("a" | "c") => 2 | S(_) | A => 3 } Diagnostic (text): Before: no warnings After: Warning 12 on the "a" in S("a" | "c"); the dead S("a") case stays silent because warning 11 is off. Area: typechecker tests Output: unchanged Formats: none PPX: unchanged Editor: unchanged Tests: ~tests/build_tests/super_errors/fixtures/warning_12_untagged_literal_shadows_or_pattern.res Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Kr4cjoUi2f5VpMvE6wT9bo Signed-off-by: Christoph Knittel <ck@cca.io>
1 parent 91b5007 commit c8ed365

3 files changed

Lines changed: 28 additions & 2 deletions

File tree

‎compiler/ml/parmatch.ml‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2435,7 +2435,10 @@ let check_shadowed_payloads casel =
24352435
(dead_cases, fun p -> List.mem p.pat_loc !reported)
24362436

24372437
let check_unused pred casel =
2438-
if Warnings.is_active (Warnings.Unused_match "") then
2438+
let unused_match = Warnings.is_active (Warnings.Unused_match "") in
2439+
(* Shadowed payloads are reported as warning 11 or 12, so they are checked
2440+
when either is active; the usefulness analysis below only when 11 is. *)
2441+
if unused_match || Warnings.is_active (Warnings.Unused_pat "") then
24392442
let casel = lower_cases casel in
24402443
let dead_cases, already_reported = check_shadowed_payloads casel in
24412444
let rec do_rec pref = function
@@ -2509,7 +2512,7 @@ let check_unused pred casel =
25092512
if c_guard <> None then do_rec pref rem else do_rec ([q] :: pref) rem
25102513
in
25112514

2512-
do_rec [] casel
2515+
if unused_match then do_rec [] casel
25132516

25142517
(*********************************)
25152518
(* Exported irrefutability tests *)

‎tests/build_tests/super_errors/expected/warning_12_untagged_literal_shadows_or_pattern.res.expected‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,18 @@
88
7 │ | S(_) | A => 3
99
8 │ }
1010

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.
13+
14+
15+
Warning number 12
16+
/.../fixtures/warning_12_untagged_literal_shadows_or_pattern.res:16:7-9
17+
18+
14 ┆ switch x {
19+
15 ┆ | S("a") => 1
20+
16 ┆ | S("a" | "c") => 2
21+
17 ┆ | S(_) | A => 3
22+
18 ┆ }
23+
1124
this sub-pattern is unused.
1225
"a" is the runtime value of constructor A, so it is always matched as A, never as S.

‎tests/build_tests/super_errors/fixtures/warning_12_untagged_literal_shadows_or_pattern.res‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,13 @@ let f = x =>
66
| S("a" | "b") => 2
77
| S(_) | A => 3
88
}
9+
10+
// With warning 11 off, the dead alternative still gets warning 12, while the
11+
// dead case gets nothing.
12+
@warning("-11")
13+
let g = x =>
14+
switch x {
15+
| S("a") => 1
16+
| S("a" | "c") => 2
17+
| S(_) | A => 3
18+
}

0 commit comments

Comments
 (0)