Redundant conjunctive patterns (&
, as
) may trigger spurious exhaustiveness warning
#18118
Labels
Area-Compiler-PatternMatching
pattern compilation, active patterns, performance, codegen
Feature Improvement
Milestone
Redundant conjunctive patterns (
&
,as
) may trigger a spurious exhaustiveness warning in some scenarios.Repro steps
Minimal repro
A normal exhaustive pattern match does not trigger an exhaustiveness warning, as expected:
However, given this example, adding a redundant match on case
C
with a conjunctive pattern like&
oras
will trigger a spurious exhaustiveness warning:More realistic/useful repro
Another, perhaps more realistic and useful example is this, where you might want to bind the first two elements of a list as well as the tail of the original list (i.e., including the second element but not the first):
It is impossible to make the compiler happy in that scenario — we cannot turn
e1 :: e2 :: _ & _ :: tail
into(e1 :: e2 :: _ & _ :: tail | [])
, because we cannot binde1
,e2
, ortail
in the[]
case which the compiler is (unnecessarily) asking us to add.Note that no warning is emitted if one side of the conjunctive pattern is a simple named pattern, e.g.,
Codegen
This even affects codegen — an unreachable
MatchFailureException
branch is emitted:SharpLab
Expected behavior
No exhaustiveness warning should be emitted, and the codegen should be identical.
Actual behavior
An exhaustiveness warning and an unreachable
MatchFailureException
branch are emitted.Known workarounds
N/A.
Related information
This behavior appears to be old — it reproduces for me in .NET 3.1, and it is still present in the latest .NET 9 SDK.
The text was updated successfully, but these errors were encountered: