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 <noreply@anthropic.com>
This commit is contained in:
parent
f3b62a7ee7
commit
3cbb744f91
4 changed files with 21 additions and 3 deletions
|
|
@ -26,6 +26,15 @@ internal sealed class CultUnionGuard(IFormatterResolver inner) : IFormatterResol
|
|||
return inner.GetFormatter<T>() is not { } formatter ? null : new Formatter<T>(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<T>(IMessagePackFormatter<T> inner) : IMessagePackFormatter<T>
|
||||
private sealed class SlotFormatter<T>(IMessagePackFormatter<T> inner) : IMessagePackFormatter<T>, 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<T>(IMessagePackFormatter<T> inner, UnionAttribute[] arms) : IMessagePackFormatter<T>
|
||||
private sealed class Formatter<T>(IMessagePackFormatter<T> inner, UnionAttribute[] arms) : IMessagePackFormatter<T>, IWrapper
|
||||
{
|
||||
object IWrapper.Inner => inner;
|
||||
|
||||
private readonly HashSet<int> keys = arms.Select(arm => arm.Key).ToHashSet();
|
||||
private readonly HashSet<Type> types = arms.Select(arm => arm.SubType).ToHashSet();
|
||||
|
||||
|
|
|
|||
|
|
@ -11,6 +11,7 @@
|
|||
<PackageReference Include="System.Text.Json" Version="9.0.10" />
|
||||
<PackageReference Include="MessagePackAnalyzer" Version="3.1.7" PrivateAssets="all" />
|
||||
<InternalsVisibleTo Include="GameCult.Caching.Tests" />
|
||||
<InternalsVisibleTo Include="GameCult.Caching.GeneratorConsumer" />
|
||||
<None Include="buildTransitive\GameCult.Caching.MessagePack.targets" Pack="true" PackagePath="buildTransitive\" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
|
|
|
|||
|
|
@ -4,6 +4,9 @@
|
|||
<TargetFramework>net10.0</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
<IsPackable>false</IsPackable>
|
||||
<!-- GameCult.Caching.MessagePack lets this check see CultUnionGuard.Inner (InternalsVisibleTo), which also shows it that
|
||||
assembly's internal GeneratedMessagePackResolver; this project's own generated one is the type in use. -->
|
||||
<NoWarn>$(NoWarn);CS0436</NoWarn>
|
||||
</PropertyGroup>
|
||||
|
||||
<PropertyGroup Condition="'$(Aot)' == 'true'">
|
||||
|
|
|
|||
|
|
@ -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");
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue