From 0e98806e7f1d0bc08c172ee7adbefc248068a35f Mon Sep 17 00:00:00 2001 From: Rex Jaeschke Date: Thu, 19 Nov 2026 17:03:05 -0500 Subject: [PATCH 1/3] add support for First-class Span Types --- standard/conversions.md | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/standard/conversions.md b/standard/conversions.md index 11a03726e..7503ebf49 100644 --- a/standard/conversions.md +++ b/standard/conversions.md @@ -64,6 +64,7 @@ The following conversions are classified as implicit conversions: - Default literal conversions ([§10.2.16](conversions.md#10216-default-literal-conversions)) - Implicit throw conversions ([§10.2.17](conversions.md#10217-implicit-throw-conversions)) - Anonymous function type conversions ([§10.2.21](conversions.md#10221-anonymous-function-type-conversion)) +- Implicit scan conversions (§implicit-scan-conversions) Implicit conversions can occur in a variety of situations, including function member invocations ([§12.6.6](expressions.md#1266-function-member-invocation)), cast expressions ([§12.9.8](expressions.md#1298-cast-expressions)), and assignments ([§12.24](expressions.md#1224-assignment-operators)). @@ -537,6 +538,17 @@ The implicit inline array ([§16.6](structs.md#166-inline-arrays)) conversions a The conversion of an inline array to a `System.Span` or `System.ReadonlySpan` ignores any declared operators in the inline array type that might otherwise appear to be applicable. See [§16.6](structs.md#166-inline-arrays) for more information. +### §implicit-scan-conversions Implicit scan conversions + +An implicit span conversion permits expressions of type *array_type*, `System.Span`, `System.ReadOnlySpan`, and `string` to be converted between each other, as follows: +- From any single-dimensional *array_type* with element type `Eᵢ` to `System.Span` +- From any single-dimensional *array_type* with element type `Eᵢ` to `System.ReadOnlySpan`, provided that `Eᵢ` is covariance-convertible ([§19.2.3.3](interfaces.md#19233-variance-conversion)) to `Ui` +- From `System.Span` to `System.ReadOnlySpan`, provided that `Tᵢ` is covariance-convertible to `Uᵢ` +- From `System.ReadOnlySpan` to `System.ReadOnlySpan`, provided that `Tᵢ` is covariance-convertible to `Uᵢ` +- From `string` to `System.ReadOnlySpan` + +Any Span/ReadOnlySpan type is considered applicable for the conversion if it is a ref struct and it matches by its fully-qualified name. + ## 10.3 Explicit conversions ### 10.3.1 General @@ -763,6 +775,12 @@ The above rules do not permit a direct explicit conversion from an unconstrained A user-defined explicit conversion consists of an optional standard explicit conversion, followed by execution of a user-defined implicit or explicit conversion operator, followed by another optional standard explicit conversion. The exact rules for evaluating user-defined explicit conversions are described in [§10.5.5](conversions.md#1055-user-defined-explicit-conversions). +### §explicit-scan-conversions Explicit span conversions + +The explicit span conversions are, as follows: +- All implicit span conversions (§implicit-scan-conversions). +- From an *array_type* with element type `Tᵢ` to `System.Span` or `System.ReadOnlySpan` provided an explicit reference conversion exists from `Tᵢ` to `Uᵢ`. + ## 10.4 Standard conversions ### 10.4.1 General @@ -782,6 +800,7 @@ The following implicit conversions are classified as standard implicit conversio - Implicit constant expression conversions ([§10.2.11](conversions.md#10211-implicit-constant-expression-conversions)) - Implicit conversions involving type parameters ([§10.2.12](conversions.md#10212-implicit-conversions-involving-type-parameters)) - Implicit inline array conversions ([§10.2.23](conversions.md#10223-implicit-inline-array-conversions)) +- Implicit scan conversions (§implicit-scan-conversions) The standard implicit conversions specifically exclude user-defined implicit conversions. @@ -791,12 +810,16 @@ The standard explicit conversions are all standard implicit conversions plus the > *Note*: In other words, if a standard implicit conversion exists from a type `A` to a type `B`, then a standard explicit conversion exists from type `A` to type `B` and from type `B` to type `A`. *end note* +However, unlike other standard explicit conversions, which always exist given the opposite standard implicit conversion, there is no standard explicit span conversion. + ## 10.5 User-defined conversions ### 10.5.1 General C# allows the pre-defined implicit and explicit conversions to be augmented by user-defined conversions. User-defined conversions are introduced by declaring conversion operators ([§15.10.4](classes.md#15104-conversion-operators)) in class and struct types. +User-defined conversions are not considered when converting between types for which an implicit or an explicit span conversion exists. + ### 10.5.2 Permitted user-defined conversions C# permits only certain user-defined conversions to be declared. In particular, it is not possible to redefine an already existing implicit or explicit conversion. From e46b81adf45ba950586d40b5e91e3e187b7ff720 Mon Sep 17 00:00:00 2001 From: Rex Jaeschke Date: Thu, 19 Nov 2026 17:16:33 -0500 Subject: [PATCH 2/3] add support for First-class Span Types --- standard/expressions.md | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/standard/expressions.md b/standard/expressions.md index c6a504763..ae356cac1 100644 --- a/standard/expressions.md +++ b/standard/expressions.md @@ -861,6 +861,8 @@ An *exact inference* *from* a type `U` *to* a type `V` is made as follows: - If `V` is one of the *unfixed* `Xᵢ` then `U` is added to the set of exact bounds for `Xᵢ`. - Otherwise, sets `V₁...Vₑ` and `U₁...Uₑ` are determined by checking if any of the following cases apply: - `V` is an array type `V₁[...]` and `U` is an array type `U₁[...]` of the same rank + - `V` is a `Span` and `U` is an array type `U₁[]` or a `Span` + - `V` is a `ReadOnlySpan` and `U` is an array type `U₁[]` or a `Span` or `ReadOnlySpan` - `V` is the type `V₁?` and `U` is the type `U₁` - `V` is a constructed type `C` and `U` is a constructed type `C` @@ -876,6 +878,8 @@ A *lower-bound inference from* a type `U` *to* a type `V` is made as follows: - Otherwise, if `V` is the type `V₁?` and `U` is the type `U₁?` then a lower bound inference is made from `U₁` to `V₁`. - Otherwise, sets `U₁...Uₑ` and `V₁...Vₑ` are determined by checking if any of the following cases apply: - `V` is an array type `V₁[...]`and `U` is an array type `U₁[...]`of the same rank + - `V` is a `Span` and `U` is an array type `U₁[]` or a `Span` + - `V` is a `ReadOnlySpan` and `U` is an array type `U₁[]` or a `Span` or `ReadOnlySpan` - `V` is one of `IEnumerable`, `ICollection`, `IReadOnlyList>`, `IReadOnlyCollection` or `IList` and `U` is a single-dimensional array type `U₁[]` - `V` is a constructed `class`, `struct`, `interface` or `delegate` type `C` and there is a unique type `C` such that `U` (or, if `U` is a type `parameter`, its effective base class or any member of its effective interface set) is identical to, `inherits` from (directly or indirectly), or implements (directly or indirectly) `C`. - `V` is a function pointer type ([§24.3.3](unsafe-code.md#2433-function-pointers)) `delegate*` and there is a function pointer type `delegate*` such that `U` is identical to `delegate*`, and the calling convention of `V` is identical to `U`, and the refness of `Vᵢ` is identical to `Uᵢ`. @@ -884,7 +888,13 @@ A *lower-bound inference from* a type `U` *to* a type `V` is made as follows: If any of these cases apply then an inference is made from each `Uᵢ` to the corresponding `Vᵢ` as follows: - If `Uᵢ` is not known to be a reference type then an *exact inference* is made; or alternatively, if `U` is not a function pointer type and `Uᵢ` is not known to be a reference type, or if `U` is a function pointer type and `Uᵢ` is not known to be a function pointer type or a reference type, then an exact inference is made > *Note*: This is only applicable in unsafe code. *end note* - - Otherwise, if `U` is an array type then a *lower-bound inference* is made + - Otherwise, if `U` is an array type then inference depends on the type of `V`: + - If `V` is a `Span`, then an *exact inference* is made + - If `V` is an array type or a `ReadOnlySpan`, then a *lower-bound inference* is made + - Otherwise, if `U` is a `Span` then inference depends on the type of `V`: + - If `V` is a `Span`, then an *exact inference* is made + - If `V` is a `ReadOnlySpan`, then a *lower-bound inference* is made + - Otherwise, if `U` is a `ReadOnlySpan` and `V` is a `ReadOnlySpan` a *lower-bound inference* is made - Otherwise, if `V` is `C` then inference depends on the `i-th` type parameter of `C`: - If it is covariant then a *lower-bound inference* is made. - If it is contravariant then an *upper-bound inference* is made. @@ -1260,8 +1270,9 @@ Given an implicit conversion `C₁` that converts from an expression `E` to a ty - `C₁` is not an *anonymous function type conversion* ([§10.2.21](conversions.md#10221-anonymous-function-type-conversion)) and `C₂` is an *anonymous function type conversion*, or - `E` does not exactly match `T₂` and at least one of the following holds: - `E` exactly matches `T₁` and `E` does not exactly match `T₂` ([§12.6.4.6](expressions.md#12646-exactly-matching-expression)). + - `E` exactly matches neither of `T₁` and `T₂`, and `C₁` is an implicit span conversion and `C₂` is not an implicit span conversion - `C₁` is not a conditional expression conversion and `C₂` is a conditional expression conversion. - - `E` exactly matches both or neither of `T₁` and `T₂`, and `T₁` is a better conversion target than `T₂` ([§12.6.4.7](expressions.md#12647-better-conversion-target)) and either `C₁` and `C₂` are both conditional expression conversions or neither is a conditional expression conversion. + - `E` exactly matches both or neither of `T₁` and `T₂`, both or neither of `C₁` and `C₂` are an implicit span conversion, and `T₁` is a better conversion target than `T₂` ([§12.6.4.7](expressions.md#12647-better-conversion-target)) and either `C₁` and `C₂` are both conditional expression conversions or neither is a conditional expression conversion. - `V` is a function pointer type `delegate*` and `U` is a function pointer type `delegate*`, and the calling convention of `V` is identical to `U`, and the refness of `Vᵢ` is identical to `Uᵢ`. > *Note*: This is only applicable in unsafe code. *end note* - `E` is a method group ([§12.2](expressions.md#122-expression-classifications)), `T₁` is compatible ([§21.4](delegates.md#214-delegate-compatibility)) with the single best method from the method group for conversion `C₁`, and `T₂` is not compatible with the single best method from the method group for conversion `C₂`. @@ -1339,7 +1350,9 @@ Given an expression `E` and a type `T`, `E` ***exactly matches*** `T` if one of Given two types `T₁` and `T₂`, `T₁` is a ***better conversion target*** than `T₂` if one of the following holds: -- An implicit conversion from `T₁` to `T₂` exists and no implicit conversion from `T₂` to `T₁` exists +- `T₁` is `System.ReadOnlySpan`, `T₂` is `System.Span`, and an identity conversion from `E₁` to `E₂` exists +- `T₁` is `System.ReadOnlySpan`, `T₂` is `System.ReadOnlySpan`, and an implicit conversion from `T₁` to `T₂` exists and no implicit conversion from `T₂` to `T₁` exists +- At least one of `T₁` or `T₂` is not `System.ReadOnlySpan` and is not `System.Span`, and an implicit conversion from `T₁` to `T₂` exists and no implicit conversion from `T₂` to `T₁` exists - `T₁` is `«TaskType»`([§15.14.1](classes.md#15141-general)), `T₂` is `«TaskType»`, and `S₁` is a better conversion target than `S₂` - `T₁` is `«TaskType»`([§15.14.1](classes.md#15141-general)), `T₂` is `«TaskType»`, and `T₁` is more specialized than `T₂` - `T₁` is `S₁` or `S₁?` where `S₁` is a signed integral type, and `T₂` is `S₂` or `S₂?` where `S₂` is an unsigned integral type. Specifically: @@ -2417,7 +2430,7 @@ An extension method `Cᵢ.Mₑ` is ***eligible*** if: - `Cᵢ` is a non-generic, non-nested class - The name of `Mₑ` is *identifier* - `Mₑ` is accessible and applicable when applied to the arguments as a static method as shown above -- An implicit identity, reference or boxing conversion exists from *expr* to the type of the first parameter of `Mₑ`. +- An implicit identity, reference, boxing, or span conversion exists from *expr* to the type of the first parameter of `Mₑ`. Span conversion is not considered when overload resolution is performed for a method group conversion. The search for `C` proceeds as follows: From bb3c979385bc8fecbebd20d602f89805401f1c34 Mon Sep 17 00:00:00 2001 From: Rex Jaeschke Date: Thu, 19 Nov 2026 17:27:23 -0500 Subject: [PATCH 3/3] fix md formatting --- standard/conversions.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/standard/conversions.md b/standard/conversions.md index 7503ebf49..c573b7b2b 100644 --- a/standard/conversions.md +++ b/standard/conversions.md @@ -541,6 +541,7 @@ The conversion of an inline array to a `System.Span` or `System.ReadonlySpan` ig ### §implicit-scan-conversions Implicit scan conversions An implicit span conversion permits expressions of type *array_type*, `System.Span`, `System.ReadOnlySpan`, and `string` to be converted between each other, as follows: + - From any single-dimensional *array_type* with element type `Eᵢ` to `System.Span` - From any single-dimensional *array_type* with element type `Eᵢ` to `System.ReadOnlySpan`, provided that `Eᵢ` is covariance-convertible ([§19.2.3.3](interfaces.md#19233-variance-conversion)) to `Ui` - From `System.Span` to `System.ReadOnlySpan`, provided that `Tᵢ` is covariance-convertible to `Uᵢ` @@ -778,6 +779,7 @@ A user-defined explicit conversion consists of an optional standard explicit con ### §explicit-scan-conversions Explicit span conversions The explicit span conversions are, as follows: + - All implicit span conversions (§implicit-scan-conversions). - From an *array_type* with element type `Tᵢ` to `System.Span` or `System.ReadOnlySpan` provided an explicit reference conversion exists from `Tᵢ` to `Uᵢ`.