-
Notifications
You must be signed in to change notification settings - Fork 1.9k
C# 14: Implicit span conversions. #21065
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
michaelnebel
merged 6 commits into
github:main
from
michaelnebel:csharp/implicitspanconversions
Jan 7, 2026
+190
−0
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
119ecff
C#: Implicit span conversion.
michaelnebel 44c9c58
C#: Add implicit span conversion test case.
michaelnebel 1817f9c
C#: Add change-note.
michaelnebel b686890
C#: Address review comments.
michaelnebel a991afd
C#: Use ref conversions (including variance conversions) for element …
michaelnebel 8fe31a1
C#: Add some more testcases and update test expected output.
michaelnebel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
4 changes: 4 additions & 0 deletions
4
csharp/ql/lib/change-notes/2025-12-18-implicit-span-conversions.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| --- | ||
| category: minorAnalysis | ||
| --- | ||
| * C# 14: Support for *implicit* span conversions in the QL library. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
|
|
||
| public interface CovariantInterface<out T> { } | ||
|
|
||
| public interface ContravariantInterface<in T> { } | ||
|
|
||
| public interface InvariantInterface<T> { } | ||
|
|
||
| public interface MixedInterface<out T1, in T2> { } | ||
|
|
||
| public class Base { } | ||
|
|
||
| public class Derived : Base { } | ||
|
|
||
| public class C | ||
| { | ||
| public void M() | ||
| { | ||
| string[] stringArray = []; | ||
| string[][] stringArrayArray = []; | ||
| string[,] stringArray2D = new string[0, 0]; | ||
|
|
||
| Span<string> stringSpan = stringArray; // string[] -> Span<string>; | ||
|
|
||
| // Assignments are included to illustrate that it compiles. | ||
| // Only the use of the types matter in terms of test output. | ||
| // Covariant conversions to ReadOnlySpan | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we need to add a test case for something like ReadOnlySpan<Base> x = // something of type `Derived[]`;
ReadOnlySpan<Base> x = // something of type `Span<Derived>`;which I'm not sure the current logic is able to handle? |
||
| ReadOnlySpan<CovariantInterface<Base>> covariantInterfaceBaseReadOnlySpan; | ||
| ReadOnlySpan<CovariantInterface<Derived>> covariantInterfaceDerivedReadOnlySpan = default; | ||
| Span<CovariantInterface<Derived>> covariantInterfaceDerivedSpan = default; | ||
| CovariantInterface<Derived>[] covariantInterfaceDerivedArray = []; | ||
| covariantInterfaceBaseReadOnlySpan = covariantInterfaceDerivedReadOnlySpan; // ReadOnlySpan<CovariantInterface<Derived>> -> ReadOnlySpan<CovariantInterface<Base>> | ||
| covariantInterfaceBaseReadOnlySpan = covariantInterfaceDerivedSpan; // Span<CovariantInterface<Derived>> -> ReadOnlySpan<CovariantInterface<Base>> | ||
| covariantInterfaceBaseReadOnlySpan = covariantInterfaceDerivedArray; // CovariantInterface<Derived>[] -> ReadOnlySpan<CovariantInterface<Base>> | ||
|
|
||
| // Identify conversions to ReadOnlySpan | ||
| ReadOnlySpan<string> stringReadOnlySpan; | ||
| stringReadOnlySpan = stringSpan; // Span<string> -> ReadOnlySpan<string>; | ||
| stringReadOnlySpan = stringArray; // string[] -> ReadOnlySpan<string>; | ||
|
|
||
| // Contravariant conversions to ReadOnlySpan | ||
| ReadOnlySpan<ContravariantInterface<Derived>> contravariantInterfaceDerivedReadOnlySpan; | ||
| ReadOnlySpan<ContravariantInterface<Base>> contravariantInterfaceBaseReadOnlySpan = default; | ||
| Span<ContravariantInterface<Base>> contravariantInterfaceBaseSpan = default; | ||
| ContravariantInterface<Base>[] contravariantInterfaceBaseArray = []; | ||
| contravariantInterfaceDerivedReadOnlySpan = contravariantInterfaceBaseReadOnlySpan; // ReadOnlySpan<ContravariantInterface<Base>> -> ReadOnlySpan<ContravariantInterface<Derived>> | ||
| contravariantInterfaceDerivedReadOnlySpan = contravariantInterfaceBaseSpan; // Span<ContravariantInterface<Base>> -> ReadOnlySpan<ContravariantInterface<Derived>> | ||
| contravariantInterfaceDerivedReadOnlySpan = contravariantInterfaceBaseArray; // ContravariantInterface<Base>[] -> ReadOnlySpan<ContravariantInterface<Derived>> | ||
|
|
||
| // Mixed variance conversions to ReadOnlySpan | ||
| ReadOnlySpan<MixedInterface<Base, Derived>> mixedInterfaceBaseReadOnlySpan; | ||
| ReadOnlySpan<MixedInterface<Derived, Base>> mixedInterfaceDerivedReadOnlySpan = default; | ||
| Span<MixedInterface<Derived, Base>> mixedInterfaceDerivedSpan = default; | ||
| MixedInterface<Derived, Base>[] mixedInterfaceDerivedArray = []; | ||
| mixedInterfaceBaseReadOnlySpan = mixedInterfaceDerivedReadOnlySpan; // ReadOnlySpan<MixedInterface<Derived, Base>> -> ReadOnlySpan<MixedInterface<Base, Derived>> | ||
| mixedInterfaceBaseReadOnlySpan = mixedInterfaceDerivedSpan; // Span<MixedInterface<Derived, Base>> -> ReadOnlySpan<MixedInterface<Base, Derived>> | ||
| mixedInterfaceBaseReadOnlySpan = mixedInterfaceDerivedArray; // MixedInterface<Derived, Base>[] -> ReadOnlySpan<MixedInterface<Base, Derived>> | ||
|
|
||
| // Convert string to ReadOnlySpan<char> | ||
| string s = ""; | ||
| ReadOnlySpan<char> charReadOnlySpan = s; // string -> ReadOnlySpan<char> | ||
|
|
||
| // Various ref type conversions | ||
| Derived[] derivedArray = []; | ||
| ReadOnlySpan<Base> baseReadOnlySpan; | ||
| baseReadOnlySpan = derivedArray; // Derived[] -> ReadOnlySpan<Base> | ||
|
|
||
| ReadOnlySpan<object> objectReadOnlySpan; | ||
| objectReadOnlySpan = stringArray; // string[] -> ReadOnlySpan<object> | ||
|
|
||
| byte[][] byteByteArray = []; | ||
| objectReadOnlySpan = byteByteArray; // byte[][] -> ReadOnlySpan<object> | ||
|
|
||
| // No conversion possible except for identity. | ||
| ReadOnlySpan<InvariantInterface<Base>> invariantInterfaceBaseReadOnlySpan; | ||
| ReadOnlySpan<InvariantInterface<Derived>> invariantInterfaceDerivedReadOnlySpan; | ||
| Span<InvariantInterface<Derived>> invariantInterfaceDerivedSpan; | ||
| InvariantInterface<Derived>[] invariantInterfaceDerivedArray; | ||
| } | ||
| } | ||
47 changes: 47 additions & 0 deletions
47
csharp/ql/test/library-tests/conversion/span/span.expected
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| | ContravariantInterface<Base>[] | ReadOnlySpan<ContravariantInterface<Base>> | | ||
| | ContravariantInterface<Base>[] | ReadOnlySpan<ContravariantInterface<Derived>> | | ||
| | ContravariantInterface<Base>[] | ReadOnlySpan<object> | | ||
| | ContravariantInterface<Base>[] | Span<ContravariantInterface<Base>> | | ||
| | CovariantInterface<Derived>[] | ReadOnlySpan<CovariantInterface<Base>> | | ||
| | CovariantInterface<Derived>[] | ReadOnlySpan<CovariantInterface<Derived>> | | ||
| | CovariantInterface<Derived>[] | ReadOnlySpan<object> | | ||
| | CovariantInterface<Derived>[] | Span<CovariantInterface<Derived>> | | ||
| | Derived[] | ReadOnlySpan<Base> | | ||
| | Derived[] | ReadOnlySpan<object> | | ||
| | InvariantInterface<Derived>[] | ReadOnlySpan<InvariantInterface<Derived>> | | ||
| | InvariantInterface<Derived>[] | ReadOnlySpan<object> | | ||
| | InvariantInterface<Derived>[] | Span<InvariantInterface<Derived>> | | ||
| | MixedInterface<Derived,Base>[] | ReadOnlySpan<MixedInterface<Base, Derived>> | | ||
| | MixedInterface<Derived,Base>[] | ReadOnlySpan<MixedInterface<Derived, Base>> | | ||
| | MixedInterface<Derived,Base>[] | ReadOnlySpan<object> | | ||
| | MixedInterface<Derived,Base>[] | Span<MixedInterface<Derived, Base>> | | ||
| | ReadOnlySpan<Base> | ReadOnlySpan<object> | | ||
| | ReadOnlySpan<ContravariantInterface<Base>> | ReadOnlySpan<ContravariantInterface<Derived>> | | ||
| | ReadOnlySpan<ContravariantInterface<Base>> | ReadOnlySpan<object> | | ||
| | ReadOnlySpan<ContravariantInterface<Derived>> | ReadOnlySpan<object> | | ||
| | ReadOnlySpan<CovariantInterface<Base>> | ReadOnlySpan<object> | | ||
| | ReadOnlySpan<CovariantInterface<Derived>> | ReadOnlySpan<CovariantInterface<Base>> | | ||
| | ReadOnlySpan<CovariantInterface<Derived>> | ReadOnlySpan<object> | | ||
| | ReadOnlySpan<InvariantInterface<Base>> | ReadOnlySpan<object> | | ||
| | ReadOnlySpan<InvariantInterface<Derived>> | ReadOnlySpan<object> | | ||
| | ReadOnlySpan<MixedInterface<Base, Derived>> | ReadOnlySpan<object> | | ||
| | ReadOnlySpan<MixedInterface<Derived, Base>> | ReadOnlySpan<MixedInterface<Base, Derived>> | | ||
| | ReadOnlySpan<MixedInterface<Derived, Base>> | ReadOnlySpan<object> | | ||
| | ReadOnlySpan<string> | ReadOnlySpan<object> | | ||
| | Span<ContravariantInterface<Base>> | ReadOnlySpan<ContravariantInterface<Base>> | | ||
| | Span<ContravariantInterface<Base>> | ReadOnlySpan<ContravariantInterface<Derived>> | | ||
| | Span<ContravariantInterface<Base>> | ReadOnlySpan<object> | | ||
| | Span<CovariantInterface<Derived>> | ReadOnlySpan<CovariantInterface<Base>> | | ||
| | Span<CovariantInterface<Derived>> | ReadOnlySpan<CovariantInterface<Derived>> | | ||
| | Span<CovariantInterface<Derived>> | ReadOnlySpan<object> | | ||
| | Span<InvariantInterface<Derived>> | ReadOnlySpan<InvariantInterface<Derived>> | | ||
| | Span<InvariantInterface<Derived>> | ReadOnlySpan<object> | | ||
| | Span<MixedInterface<Derived, Base>> | ReadOnlySpan<MixedInterface<Base, Derived>> | | ||
| | Span<MixedInterface<Derived, Base>> | ReadOnlySpan<MixedInterface<Derived, Base>> | | ||
| | Span<MixedInterface<Derived, Base>> | ReadOnlySpan<object> | | ||
| | Span<string> | ReadOnlySpan<object> | | ||
| | Span<string> | ReadOnlySpan<string> | | ||
| | String[] | ReadOnlySpan<object> | | ||
| | String[] | ReadOnlySpan<string> | | ||
| | String[] | Span<string> | | ||
| | string | ReadOnlySpan<char> | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| import semmle.code.csharp.Conversion | ||
|
|
||
| private class InterestingType extends Type { | ||
| InterestingType() { exists(LocalVariable lv | lv.getType() = this) } | ||
| } | ||
|
|
||
| from InterestingType sub, InterestingType sup | ||
| where convSpan(sub, sup) and sub != sup | ||
| select sub.toStringWithTypes() as s1, sup.toStringWithTypes() as s2 order by s1, s2 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
SimpleArrayTypeclass lacks documentation explaining what distinguishes it from other array types. Add a docstring clarifying that this represents single-dimensional, zero-based arrays (SZ arrays), asgetRank() = 1 and getDimension() = 1restricts it to this specific array form.