Skip to content

[Version 9.0] Feature support for pattern-matching enhancements - #1460

Draft
BillWagner wants to merge 30 commits into
draft-v9from
v9-pattern-enhancements
Draft

BillWagner wants to merge 30 commits into
draft-v9from
v9-pattern-enhancements

Conversation

@BillWagner

@BillWagner BillWagner commented Nov 10, 2025 •

Copy link
Copy Markdown
Member

@BillWagner
BillWagner changed the base branch from draft-v8 to draft-v9 November 10, 2025 23:03
@BillWagner
BillWagner force-pushed the v9-pattern-enhancements branch from 4404f2a to d2c1cff Compare November 10, 2025 23:13
@RexJaeschke RexJaeschke added this to the C# 9.0 milestone Nov 11, 2025
@RexJaeschke RexJaeschke added type: feature This issue describes a new feature Review: pending Proposal is available for review labels Nov 11, 2025
@RexJaeschke RexJaeschke changed the title [Version 9.0] V9 pattern enhancements [Version 9.0] Feature support for pattern-matching enhancements Nov 11, 2025
@BillWagner BillWagner self-assigned this Feb 17, 2026
@BillWagner BillWagner closed this Feb 19, 2026
@BillWagner BillWagner reopened this Feb 19, 2026
@BillWagner

BillWagner commented Feb 19, 2026 •

Copy link
Copy Markdown
Member Author

The proposal has several open issues. We need to ensure this PR matches the final decision on those questions.

I've looked through the LDM notes, and here's my initial analysis:

Are and, or, and not some kind of contextual keyword?

Yes, and, or and not are contextual keywords in a pattern. Even if a type or identifier is in scope, it's a pattern keyword. when used as a pattern. However, you can write this (confusing though it is):

public class or { }

or or = new or();

if (or is or)
{ }

Semantic type for relational operators: We have proposed that when the input type is already a comparable primitive, that is the type of the comparison. However, when the input is not a comparable primitive, we treat the relational as including an implicit type test to the type of the constant on the right-hand-side of the relational. If the programmer intends to support more than one input type, that must be done explicitly:

That is the decision. Those rules are in this PR.

Most of the other open questions in the speclet have resolutions documented. I haven't yet verified that this PR includes those decisions. Now verified, or added in the 8th commit.

The only remaining issue is the diagnostic rules for relational and logical patterns.

@BillWagner
BillWagner force-pushed the v9-pattern-enhancements branch from 793032d to 63eff42 Compare February 27, 2026 20:57
@BillWagner
BillWagner force-pushed the v9-pattern-enhancements branch from 94ee79d to b83abc0 Compare April 10, 2026 20:26
@BillWagner
BillWagner force-pushed the v9-pattern-enhancements branch from 885475d to 2aba1c7 Compare May 13, 2026 11:58
@BillWagner
BillWagner force-pushed the v9-pattern-enhancements branch from 2aba1c7 to c099d54 Compare June 24, 2026 18:53
@BillWagner

Copy link
Copy Markdown
Member Author

An earlier version of this feature is already present on alpha-v9 from a prior meeting. Edits made to this PR since then are not yet on alpha-v9; they will land at the next propagation. If you need them on alpha-v9 sooner, please open a separate PR targeting alpha-v9.
Thanks!

@BillWagner

Copy link
Copy Markdown
Member Author

I attempted to rebase this PR onto the updated draft-v9 after the latest TC49-TG2 committee meeting but encountered conflicts that need author resolution. Please rebase locally onto draft-v9 and resolve them. Thanks!

@BillWagner
BillWagner force-pushed the v9-pattern-enhancements branch from c099d54 to 7f8e7e1 Compare September 18, 2026 19:55
@BillWagner
BillWagner force-pushed the v9-pattern-enhancements branch from 7f8e7e1 to 4970c31 Compare October 14, 2026 13:56
BillWagner added a commit that referenced this pull request Oct 14, 2026
Independent Hendrix revision: discard retired sections resurrected by the #1460 rebase. Retain approved definitions in 11.1 and C# 9 pattern forms in 11.2.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot-Session: 21570b74-c6f5-4fc5-ab6d-7546918d6ddb
@jskeet

jskeet commented Nov 4, 2026

Copy link
Copy Markdown
Contributor

When we get to this, we should check #1843.

RexJaeschke and others added 8 commits November 12, 2026 12:20
Add support for pattern additions

fix md and example

update headings

fix link warnings

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 21570b74-c6f5-4fc5-ab6d-7546918d6ddb
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 361b9a77-2b4c-45e6-969f-e6785c47f27e
Address the comments on specific lines in #1026.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 21570b74-c6f5-4fc5-ab6d-7546918d6ddb
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 361b9a77-2b4c-45e6-969f-e6785c47f27e
Address the comments in the general review comment on #1026:

- I did continue to use the *logical_pattern* production. We use that term in the text, and it's a good shorthand so readers don't need to snake through the grammar of *disjunctive_pattern*, *conjunctive_pattern* and *negated_pattern*.

- I haven't addressed or created issues for the open questions in the proposal yet.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 21570b74-c6f5-4fc5-ab6d-7546918d6ddb
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 361b9a77-2b4c-45e6-969f-e6785c47f27e
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 21570b74-c6f5-4fc5-ab6d-7546918d6ddb
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 361b9a77-2b4c-45e6-969f-e6785c47f27e
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 21570b74-c6f5-4fc5-ab6d-7546918d6ddb
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 361b9a77-2b4c-45e6-969f-e6785c47f27e
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 21570b74-c6f5-4fc5-ab6d-7546918d6ddb
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 361b9a77-2b4c-45e6-969f-e6785c47f27e
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 21570b74-c6f5-4fc5-ab6d-7546918d6ddb
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 361b9a77-2b4c-45e6-969f-e6785c47f27e
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 21570b74-c6f5-4fc5-ab6d-7546918d6ddb
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 361b9a77-2b4c-45e6-969f-e6785c47f27e
BillWagner and others added 10 commits November 12, 2026 12:20
Add the subsumption and exhaustiveness rules for the new patterns in V9.

The subsumption/exhaustiveness rules in the Roslyn compiler use a BDD (binary decision diagram)-based algorithm that is more powerful than can be expressed with simple prose rules. These rules attempt to capture the *observable behavior* (what the compiler accepts/rejects) rather than the implementation algorithm. Where the compiler's algorithm is too complex to fully express (e.g., relational range analysis), the spec describes the intent and give examples.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 21570b74-c6f5-4fc5-ab6d-7546918d6ddb
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 361b9a77-2b4c-45e6-969f-e6785c47f27e
- Removed duplicate parenthesized_pattern from the primary_pattern production
- Fixed relational pattern prose
Added a sentence after the grammar: "The relational_expression in a relational_pattern is required to evaluate to a constant value."
Replaced backtick-formatted `constant_expression` references with plain prose ("the expression", "the constant expression").
Changed `null_literal` to "a null constant" (it's a semantic constraint, not a syntactic one).

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 21570b74-c6f5-4fc5-ab6d-7546918d6ddb
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 361b9a77-2b4c-45e6-969f-e6785c47f27e
This was in the incorrect namespace

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 21570b74-c6f5-4fc5-ab6d-7546918d6ddb
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 361b9a77-2b4c-45e6-969f-e6785c47f27e
Applicability rule for type pattern: "A type pattern naming a type T is applicable to every type E for which E is pattern compatible with T (§11.2.2)."

Applicability rule for relational pattern: "A relational_pattern is applicable to a type T if a suitable built-in binary relational operator is defined with both operands of type T, or if an explicit nullable or unboxing conversion exists from T to the type of the constant expression."

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 21570b74-c6f5-4fc5-ab6d-7546918d6ddb
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 361b9a77-2b4c-45e6-969f-e6785c47f27e
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 21570b74-c6f5-4fc5-ab6d-7546918d6ddb
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 361b9a77-2b4c-45e6-969f-e6785c47f27e
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 21570b74-c6f5-4fc5-ab6d-7546918d6ddb
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 361b9a77-2b4c-45e6-969f-e6785c47f27e
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 21570b74-c6f5-4fc5-ab6d-7546918d6ddb
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 361b9a77-2b4c-45e6-969f-e6785c47f27e
Update grammar test infrastructure to match new pattern rule hierarchy:
- pattern : logical_pattern (was 6 direct alternatives)
- New rules: primary_pattern, parenthesized_pattern, type_pattern,
  relational_pattern, logical_pattern, disjunctive_pattern,
  conjunctive_pattern, negated_pattern

Changes:
- GrammarTestingEnv.tgz: Updated baseline grammar and Rules modification
  set to handle new pattern hierarchy. TypePatternPredicate now walks up
  the tree through intermediate pattern nodes to find relational_expression.
- Reference tree files: 'is (type' -> 'is (pattern' since
  ReplacePatternWithType action is no longer needed (type_pattern is now
  a proper grammar rule).
- RightShift stderr: Added 'and', 'not', 'or' to expected token sets
  (new pattern operator keywords).

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 21570b74-c6f5-4fc5-ab6d-7546918d6ddb
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 361b9a77-2b4c-45e6-969f-e6785c47f27e
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 21570b74-c6f5-4fc5-ab6d-7546918d6ddb
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 361b9a77-2b4c-45e6-969f-e6785c47f27e
Independent Hendrix revision: discard retired sections resurrected by the #1460 rebase. Retain approved definitions in 11.1 and C# 9 pattern forms in 11.2.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot-Session: 21570b74-c6f5-4fc5-ab6d-7546918d6ddb
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 361b9a77-2b4c-45e6-969f-e6785c47f27e
@BillWagner
BillWagner force-pushed the v9-pattern-enhancements branch from 4970c31 to 858d6e7 Compare November 12, 2026 18:53
BillWagner added a commit that referenced this pull request Nov 12, 2026
Validated source 858d6e7. Preserve #1691 retirement and combine pattern-operator keywords with all previously integrated keywords, keeping five-column alternatives and token order.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 361b9a77-2b4c-45e6-969f-e6785c47f27e
BillWagner added a commit that referenced this pull request Nov 12, 2026
Predecessor draft: cebb6e9; old alpha: 272e754. Current validated sources integrated in historical order: #1452, #1459, #1458, #1462, #1460, #1469, #1467, #1465, #1468, #1470, #1471, #1472. Already-merged native integers, static anonymous functions and lambda discard parameters come from the authoritative draft, not stale previews. Exact StandardAnchorTags ran last and identically twice with byte idempotence. All 604 actual example tests, source/compiler audits, pinned lint and converter pass. Downstream grammar-validator debt is recorded separately under Bill's current policy; no grammar remediation or waiver is claimed.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 361b9a77-2b4c-45e6-969f-e6785c47f27e
- *standard\patterns.md*: permits leading is not declarations, including parentheses and repeated negation. Preserves restrictions on declarations under or and on nested/switch-context negations.
- *standard\variables.md*: defines definite assignment for matching and non-matching outcomes, including negation and composition.
- *standard\expressions.md*: references those authoritative rules instead of incorrectly asserting assignment only when true.

Added three focused annotated examples
- Specified that parentheses preserve the enclosed pattern’s input type, applicability, matching behavior, and narrowed type.
- Added an explicit parenthesized-pattern case before the narrowing fallback.
- Referenced F01’s definite-assignment rules without duplicating them.
- Added two annotated examples covering narrowing, matching, and invalid applicability.
- Defined how the input type and constant determine the comparison type and converted constant.
- Specified applicability, required conversions, and runtime type testing before comparison.
- Preserved nullable, boxed, enum, native-integer, and generic cases.
- Distinguished invalid constant conversions from runtime non-matches.
- Added examples showing why ordinary binary numeric promotion does not determine relational-pattern applicability.
- Relational patterns now narrow to F03’s comparison type.
- Non-null constant patterns reuse F03’s conversion steps, preserving numeric conversions, nullable unwrapping, and pre-boxing types.
- Added an example demonstrating `double` narrowing for numeric conversions and `int` narrowing for a boxed integer.
- Corrected `(int, int) x` to a positional pattern containing type subpatterns and an outer designation.
- Added recognition rules distinguishing tuple-looking, parenthesized, constant, and declaration patterns.
- Preserved named tuple types, eligible aliases, tuple arrays, nested generic tuple types, and legacy `is` behavior.
- Specified that outer captures retain the input or explicit pattern type, preserve tuple names, unwrap nullable inputs, and receive the matched value—not a reconstructed tuple.
- Added two annotated examples.
- Replaced undefined “recursive pattern” with explicit positional/property patterns specifying a type.
- Corrected `System.ITuple` to `System.Runtime.CompilerServices.ITuple`
- Specified that `not` and both operands of `or` retain the enclosing input type.
- Specified that `and` passes its left operand’s narrowed type to its right operand.
- Defined applicability in terms of each operand’s input type.
- Removed duplicate conjunction input-type wording and added two focused examples.
- Parentheses specify grouping rather than runtime evaluation order.
- `and`/`or` do not guarantee left-to-right short-circuit evaluation; tests that cannot affect the result may be skipped.
- Clarified `is` precedence relative to `&&`/`||`, preserving ordinary unary `!` precedence.
Implemented one sentence change in `standard/patterns.md`, uncommitted.

- Explicitly prohibited `dynamic`, nullable value types, and nullable reference types in type patterns.
- Preserved legacy `o is dynamic` and nullable-alias is-type expressions.
Implemented locally in `standard/patterns.md` and `standard/lexical-structure.md`, uncommitted.

- Specified when `not` is a negation operator.
- Defined lookahead distinguishing `and`/`or` operators from variable designations.
- Preserved escaped identifiers and legacy expression boundaries.
- Added one annotated example and a lexical cross-reference.
Implemented locally in `standard/patterns.md`, uncommitted.

- Allowed implementing structs and nullable inputs to use `ITuple` fallback.
- Required omission of an explicit type, property clause, named outer designation, and element-name prefixes.
- Preserved outer discards and variables declared within element subpatterns.
- Preserved tuple/applicable-`Deconstruct` priority; a wrong-arity `Deconstruct` no longer blocks fallback.
- Added two annotated examples.
Implemented locally in `standard/patterns.md`, uncommitted.

- Specified that nested and switch-expression `_` patterns are discards regardless of same-named declarations.
- Preserved legacy top-level `is _` type lookup and switch-label lookup.
- Distinguished escaped identifiers from discard tokens.
- Corrected the `WithUnderscore` example and added a type/discard comparison example.
@BillWagner BillWagner added meeting: discuss This issue should be discussed at the next TC49-TG2 meeting Review Requested: General direction This label requests a review for the broad direction only. PR is likely not ready to merge. labels Nov 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

meeting: discuss This issue should be discussed at the next TC49-TG2 meeting Review: pending Proposal is available for review Review Requested: General direction This label requests a review for the broad direction only. PR is likely not ready to merge. type: feature This issue describes a new feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants