Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 4 additions & 8 deletions src/MemoryPack.Generator/MemoryPackGenerator.Emitter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ public void Emit(StringBuilder writer, IGeneratorContext context)
string staticRegisterFormatterMethod, staticMemoryPackableMethod, scopedRef, constraint, registerBody, registerT;
var fixedSizeInterface = "";
var fixedSizeMethod = "";
scopedRef = (context.IsCSharp11OrGreater())
scopedRef = context.IsNet7OrGreater && context.IsCSharp11OrGreater()
Copy link

Copilot AI Jan 26, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new scopedRef condition (context.IsNet7OrGreater && context.IsCSharp11OrGreater()) fixes the reported mismatch between generated scoped parameters and the base formatter signatures, but there doesn't appear to be a regression test that covers this specific matrix (C# 11 language version with IsNet7OrGreater == false). It would be helpful to add a generator test that simulates this environment and asserts that the generated formatter methods use plain ref, so future changes to IsNet7OrGreater/language-version logic don't reintroduce this compiler error.

Copilot uses AI. Check for mistakes.
? "scoped ref"
: "ref";
if (!context.IsNet7OrGreater)
Expand All @@ -350,11 +350,7 @@ public void Emit(StringBuilder writer, IGeneratorContext context)
registerT = $"global::MemoryPack.MemoryPackFormatterProvider.Register<{TypeName}>();";

// similar as VersionTolerantOptimized but not includes String, Array
var fixedSize = false;
if (Members.All(x => x.Kind is MemberKind.Unmanaged or MemberKind.Enum or MemberKind.UnmanagedNullable or MemberKind.Blank))
{
fixedSize = true;
}
bool fixedSize = Members.All(x => x.Kind is MemberKind.Unmanaged or MemberKind.Enum or MemberKind.UnmanagedNullable or MemberKind.Blank);

var callbackCount = new[] { this.OnSerializing, this.OnSerialized, this.OnDeserialized, this.OnDeserializing }.Select(x => x.Length).Sum();
if (fixedSize && GenerateType == GenerateType.Object && !this.IsValueType && callbackCount == 0)
Expand Down Expand Up @@ -984,7 +980,7 @@ string EmitUnionTemplate(IGeneratorContext context)
var register = (context.IsNet7OrGreater)
? $"global::MemoryPack.MemoryPackFormatterProvider.Register<{TypeName}>();"
: "RegisterFormatter();";
var scopedRef = context.IsCSharp11OrGreater()
var scopedRef = context.IsNet7OrGreater && context.IsCSharp11OrGreater()
? "scoped ref"
: "ref";
string serializeMethodSignarture = context.IsForUnity
Expand Down Expand Up @@ -1045,7 +1041,7 @@ public override void Deserialize(ref MemoryPackReader reader, {{scopedRef}} {{Ty

public void EmitUnionFormatterTemplate(StringBuilder writer, IGeneratorContext context, INamedTypeSymbol formatterSymbol)
{
var scopedRef = context.IsCSharp11OrGreater()
var scopedRef = context.IsNet7OrGreater && context.IsCSharp11OrGreater()
? "scoped ref"
: "ref";
string serializeMethodSignarture = context.IsForUnity
Expand Down
Loading