Skip to content

Commit

Permalink
Merge pull request #331 from kostasbesson/feature/issue-300
Browse files Browse the repository at this point in the history
[issue-300] Fix for Unmanaged Structs Using GenerateType.VersionTolerant (MEMPACK041)
  • Loading branch information
neuecc authored Sep 18, 2024
2 parents 7e70a28 + 1a19f74 commit 43548e8
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
8 changes: 8 additions & 0 deletions src/MemoryPack.Generator/DiagnosticDescriptors.cs
Original file line number Diff line number Diff line change
Expand Up @@ -332,4 +332,12 @@ internal static class DiagnosticDescriptors
category: Category,
defaultSeverity: DiagnosticSeverity.Error,
isEnabledByDefault: true);

public static readonly DiagnosticDescriptor VersionTolerantOnUnmanagedStruct = new(
id: "MEMPACK041",
title: "Invalid usage of VersionTolerant on unmanaged struct",
messageFormat: "The unmanaged struct '{0}' cannot be used for VersionTolerant serialization.",
category: Category,
defaultSeverity: DiagnosticSeverity.Error,
isEnabledByDefault: true);
}
6 changes: 6 additions & 0 deletions src/MemoryPack.Generator/MemoryPackGenerator.Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,12 @@ public bool Validate(TypeDeclarationSyntax syntax, IGeneratorContext context, bo

if (this.IsUnmanagedType)
{
if (GenerateType is GenerateType.VersionTolerant)
{
context.ReportDiagnostic(Diagnostic.Create(DiagnosticDescriptors.VersionTolerantOnUnmanagedStruct, syntax.Identifier.GetLocation(), Symbol.Name));
noError = false;
}

var structLayoutFields = this.Symbol.GetAllMembers()
.OfType<IFieldSymbol>()
.Select(x =>
Expand Down
15 changes: 14 additions & 1 deletion tests/MemoryPack.Tests/GeneratorDiagnosticsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -687,9 +687,22 @@ public Tester(int i1)
}
""");
}

[Fact]
public void MEMPACK041_UnmanagedStructCannotBeVersionTolerant()
{
Compile(41, """
using MemoryPack;
[MemoryPackable(GenerateType.VersionTolerant)]
public partial struct Tester
{
[MemoryPackOrder(0)]
public int I1 { get; init; }
}
""");
}
}


#endif

0 comments on commit 43548e8

Please sign in to comment.