Skip to content

Conversation

@michaelnebel
Copy link
Contributor

@michaelnebel michaelnebel commented Dec 18, 2025

In this PR we introduce support for implicit span conversions. The feature is documented here.
That is, C# now supports implicit conversions like (and more). Some of these were previously supported by implicit operators, but now they are an integrated part of the language.

string[] arr;
Span<string> span = arr;
ReadOnlySpan<string> rospan = arr; 

There is out-of-the-box extractor support for this.

DCA looks good. Even though the content of the PR might affect some queries (like cs/useless-upcast) it doesn't look like there are any changes to results.

@github-actions github-actions bot added the C# label Dec 18, 2025
@michaelnebel michaelnebel force-pushed the csharp/implicitspanconversions branch from 16a8982 to 741ef80 Compare December 18, 2025 10:59
@michaelnebel michaelnebel force-pushed the csharp/implicitspanconversions branch from 741ef80 to 1817f9c Compare December 18, 2025 11:51
@michaelnebel michaelnebel marked this pull request as ready for review December 18, 2025 15:00
@michaelnebel michaelnebel requested a review from a team as a code owner December 18, 2025 15:00
Copilot AI review requested due to automatic review settings December 18, 2025 15:00
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR adds support for implicit span conversions in C# 14, allowing automatic conversions between arrays, Span<T>, and ReadOnlySpan<T> types. This language feature is now integrated into the CodeQL library to properly model these conversions during analysis.

Key changes:

  • Implemented convSpan predicate to handle implicit span conversions including array-to-span, span-to-readonly-span, and string-to-readonly-span-of-char conversions
  • Added covariance support for span conversions through new convCovariance helper predicate
  • Added comprehensive test cases covering various span conversion scenarios

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
csharp/ql/lib/semmle/code/csharp/Conversion.qll Core implementation of span conversion logic and covariance helper
csharp/ql/test/library-tests/conversion/span/Span.cs Test cases demonstrating various span conversion scenarios
csharp/ql/test/library-tests/conversion/span/span.ql Query to verify span conversions are correctly identified
csharp/ql/test/library-tests/conversion/span/span.expected Expected output for span conversion test cases
csharp/ql/lib/change-notes/2025-12-18-implicit-span-conversions.md Release note documenting the new feature

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +510 to +515
private class SimpleArrayType extends ArrayType {
SimpleArrayType() {
this.getRank() = 1 and
this.getDimension() = 1
}
}
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.
Comment on lines +872 to 877
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())
)
}
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 convCovariance predicate lacks documentation explaining its purpose and relationship to convVariance. Add a docstring clarifying that this predicate specifically checks for covariance conversions (where all type parameters must be covariant/out), distinguishing it from general variance conversions which may include contravariance.

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant