mirror of
https://github.com/github/codeql.git
synced 2026-05-02 12:15:17 +02:00
Merge pull request #21065 from michaelnebel/csharp/implicitspanconversions
C# 14: Implicit span conversions.
This commit is contained in:
@@ -0,0 +1,4 @@
|
||||
---
|
||||
category: minorAnalysis
|
||||
---
|
||||
* C# 14: Support for *implicit* span conversions in the QL library.
|
||||
@@ -28,6 +28,7 @@ private module Cached {
|
||||
*
|
||||
* - Identity conversions
|
||||
* - Implicit numeric conversions
|
||||
* - Implicit span conversions
|
||||
* - Implicit nullable conversions
|
||||
* - Implicit reference conversions
|
||||
* - Boxing conversions
|
||||
@@ -38,6 +39,8 @@ private module Cached {
|
||||
or
|
||||
convNumeric(fromType, toType)
|
||||
or
|
||||
convSpan(fromType, toType)
|
||||
or
|
||||
convNullableType(fromType, toType)
|
||||
or
|
||||
convRefTypeNonNull(fromType, toType)
|
||||
@@ -81,6 +84,7 @@ private predicate implicitConversionNonNull(Type fromType, Type toType) {
|
||||
*
|
||||
* - Identity conversions
|
||||
* - Implicit numeric conversions
|
||||
* - Implicit span conversions
|
||||
* - Implicit nullable conversions
|
||||
* - Implicit reference conversions
|
||||
* - Boxing conversions
|
||||
@@ -491,6 +495,51 @@ private predicate convNumericChar(SimpleType toType) {
|
||||
|
||||
private predicate convNumericFloat(SimpleType toType) { toType instanceof DoubleType }
|
||||
|
||||
private class SpanType extends GenericType {
|
||||
SpanType() { this.getUnboundGeneric() instanceof SystemSpanStruct }
|
||||
|
||||
Type getElementType() { result = this.getTypeArgument(0) }
|
||||
}
|
||||
|
||||
private class ReadOnlySpanType extends GenericType {
|
||||
ReadOnlySpanType() { this.getUnboundGeneric() instanceof SystemReadOnlySpanStruct }
|
||||
|
||||
Type getElementType() { result = this.getTypeArgument(0) }
|
||||
}
|
||||
|
||||
private class SimpleArrayType extends ArrayType {
|
||||
SimpleArrayType() {
|
||||
this.getRank() = 1 and
|
||||
this.getDimension() = 1
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* INTERNAL: Do not use.
|
||||
*
|
||||
* Holds if there is an implicit span conversion from `fromType` to `toType`.
|
||||
*
|
||||
* 10.2.1: Implicit span conversions (added in C# 14).
|
||||
* [Documentation](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/proposals/csharp-14.0/first-class-span-types#span-conversions)
|
||||
*/
|
||||
predicate convSpan(Type fromType, Type toType) {
|
||||
fromType.(SimpleArrayType).getElementType() = toType.(SpanType).getElementType()
|
||||
or
|
||||
exists(Type fromElementType, Type toElementType |
|
||||
(
|
||||
fromElementType = fromType.(SimpleArrayType).getElementType() or
|
||||
fromElementType = fromType.(SpanType).getElementType() or
|
||||
fromElementType = fromType.(ReadOnlySpanType).getElementType()
|
||||
) and
|
||||
toElementType = toType.(ReadOnlySpanType).getElementType()
|
||||
|
|
||||
convRefTypeNonNull(fromElementType, toElementType)
|
||||
)
|
||||
or
|
||||
fromType instanceof SystemStringClass and
|
||||
toType.(ReadOnlySpanType).getElementType() instanceof CharType
|
||||
}
|
||||
|
||||
/**
|
||||
* INTERNAL: Do not use.
|
||||
*
|
||||
|
||||
81
csharp/ql/test/library-tests/conversion/span/Span.cs
Normal file
81
csharp/ql/test/library-tests/conversion/span/Span.cs
Normal file
@@ -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
|
||||
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
csharp/ql/test/library-tests/conversion/span/span.expected
Normal file
47
csharp/ql/test/library-tests/conversion/span/span.expected
Normal file
@@ -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> |
|
||||
9
csharp/ql/test/library-tests/conversion/span/span.ql
Normal file
9
csharp/ql/test/library-tests/conversion/span/span.ql
Normal file
@@ -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
|
||||
Reference in New Issue
Block a user