-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Changes from 3 commits
119ecff
44c9c58
1817f9c
b686890
a991afd
8fe31a1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
|
|
||
| public interface CovariantInterface<out T> { } | ||
|
|
||
| public interface InvariantInterface<T> { } | ||
|
|
||
| public interface Interface<out T1, T2> { } | ||
|
|
||
| public class Base { } | ||
|
|
||
| public class Derived : Base { } | ||
|
|
||
| public class C | ||
| { | ||
| public void M() | ||
| { | ||
| string[] stringArray = []; | ||
| string[][] stringArrayArray; | ||
| string[,] stringArray2D; | ||
|
|
||
| Span<string> stringSpan = stringArray; // string[] -> Span<string>; | ||
|
|
||
| // 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? |
||
| // Assignments are included to illustrate that this compiles. | ||
| // Only the use of the types matter in terms of test output. | ||
| 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>; | ||
|
|
||
| // Convert string to ReadOnlySpan<char> | ||
| string s = ""; | ||
| ReadOnlySpan<char> charReadOnlySpan = s; // string -> ReadOnlySpan<char> | ||
|
|
||
| // Use the non-covariant interfaces to show that no conversion is possible. | ||
| ReadOnlySpan<InvariantInterface<Base>> invariantInterfaceBaseReadOnlySpan; | ||
| ReadOnlySpan<InvariantInterface<Derived>> invariantInterfaceDerivedReadOnlySpan; | ||
| Span<InvariantInterface<Derived>> invariantInterfaceDerivedSpan; | ||
| InvariantInterface<Derived>[] invariantInterfaceDerivedArray; | ||
| ReadOnlySpan<Interface<Base, string>> interfaceBaseReadOnlySpan; | ||
| ReadOnlySpan<Interface<Derived, string>> interfaceDerivedReadOnlySpan; | ||
| Span<Interface<Derived, string>> interfaceDerivedSpan; | ||
| Interface<Derived, string>[] interfaceDerivedArray; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| | CovariantInterface<Derived>[] | ReadOnlySpan<CovariantInterface<Base>> | | ||
| | CovariantInterface<Derived>[] | ReadOnlySpan<CovariantInterface<Derived>> | | ||
| | CovariantInterface<Derived>[] | Span<CovariantInterface<Derived>> | | ||
| | Interface<Derived,String>[] | ReadOnlySpan<Interface<Derived, string>> | | ||
| | Interface<Derived,String>[] | Span<Interface<Derived, string>> | | ||
| | InvariantInterface<Derived>[] | ReadOnlySpan<InvariantInterface<Derived>> | | ||
| | InvariantInterface<Derived>[] | Span<InvariantInterface<Derived>> | | ||
| | ReadOnlySpan<CovariantInterface<Derived>> | ReadOnlySpan<CovariantInterface<Base>> | | ||
| | Span<CovariantInterface<Derived>> | ReadOnlySpan<CovariantInterface<Base>> | | ||
| | Span<CovariantInterface<Derived>> | ReadOnlySpan<CovariantInterface<Derived>> | | ||
| | Span<Interface<Derived, string>> | ReadOnlySpan<Interface<Derived, string>> | | ||
|
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 would have expected something like here?
Contributor
Author
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. Uh, that is a very nice catch! The documentation on variance conversion is a bit sketchy and I mistakenly assumed that all type parameters needed to be declared
Contributor
Author
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. It appears that the implicit conversions are allowed for all variance convertible types (and not only covariant convertible types as hinted in the documentation of the feature) |
||
| | Span<InvariantInterface<Derived>> | ReadOnlySpan<InvariantInterface<Derived>> | | ||
| | Span<string> | ReadOnlySpan<string> | | ||
| | String[] | ReadOnlySpan<string> | | ||
| | String[] | Span<string> | | ||
| | string | ReadOnlySpan<char> | | ||
| 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 |
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.