From 3cbb744f9102dfeb0a2505dd9dcdf80818a45c78 Mon Sep 17 00:00:00 2001 From: Libby Date: Thu, 12 Nov 2026 01:51:03 +0000 Subject: [PATCH] GeneratorConsumer: check the generated formatter behind the guard's wrapper (CultUnionGuard.Inner) The check that each document type runs MessagePack's source-generated formatter saw the guard's SlotFormatter and exited 1 (Soul s3 generator-consumer-check-red). The guard owns the one unwrap, internal, visible to the consumer through InternalsVisibleTo; that also exposes the library's internal GeneratedMessagePackResolver, so the consumer suppresses CS0436 (its own is the type used). Co-Authored-By: Claude Opus 5.5 --- .../CultUnionGuard.cs | 17 +++++++++++++++-- .../GameCult.Caching.MessagePack.csproj | 1 + .../GameCult.Caching.GeneratorConsumer.csproj | 3 +++ .../Program.cs | 3 ++- 4 files changed, 21 insertions(+), 3 deletions(-) diff --git a/src/GameCult.Caching.MessagePack/CultUnionGuard.cs b/src/GameCult.Caching.MessagePack/CultUnionGuard.cs index b8e38f3c..86d26624 100644 --- a/src/GameCult.Caching.MessagePack/CultUnionGuard.cs +++ b/src/GameCult.Caching.MessagePack/CultUnionGuard.cs @@ -26,6 +26,15 @@ internal sealed class CultUnionGuard(IFormatterResolver inner) : IFormatterResol return inner.GetFormatter() is not { } formatter ? null : new Formatter(formatter, arms); } + // The formatter that writes and reads the bytes: the one a guard wrapper delegates to, else the formatter itself. A check that a + // generated formatter is the one in use asks this (tests/GameCult.Caching.GeneratorConsumer). + internal static object Inner(object formatter) => formatter is IWrapper wrapper ? wrapper.Inner : formatter; + + private interface IWrapper + { + object Inner { get; } + } + // The one decision of whether a type is measured: the formatter the chain hands out for an int-keyed [MessagePackObject] type is // MessagePack's own [Key]-layout formatter, the very instance StandardResolver (or its AllowPrivate twin) builds for it. A formatter a // consumer resolver or a [MessagePackFormatter] attribute supplies owns its layout and its tolerance, and a [MessagePackObject] @@ -56,8 +65,10 @@ internal sealed class CultUnionGuard(IFormatterResolver inner) : IFormatterResol // inner formatter read it unchanged: no byte scan, no second decode. With no scope open it is a depth-free pass-through. // Declared slots are what the type's own formatter writes: the array header of one instance written through it, measured on the // first read inside a scope (not in GetFormatter, so a self-referential type never recurses into its own resolver cache) and kept. - private sealed class SlotFormatter(IMessagePackFormatter inner) : IMessagePackFormatter + private sealed class SlotFormatter(IMessagePackFormatter inner) : IMessagePackFormatter, IWrapper { + object IWrapper.Inner => inner; + private const int NotMeasured = -1; // A type no instance of which can be written is not guarded: no count can exceed this. @@ -117,8 +128,10 @@ internal sealed class CultUnionGuard(IFormatterResolver inner) : IFormatterResol } } - private sealed class Formatter(IMessagePackFormatter inner, UnionAttribute[] arms) : IMessagePackFormatter + private sealed class Formatter(IMessagePackFormatter inner, UnionAttribute[] arms) : IMessagePackFormatter, IWrapper { + object IWrapper.Inner => inner; + private readonly HashSet keys = arms.Select(arm => arm.Key).ToHashSet(); private readonly HashSet types = arms.Select(arm => arm.SubType).ToHashSet(); diff --git a/src/GameCult.Caching.MessagePack/GameCult.Caching.MessagePack.csproj b/src/GameCult.Caching.MessagePack/GameCult.Caching.MessagePack.csproj index 6261ca3d..63cbf614 100644 --- a/src/GameCult.Caching.MessagePack/GameCult.Caching.MessagePack.csproj +++ b/src/GameCult.Caching.MessagePack/GameCult.Caching.MessagePack.csproj @@ -11,6 +11,7 @@ + diff --git a/tests/GameCult.Caching.GeneratorConsumer/GameCult.Caching.GeneratorConsumer.csproj b/tests/GameCult.Caching.GeneratorConsumer/GameCult.Caching.GeneratorConsumer.csproj index 6d443a4f..4231195a 100644 --- a/tests/GameCult.Caching.GeneratorConsumer/GameCult.Caching.GeneratorConsumer.csproj +++ b/tests/GameCult.Caching.GeneratorConsumer/GameCult.Caching.GeneratorConsumer.csproj @@ -4,6 +4,9 @@ net10.0 enable false + + $(NoWarn);CS0436 diff --git a/tests/GameCult.Caching.GeneratorConsumer/Program.cs b/tests/GameCult.Caching.GeneratorConsumer/Program.cs index 2511ed80..ab83330f 100644 --- a/tests/GameCult.Caching.GeneratorConsumer/Program.cs +++ b/tests/GameCult.Caching.GeneratorConsumer/Program.cs @@ -20,7 +20,8 @@ var documents = new (Type Type, object Value)[] foreach (var (type, _) in documents) { - var formatter = options.Resolver.GetFormatterDynamic(type)!.GetType(); + // The guard wraps a measured type's formatter; the generated formatter it delegates to is the one that runs. + var formatter = CultUnionGuard.Inner(options.Resolver.GetFormatterDynamic(type)!).GetType(); if (formatter.Assembly.IsDynamic || !(formatter.FullName ?? "").Contains("GeneratedMessagePackResolver")) return Fail($"{type.Name} is formatted by {formatter.FullName}, not a generated formatter"); }