Skip to content

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

Draft
BillWagner wants to merge 13 commits intodraft-v9from
v9-pattern-enhancements
Draft

[Version 9.0] Feature support for pattern-matching enhancements#1460
BillWagner wants to merge 13 commits intodraft-v9from
v9-pattern-enhancements

Conversation

@BillWagner
Copy link
Member

@BillWagner BillWagner commented Nov 10, 2025

@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
Copy link
Member Author

BillWagner commented Feb 19, 2026

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.

RexJaeschke and others added 13 commits February 27, 2026 12:58
Add support for pattern additions

fix md and example

update headings

fix link warnings
Address the comments on specific lines in #1026.
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.
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.
- 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).
This was in the incorrect namespace
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."
@BillWagner BillWagner force-pushed the v9-pattern-enhancements branch from 793032d to 63eff42 Compare February 27, 2026 20:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Review: pending Proposal is available for review type: feature This issue describes a new feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants