From da946878aa3243ffca5affb9ad7458e3ebc08749 Mon Sep 17 00:00:00 2001 From: Rex Jaeschke Date: Thu, 5 Mar 2026 16:36:18 -0500 Subject: [PATCH 1/4] Support pattern match Span char on constant string 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 --- standard/patterns.md | 1 + 1 file changed, 1 insertion(+) diff --git a/standard/patterns.md b/standard/patterns.md index a84e96f2d..3a16801ff 100644 --- a/standard/patterns.md +++ b/standard/patterns.md @@ -227,6 +227,7 @@ For a constant pattern `P`, its *converted value* is Given a pattern input value *e* and a constant pattern `P` with converted value *v*, - if *e* has integral type or enum type, or a nullable form of one of those, and *v* has integral type, the pattern `P` *matches* the value *e* if result of the expression `e == v` is `true`; otherwise +- if *e* is of type `System.Span` or `System.ReadOnlySpan`, and *v* is a constant string, and *v* does not have a constant value of `null`, then the pattern is considered matching if `System.MemoryExtensions.SequenceEqual(e, System.MemoryExtensions.AsSpan(v))` returns `true`; otherwise - the pattern `P` *matches* the value *e* if `object.Equals(e, v)` returns `true`. > *Example*: The `switch` statement in the following method uses five constant patterns in its case labels. From f962f32332a9ce81f52f8cbc3d6cc682d3ca3512 Mon Sep 17 00:00:00 2001 From: Rex Jaeschke Date: Thu, 5 Mar 2026 16:42:05 -0500 Subject: [PATCH 2/4] Support pattern match Span char on constant string 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 --- standard/standard-library.md | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/standard/standard-library.md b/standard/standard-library.md index 5e491c7a2..e4808ee83 100644 --- a/standard/standard-library.md +++ b/standard/standard-library.md @@ -413,6 +413,15 @@ namespace System public abstract class FormattableString : IFormattable { } + public static class MemoryExtensions + { + public static ReadOnlySpan AsSpan (this string? text); + public static bool SequenceEqual (this Span span, ReadOnlySpan other) + where T : IEquatable; + public static bool SequenceEqual (this ReadOnlySpan span, + ReadOnlySpan other) where T : IEquatable; + } + public class OperationCanceledException : Exception { public OperationCanceledException(); @@ -1424,6 +1433,7 @@ The following library types are referenced in this specification. The full names - `global::System.IntPtr` - `global::System.InvalidCastException` - `global::System.InvalidOperationException` +- `global::System.MemoryExtensions` - `global::System.NotSupportedException` - `global::System.Nullable` - `global::System.NullReferenceException` From b9b3fc41ba5994c1691704b46144ba1d27b75c16 Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Mon, 4 May 2026 11:27:14 -0400 Subject: [PATCH 3/4] a couple missing mentions Add a couple missing mentions in rules on Span 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 --- standard/patterns.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/standard/patterns.md b/standard/patterns.md index 3a16801ff..65eb1e821 100644 --- a/standard/patterns.md +++ b/standard/patterns.md @@ -216,7 +216,7 @@ constant_pattern ; ``` -A constant pattern `P` is *applicable to* a type `T` if there is an implicit conversion from the constant expression of `P` to the type `T`. +A constant pattern `P` is *applicable to* a type `T` if there is an implicit conversion from the constant expression of `P` to the type `T`, or if `T` is `System.Span` or `System.ReadOnlySpan` and the constant expression of `P` is of type `string` and is not the `null` literal. For a constant pattern `P`, its *converted value* is @@ -227,7 +227,7 @@ For a constant pattern `P`, its *converted value* is Given a pattern input value *e* and a constant pattern `P` with converted value *v*, - if *e* has integral type or enum type, or a nullable form of one of those, and *v* has integral type, the pattern `P` *matches* the value *e* if result of the expression `e == v` is `true`; otherwise -- if *e* is of type `System.Span` or `System.ReadOnlySpan`, and *v* is a constant string, and *v* does not have a constant value of `null`, then the pattern is considered matching if `System.MemoryExtensions.SequenceEqual(e, System.MemoryExtensions.AsSpan(v))` returns `true`; otherwise +- if *e* is of type `System.Span` or `System.ReadOnlySpan`, and *v* is a constant string, and *v* does not have a constant value of `null`, the pattern `P` *matches* the value *e* if `System.MemoryExtensions.SequenceEqual(e, System.MemoryExtensions.AsSpan(v))` returns `true`; otherwise - the pattern `P` *matches* the value *e* if `object.Equals(e, v)` returns `true`. > *Example*: The `switch` statement in the following method uses five constant patterns in its case labels. From 6e71aa43ce2655d683ecc1df136a6f6db518d601 Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Tue, 17 Nov 2026 14:30:03 -0500 Subject: [PATCH 4/4] Add pattern rules for Span and string --- standard/patterns.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/standard/patterns.md b/standard/patterns.md index 65eb1e821..333f9a666 100644 --- a/standard/patterns.md +++ b/standard/patterns.md @@ -216,7 +216,10 @@ constant_pattern ; ``` -A constant pattern `P` is *applicable to* a type `T` if there is an implicit conversion from the constant expression of `P` to the type `T`, or if `T` is `System.Span` or `System.ReadOnlySpan` and the constant expression of `P` is of type `string` and is not the `null` literal. +A constant pattern `P` is *applicable to* a type `T` if the corresponding condition holds: + +- If `T` is `System.Span` or `System.ReadOnlySpan`, the constant expression of `P` is of type `string` and has a non-null constant value. +- Otherwise, there is an implicit conversion from the constant expression of `P` to the type `T`. For a constant pattern `P`, its *converted value* is @@ -690,6 +693,7 @@ In a *conjunctive_pattern*, the *input type* of the second pattern is narrowed b - Otherwise, if `P` is a declaration pattern, the *narrowed type* is the type of the declaration pattern’s type. - Otherwise, if `P` is a recursive pattern that gives an explicit type, the *narrowed type* is that type. - Otherwise, if `P` is matched via the rules for `ITuple` in a *positional_pattern* ([§11.2.5](patterns.md#1125-positional-pattern)), the *narrowed type* is the type `System.ITuple`. +- Otherwise, if `P` is a constant pattern applicable to its *input type* by the first applicability alternative in [§11.2.3](patterns.md#1123-constant-pattern), the *narrowed type* is the *input type*. - Otherwise, if `P` is a constant pattern where the constant is not the null constant and where the expression has no *constant expression conversion* to the *input type*, the *narrowed type* is the type of the constant. - Otherwise, if `P` is a relational pattern where the constant expression has no *constant expression conversion* to the *input type*, the *narrowed type* is the type of the constant. - Otherwise, if `P` is an `or` pattern, the *narrowed type* is the common type of the *narrowed type* of the subpatterns if such a common type exists. For this purpose, the common type algorithm considers only identity, boxing, and implicit reference conversions, and it considers all subpatterns of a sequence of `or` patterns (ignoring parenthesized patterns).