Repository navigation
[Version 9.0] Feature support for pattern-matching enhancements - #1460
BillWagner wants to merge 30 commits into
Conversation
4404f2a to
d2c1cff
Compare
|
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:
Yes, public class or { }
or or = new or();
if (or is or)
{ }
That is the decision. Those rules are in this PR. Most of the other open questions in the speclet have resolutions documented. The only remaining issue is the diagnostic rules for relational and logical patterns. |
793032d to
63eff42
Compare
94ee79d to
b83abc0
Compare
885475d to
2aba1c7
Compare
2aba1c7 to
c099d54
Compare
|
An earlier version of this feature is already present on |
|
I attempted to rebase this PR onto the updated |
c099d54 to
7f8e7e1
Compare
7f8e7e1 to
4970c31
Compare
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
|
When we get to this, we should check #1843. |
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
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
4970c31 to
858d6e7
Compare
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
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.
This includes all commits from #1026
There are a number of conversations that haven't been addressed from #1026:
As of the edits on Nov 26, 2026, this PR:
Fixes #1843