Some MemoryMarshal methods are source incompatible in .NET11

オープン 初心者向け
#137,334 コメント 4 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
2/5
見積もり時間
1〜3時間
初心者へのやさしさ
75/100
issue の種類
ドキュメント
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
csharp
領域
documentation

調査の方向性

まず、PR #126252 のオーバーロード優先順位の変更と、ソース互換性に関する注記の .NET Runtime ドキュメント規約を読みます。影響を受ける MemoryMarshal オーバーロードに this を渡す readonly struct のメソッドはコンパイルに失敗する場合があることを文書化し、issue の例または回避策を含めます。互換性の破壊とその範囲が正式に文書化されれば完了です。

索引モデルが issue の本文から書いたものです。

説明

area-System.Memory untriaged

#126252 added [OverloadResolutionPriority(1)] to several methods in System.Runtime.InteropServices.MemoryMarshal to prefer Span<T> over ReadOnlySpan<T>. This change fixed source incompatibility for a larger issue, but at the expense of source compatibility for edge cases.

One such example is attempting to use these methods by passing this inside an instance method of a struct that has implicit conversions to both Span<T> and ReadOnlySpan<T> when the method is marked readonly.

This change should be formally documented to acknowledge that such code patterns will now fail to compile.

Minimum example


[InlineArray(8)]
public struct CoolInlineArray {
    byte data;

	// Works in .NET10 - chooses ReadOnlySpan<T> overload
	// Fails in .NET11 - chooses Span<T> overload
	internal readonly ulong GetLongA() => MemoryMarshal.Cast<byte, ulong>(this)[0];

	// Works in .NET10
	// Works in .NET11
	internal ulong GetLongB() => MemoryMarshal.Cast<byte, ulong>(this)[0];
}

Workarounds

This pattern is likely incredibly rare, and easily addressed:

  • Explicitly cast this to ReadOnlySpan<T>
  • Remove readonly from the method (potentially undesirable for performance reasons)
主要言語
C#
スター
18.4k
フォーク
5.8k
平均マージ
2日 19時間
マージ済み PR(30日)
590

環境構築

Codespaces で開く

このプロジェクトの開発コンテナを、あなたの GitHub アカウントでブラウザ上に起動します。

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

dotnet/runtime のほかの issue

dotnet/runtime の issue をすべて見る

似ている issue

C# の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。