From b50ca0c24f69c47e83c7c9f6a606363c6be0849b Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Wed, 13 May 2026 15:58:57 -0400 Subject: [PATCH 1/4] Minimal updates This first commit makes minimal adjustments to the subsumption and exhaustiveness sections. This keeps the rules fairly high level, and provides more leeway for an implementation to devise any algorithm for managing recursive patterns. --- standard/patterns.md | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/standard/patterns.md b/standard/patterns.md index c21159c64..f317849fb 100644 --- a/standard/patterns.md +++ b/standard/patterns.md @@ -456,9 +456,11 @@ A pattern `P` *would match* a constant `K` if the specification for that pattern A set of patterns `Q` *subsumes* a pattern `P` if any of the following conditions hold: - `P` is a constant pattern and any of the patterns in the set `Q` would match `P`’s *converted value* -- `P` is a var pattern and the set of patterns `Q` is *exhaustive* ([§11.4](patterns.md#114-pattern-exhaustiveness)) for the type of the pattern input value ([§11.1](patterns.md#111-general)), and either the pattern input value is not of a nullable type or some pattern in `Q` would match `null`. +- `P` is a var pattern, the set of patterns `Q` is *exhaustive* ([§11.4](patterns.md#114-pattern-exhaustiveness)) for the type of the pattern input value ([§11.1](patterns.md#111-general)), and, if the pattern input value is of a nullable type, some pattern in `Q` would match `null`. - `P` is a declaration pattern with type `T` and the set of patterns `Q` is *exhaustive* for the type `T` ([§11.4](patterns.md#114-pattern-exhaustiveness)). +Subsumption involving a *positional_pattern* or a *property_pattern* is not otherwise defined by this specification; an implementation is permitted, but not required, to detect additional cases of subsumption involving such patterns. + > *Example*: In the following switch expression, no arm is subsumed even though arms 1, 2, and 3 share the same pattern: > > @@ -484,9 +486,11 @@ The following rules define when a set of patterns is *exhaustive* for a type: A set of patterns `Q` is *exhaustive* for a type `T` if any of the following conditions hold: 1. `T` is an integral or enum type, or a nullable version of one of those, and for every possible value of `T`’s non-nullable underlying type, some pattern in `Q` would match that value; or -2. Some pattern in `Q` is a *var pattern*; or +2. Some pattern in `Q` is a *var_pattern* or a *discard_pattern*; or 3. Some pattern in `Q` is a *declaration pattern* for type `D`, and there is an identity conversion, an implicit reference conversion, or a boxing conversion from `T` to `D`. +A set of patterns containing only *constant_pattern*s, *positional_pattern*s, or *property_pattern*s (in any combination), other than as covered by rule 1 above, is not considered exhaustive by this specification. An implementation is permitted, but not required, to recognize additional sets of patterns as exhaustive. + > *Example*: > > From 4858e6598c5ee4deac3c2d33f69e186027e0e942 Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Wed, 13 May 2026 16:08:55 -0400 Subject: [PATCH 2/4] More extensive rules Provide more extensive rule definitions for recursive patterns on subsumption and exhaustiveness. This version leans more closely to the implementation, but also doesn't dive deep enough to provide more clarity on pattern forms where exhaustiveness or subsumption might not be detected. I'm tempted to use the first commit, but I want people to see both. --- standard/patterns.md | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/standard/patterns.md b/standard/patterns.md index f317849fb..e0d618564 100644 --- a/standard/patterns.md +++ b/standard/patterns.md @@ -458,8 +458,9 @@ A set of patterns `Q` *subsumes* a pattern `P` if any of the following condition - `P` is a constant pattern and any of the patterns in the set `Q` would match `P`’s *converted value* - `P` is a var pattern, the set of patterns `Q` is *exhaustive* ([§11.4](patterns.md#114-pattern-exhaustiveness)) for the type of the pattern input value ([§11.1](patterns.md#111-general)), and, if the pattern input value is of a nullable type, some pattern in `Q` would match `null`. - `P` is a declaration pattern with type `T` and the set of patterns `Q` is *exhaustive* for the type `T` ([§11.4](patterns.md#114-pattern-exhaustiveness)). +- `P` is a *positional_pattern* or a *property_pattern* and every value that `P` would match would also be matched by some pattern in `Q`. -Subsumption involving a *positional_pattern* or a *property_pattern* is not otherwise defined by this specification; an implementation is permitted, but not required, to detect additional cases of subsumption involving such patterns. +> *Note*: The last rule applies recursively through *sub-patterns*; for example, the set `{ (var x, var y) }` subsumes the pattern `(0, 0)` because every pair of values matched by the latter is also matched by the former. The precise extent to which an implementation detects subsumption involving *positional_pattern*s and *property_pattern*s is not specified beyond what these rules require. *end note* > *Example*: In the following switch expression, no arm is subsumed even though arms 1, 2, and 3 share the same pattern: > @@ -487,10 +488,13 @@ A set of patterns `Q` is *exhaustive* for a type `T` if any of the following con 1. `T` is an integral or enum type, or a nullable version of one of those, and for every possible value of `T`’s non-nullable underlying type, some pattern in `Q` would match that value; or 2. Some pattern in `Q` is a *var_pattern* or a *discard_pattern*; or -3. Some pattern in `Q` is a *declaration pattern* for type `D`, and there is an identity conversion, an implicit reference conversion, or a boxing conversion from `T` to `D`. +3. Some pattern in `Q` is a *declaration pattern* for type `D`, and there is an identity conversion, an implicit reference conversion, or a boxing conversion from `T` to `D`; or +4. `Q` contains a set of *positional_pattern*s or *property_pattern*s such that every non-`null` value of `T` is matched by at least one pattern in that set. -A set of patterns containing only *constant_pattern*s, *positional_pattern*s, or *property_pattern*s (in any combination), other than as covered by rule 1 above, is not considered exhaustive by this specification. An implementation is permitted, but not required, to recognize additional sets of patterns as exhaustive. +> *Note*: Rule 4 applies recursively through *sub-patterns*. The precise extent to which an implementation detects exhaustiveness arising from combinations of *positional_pattern*s, *property_pattern*s, and *constant_pattern*s is not specified beyond what these rules require. *end note* + + > *Example*: > > From ac34c143a2fbb365f1818c49d20ece1c074a001e Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Thu, 11 Jun 2026 14:56:26 -0400 Subject: [PATCH 3/4] Revert "More extensive rules" This reverts commit 4858e6598c5ee4deac3c2d33f69e186027e0e942. --- standard/patterns.md | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/standard/patterns.md b/standard/patterns.md index e0d618564..f317849fb 100644 --- a/standard/patterns.md +++ b/standard/patterns.md @@ -458,9 +458,8 @@ A set of patterns `Q` *subsumes* a pattern `P` if any of the following condition - `P` is a constant pattern and any of the patterns in the set `Q` would match `P`’s *converted value* - `P` is a var pattern, the set of patterns `Q` is *exhaustive* ([§11.4](patterns.md#114-pattern-exhaustiveness)) for the type of the pattern input value ([§11.1](patterns.md#111-general)), and, if the pattern input value is of a nullable type, some pattern in `Q` would match `null`. - `P` is a declaration pattern with type `T` and the set of patterns `Q` is *exhaustive* for the type `T` ([§11.4](patterns.md#114-pattern-exhaustiveness)). -- `P` is a *positional_pattern* or a *property_pattern* and every value that `P` would match would also be matched by some pattern in `Q`. -> *Note*: The last rule applies recursively through *sub-patterns*; for example, the set `{ (var x, var y) }` subsumes the pattern `(0, 0)` because every pair of values matched by the latter is also matched by the former. The precise extent to which an implementation detects subsumption involving *positional_pattern*s and *property_pattern*s is not specified beyond what these rules require. *end note* +Subsumption involving a *positional_pattern* or a *property_pattern* is not otherwise defined by this specification; an implementation is permitted, but not required, to detect additional cases of subsumption involving such patterns. > *Example*: In the following switch expression, no arm is subsumed even though arms 1, 2, and 3 share the same pattern: > @@ -488,13 +487,10 @@ A set of patterns `Q` is *exhaustive* for a type `T` if any of the following con 1. `T` is an integral or enum type, or a nullable version of one of those, and for every possible value of `T`’s non-nullable underlying type, some pattern in `Q` would match that value; or 2. Some pattern in `Q` is a *var_pattern* or a *discard_pattern*; or -3. Some pattern in `Q` is a *declaration pattern* for type `D`, and there is an identity conversion, an implicit reference conversion, or a boxing conversion from `T` to `D`; or -4. `Q` contains a set of *positional_pattern*s or *property_pattern*s such that every non-`null` value of `T` is matched by at least one pattern in that set. +3. Some pattern in `Q` is a *declaration pattern* for type `D`, and there is an identity conversion, an implicit reference conversion, or a boxing conversion from `T` to `D`. -> *Note*: Rule 4 applies recursively through *sub-patterns*. The precise extent to which an implementation detects exhaustiveness arising from combinations of *positional_pattern*s, *property_pattern*s, and *constant_pattern*s is not specified beyond what these rules require. *end note* - +A set of patterns containing only *constant_pattern*s, *positional_pattern*s, or *property_pattern*s (in any combination), other than as covered by rule 1 above, is not considered exhaustive by this specification. An implementation is permitted, but not required, to recognize additional sets of patterns as exhaustive. - > *Example*: > > From fef79e02f3322be9531591d50d9c055995237c7d Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Thu, 11 Jun 2026 17:25:46 -0400 Subject: [PATCH 4/4] Make exhaustiveness minimal Use a minimal definition for subsumption and exhaustiveness. --- standard/patterns.md | 59 ++------------------------------------------ 1 file changed, 2 insertions(+), 57 deletions(-) diff --git a/standard/patterns.md b/standard/patterns.md index f317849fb..6bfa3f744 100644 --- a/standard/patterns.md +++ b/standard/patterns.md @@ -448,64 +448,9 @@ If, after applying the preceding rule, the token `_` is still a *discard_pattern ## 11.3 Pattern subsumption In a switch statement ([§13.8.3](statements.md#1383-the-switch-statement)), it is an error if a case’s pattern is *subsumed* by the preceding set of *unguarded* ([§13.8.3](statements.md#1383-the-switch-statement)) cases. In a switch expression ([§12.11](expressions.md#1211-switch-expression)), it is an error if a *switch_expression_arm*’s pattern is *subsumed* by the preceding set of *unguarded* *switch_expression_arm*s’ patterns. -> *Note*: This means that any input value would have been matched by one of the previous cases or arms. *end note* -The following rules define when a set of patterns subsumes a given pattern: - -A pattern `P` *would match* a constant `K` if the specification for that pattern’s runtime behavior is that `P` matches `K`. - -A set of patterns `Q` *subsumes* a pattern `P` if any of the following conditions hold: - -- `P` is a constant pattern and any of the patterns in the set `Q` would match `P`’s *converted value* -- `P` is a var pattern, the set of patterns `Q` is *exhaustive* ([§11.4](patterns.md#114-pattern-exhaustiveness)) for the type of the pattern input value ([§11.1](patterns.md#111-general)), and, if the pattern input value is of a nullable type, some pattern in `Q` would match `null`. -- `P` is a declaration pattern with type `T` and the set of patterns `Q` is *exhaustive* for the type `T` ([§11.4](patterns.md#114-pattern-exhaustiveness)). -Subsumption involving a *positional_pattern* or a *property_pattern* is not otherwise defined by this specification; an implementation is permitted, but not required, to detect additional cases of subsumption involving such patterns. - -> *Example*: In the following switch expression, no arm is subsumed even though arms 1, 2, and 3 share the same pattern: -> -> -> ```csharp -> object x = 10; -> bool b = false; -> int y = x switch -> { -> int i when !b => 0, -> int i when b => 1, -> int i => 2, -> _ => 3 -> }; -> ``` -> -> Arms 1 and 2 have non-constant guards and so are not *unguarded*; only arm 3 is *unguarded* with pattern `int i`, which does not subsume the final `_` arm because it does not match a non-`int` value such as `null`. *end example* +> *Note*: This means that any input value would have been matched by one of the previous cases or arms. *end note* ## 11.4 Pattern exhaustiveness -Informally, a set of patterns is exhaustive for a type if, for every possible value of that type other than null, some pattern in the set is applicable. -The following rules define when a set of patterns is *exhaustive* for a type: - -A set of patterns `Q` is *exhaustive* for a type `T` if any of the following conditions hold: - -1. `T` is an integral or enum type, or a nullable version of one of those, and for every possible value of `T`’s non-nullable underlying type, some pattern in `Q` would match that value; or -2. Some pattern in `Q` is a *var_pattern* or a *discard_pattern*; or -3. Some pattern in `Q` is a *declaration pattern* for type `D`, and there is an identity conversion, an implicit reference conversion, or a boxing conversion from `T` to `D`. - -A set of patterns containing only *constant_pattern*s, *positional_pattern*s, or *property_pattern*s (in any combination), other than as covered by rule 1 above, is not considered exhaustive by this specification. An implementation is permitted, but not required, to recognize additional sets of patterns as exhaustive. - -> *Example*: -> -> -> ```csharp -> static void M(byte b) -> { -> switch (b) { -> case 0: case 1: case 2: ... // handle every specific value of byte -> break; -> // error: the pattern 'byte other' is subsumed by the (exhaustive) -> // previous cases -> case byte other: -> break; -> } -> } -> ``` -> -> *end example* +A set of patterns is exhaustive if, for every possible input value, some pattern in the set is applicable. When an implementation detects that a set of patterns is not exhaustive, it shall issue a warning.