CS0246 error when using custom formatter for generic types

オープン
#2,160 コメント 3 件 リアクション 2 件 担当者 0 名 GitHub で見る

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

評価

難易度
3/5
見積もり時間
1〜2日
初心者へのやさしさ
45/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
停滞
技術スタック
csharp
領域
tooling

調査の方向性

.NET 8 上で MessagePack v3.1.3 を使用し、提供された Person、Foo、FooMessagePackFormatter の定義で問題を再現してから、レポートに示されている生成された PersonFormatter.g.cs を調査します。生成された formatter が有効なジェネリック型参照を使用し、サンプルが CS0246 なしでコンパイルできれば作業完了です。

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

説明

Bug description

CS0246 error when using generic class in custom formatter when class with MessagePackObject(true) attribute or set the Key attribute to string
CS0246 does not occur if Key attribute is set to a numeric value

Repro steps
    [MessagePackObject(true)]
    public record Person
    {
        [MessagePackFormatter(typeof(FooMessagePackFormatter<int>))]
        public Foo<int> Foo { get; set; } = new Foo<int>();
    }

    public class Foo<T> { }

    // Formatter  
    public class FooMessagePackFormatter<T> : IMessagePackFormatter<Foo<T>>
    {
        public Foo<T> Deserialize(ref MessagePackReader reader, MessagePackSerializerOptions options) => throw new NotImplementedException();
        public void Serialize(ref MessagePackWriter writer, Foo<T> value, MessagePackSerializerOptions options) => throw new NotImplementedException();
    }

Error

Severity	Code	Description	Project	File	Line	Suppression State	Details
Error (active)	CS0246	The type or namespace name 'T' could not be found (are you missing a using directive or an assembly reference?)	ConsoleAppTest	~\ConsoleAppTest\obj\Debug\net8.0\MessagePack.SourceGenerator\MessagePack.SourceGenerator.MessagePackGenerator\Formatters.MessagePack.GeneratedMessagePackResolver.ConsoleAppTest.PersonFormatter.g.cs	12		

Formatters.MessagePack.GeneratedMessagePackResolver.ConsoleAppTest.PersonFormatter.g.cs

// <auto-generated />

#pragma warning disable 618, 612, 414, 168, CS1591, SA1129, SA1309, SA1312, SA1403, SA1649

using MsgPack = global::MessagePack;

namespace MessagePack {
partial class GeneratedMessagePackResolver {
internal partial class ConsoleAppTest {
	internal sealed class PersonFormatter : global::MessagePack.Formatters.IMessagePackFormatter<global::ConsoleAppTest.Person>
	{
		private readonly global::ConsoleAppTest.FooMessagePackFormatter<T> __FooCustomFormatter__ = new global::ConsoleAppTest.FooMessagePackFormatter<int>();
		// Foo
		private static global::System.ReadOnlySpan<byte> GetSpan_Foo() => new byte[1 + 3] { 163, 70, 111, 111 };

		public void Serialize(ref global::MessagePack.MessagePackWriter writer, global::ConsoleAppTest.Person value, global::MessagePack.MessagePackSerializerOptions options)
		{
			if (value is null)
			{
				writer.WriteNil();
				return;
			}

			writer.WriteMapHeader(1);
			writer.WriteRaw(GetSpan_Foo());
			this.__FooCustomFormatter__.Serialize(ref writer, value.Foo, options);
		}

		public global::ConsoleAppTest.Person Deserialize(ref global::MessagePack.MessagePackReader reader, global::MessagePack.MessagePackSerializerOptions options)
		{
			if (reader.TryReadNil())
			{
				return null;
			}

			options.Security.DepthStep(ref reader);
			var length = reader.ReadMapHeader();
			var ____result = new global::ConsoleAppTest.Person();

			for (int i = 0; i < length; i++)
			{
				var stringKey = global::MessagePack.Internal.CodeGenHelpers.ReadStringSpan(ref reader);
				switch (stringKey.Length)
				{
					default:
					FAIL:
					  reader.Skip();
					  continue;
					case 3:
					    if (global::MessagePack.Internal.AutomataKeyGen.GetKey(ref stringKey) != 7302982UL) { goto FAIL; }

					    ____result.Foo = this.__FooCustomFormatter__.Deserialize(ref reader, options);
					    continue;

				}
			}

			reader.Depth--;
			return ____result;
		}
	}

}
}
}
Expected behavior

The code should compile without error CS0246.

Actual behavior
  • Version used: MessagePack v3.1.3
  • Runtime: .Net 8.0
主要言語
C#
スター
6.8k
フォーク
783
平均マージ
19時間 47分
マージ済み PR(30日)
3

環境構築

Codespaces で開く

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

はじめの一歩

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

MessagePack-CSharp/MessagePack-CSharp のほかの issue

MessagePack-CSharp/MessagePack-CSharp の issue をすべて見る

似ている issue

C# の issue をもっと見る

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

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