-
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
base: main
Are you sure you want to change the base?
C# 14: Implicit span conversions. #21065
Conversation
16a8982 to
741ef80
Compare
741ef80 to
1817f9c
Compare
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.
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
convSpanpredicate 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
convCovariancehelper 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.
| private class SimpleArrayType extends ArrayType { | ||
| SimpleArrayType() { | ||
| this.getRank() = 1 and | ||
| this.getDimension() = 1 | ||
| } | ||
| } |
Copilot
AI
Dec 18, 2025
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 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.
| 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()) | ||
| ) | ||
| } |
Copilot
AI
Dec 18, 2025
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 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.
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.
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.