Add [OverloadResolutionPriority(1)] to MemoryMarshal.AsBytes(Span) and Cast(Span)#126252
Open
Add [OverloadResolutionPriority(1)] to MemoryMarshal.AsBytes(Span) and Cast(Span)#126252
Conversation
Contributor
|
Tagging subscribers to this area: @dotnet/area-system-memory |
…d Cast(Span) Add the attribute to prioritize the mutable Span overloads over the ReadOnlySpan overloads for AsBytes and Cast, matching what was already done for AsRef. This fixes a C# 14 regression where passing an array to these methods would resolve to the ReadOnlySpan overload. Agent-Logs-Url: https://github.com/dotnet/runtime/sessions/28da867d-a92d-4422-b825-5bc281480c04 Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Increase priority of MemoryMarshal.AsBytes/Cast with OverloadResolutionPriority
Add [OverloadResolutionPriority(1)] to MemoryMarshal.AsBytes(Span) and Cast(Span)
Mar 28, 2026
stephentoub
approved these changes
Mar 28, 2026
Contributor
There was a problem hiding this comment.
Pull request overview
This PR updates System.Runtime.InteropServices.MemoryMarshal to account for C# 14 overload resolution changes by explicitly prioritizing the mutable Span<T> overloads over the ReadOnlySpan<T> overloads, preventing source-breaking overload selection in common call sites (e.g., passing an array and assigning to Span<...>).
Changes:
- Add
[OverloadResolutionPriority(1)]toMemoryMarshal.AsBytes<T>(Span<T>)andMemoryMarshal.Cast<TFrom, TTo>(Span<TFrom>)in CoreLib. - Mirror the same priority attribute additions in the
System.Runtimereference assembly. - Add new unit tests that validate arrays bind to the
Span<T>overloads (so assignments toSpan<...>continue to compile under C# 14).
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| src/libraries/System.Runtime/ref/System.Runtime.cs | Adds overload priority attributes to the public ref signatures for AsBytes(Span) and Cast(Span). |
| src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/MemoryMarshal.cs | Prioritizes the mutable Span<T> overloads for AsBytes and Cast to preserve pre-C#14 call-site behavior. |
| src/libraries/System.Memory/tests/MemoryMarshal/AsBytesSpan.cs | Adds a regression test ensuring array arguments select the Span<T> AsBytes overload. |
| src/libraries/System.Memory/tests/MemoryMarshal/CastSpan.cs | Adds a regression test ensuring array arguments select the Span<T> Cast overload. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
C# 14 overload resolution changes cause
MemoryMarshal.AsBytes<T>(int[])andMemoryMarshal.Cast<TFrom, TTo>(int[])to resolve to theReadOnlySpanoverloads instead of theSpanoverloads, breaking existing code that assigns the result toSpan<byte>. This is the same issue that was already fixed forAsRef<T>in #122217 and #122330.Changes
MemoryMarshal.cs: Added[OverloadResolutionPriority(1)]toAsBytes<T>(Span<T>)andCast<TFrom, TTo>(Span<TFrom>), matching the existingAsRef<T>(Span<byte>)patternSystem.Runtime.cs(ref assembly): Added corresponding attribute declarationsSpan_AsBytes_ImplicitSpanConversion_ReturnsMutableSpanandCastSpan_ImplicitSpanConversion_ReturnsMutableSpan, following the existingAsRef_ImplicitSpanConversion_ReturnsMutableRefpattern💬 Send tasks to Copilot coding agent from Slack and Teams to turn conversations into code. Copilot posts an update in your thread when it's finished.