Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
72 changes: 68 additions & 4 deletions csharp/ql/lib/semmle/code/csharp/Conversion.qll
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ private module Cached {
*
* - Identity conversions
* - Implicit numeric conversions
* - Implicit span conversions
* - Implicit nullable conversions
* - Implicit reference conversions
* - Boxing conversions
Expand All @@ -38,6 +39,8 @@ private module Cached {
or
convNumeric(fromType, toType)
or
convSpan(fromType, toType)
or
convNullableType(fromType, toType)
or
convRefTypeNonNull(fromType, toType)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -491,6 +495,53 @@ 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
}
}
Comment on lines +510 to +515
Copy link

Copilot AI Dec 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The SimpleArrayType class lacks documentation explaining what distinguishes it from other array types. Add a docstring clarifying that this represents single-dimensional, zero-based arrays (SZ arrays), as getRank() = 1 and getDimension() = 1 restricts it to this specific array form.

Copilot uses AI. Check for mistakes.

/**
* 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()
|
convIdentity(fromElementType, toElementType)
or
convCovariance(fromElementType, toElementType)
)
or
fromType instanceof SystemStringClass and
toType.(ReadOnlySpanType).getElementType() instanceof CharType
}

/**
* INTERNAL: Do not use.
*
Expand Down Expand Up @@ -784,8 +835,8 @@ predicate convConversionOperator(Type fromType, Type toType) {
)
}

/** 13.1.3.2: Variance conversion. */
private predicate convVariance(GenericType fromType, GenericType toType) {
pragma[nomagic]
private predicate convVarianceAux(UnboundGenericType ugt, GenericType fromType, GenericType toType) {
Comment thread Fixed
// Semantically equivalent with
// ```ql
// ugt = fromType.getUnboundGeneric()
Expand All @@ -805,10 +856,23 @@ private predicate convVariance(GenericType fromType, GenericType toType) {
// ```
// but performance is improved by explicitly evaluating the `i`th argument
// only when all preceding arguments are convertible.
Variance::convVarianceSingle(_, fromType, toType)
Variance::convVarianceSingle(ugt, fromType, toType)
or
Variance::convVarianceMultiple(ugt, fromType, toType, ugt.getNumberOfTypeParameters() - 1)
}

/** 13.1.3.2: Variance conversion. */
private predicate convVariance(GenericType fromType, GenericType toType) {
convVarianceAux(_, fromType, toType)
}

/**
* Holds, if `fromType` is covariance convertible to `toType`.
*/
private predicate convCovariance(GenericType fromType, GenericType toType) {
exists(UnboundGenericType ugt |
Variance::convVarianceMultiple(ugt, fromType, toType, ugt.getNumberOfTypeParameters() - 1)
convVarianceAux(ugt, fromType, toType) and
forall(TypeParameter tp | tp = ugt.getATypeParameter() | tp.isOut())
Comment thread
michaelnebel marked this conversation as resolved.
Outdated
)
}
Comment thread
michaelnebel marked this conversation as resolved.
Outdated

Expand Down
54 changes: 54 additions & 0 deletions csharp/ql/test/library-tests/conversion/span/Span.cs
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
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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;
}
}
16 changes: 16 additions & 0 deletions csharp/ql/test/library-tests/conversion/span/span.expected
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>> |
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would have expected something like

| Span<Interface<Derived, string>> | ReadOnlySpan<Interface<Base, string>> |

here?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The 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 out. However, as you point out (and this is also accepted by the compiler), identity conversions are also accepted for non-covariant type parameters.
I will improve this.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The 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> |
9 changes: 9 additions & 0 deletions csharp/ql/test/library-tests/conversion/span/span.ql
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
Loading