Skip to content

Commit

Permalink
Merge pull request #233 from Cysharp/hadashiA/add-union-validation
Browse files Browse the repository at this point in the history
Add validation to prevent `VersionTorelant` for Union base type.
  • Loading branch information
hadashiA authored Mar 14, 2024
2 parents efc72a2 + 820830b commit 27feb86
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 2 deletions.
8 changes: 8 additions & 0 deletions src/MemoryPack.Generator/DiagnosticDescriptors.cs
Original file line number Diff line number Diff line change
Expand Up @@ -316,4 +316,12 @@ internal static class DiagnosticDescriptors
category: Category,
defaultSeverity: DiagnosticSeverity.Error,
isEnabledByDefault: true);

public static readonly DiagnosticDescriptor GenerateTypeCannotSpeciyToUnionBaseType = new(
id: "MEMPACK039",
title: "GenerateType cannot be specified for the Union base type itself",
messageFormat: "The MemoryPackable object '{0}' cannot specify '{1}'. Because it is Union base type.",
category: Category,
defaultSeverity: DiagnosticSeverity.Error,
isEnabledByDefault: true);
}
7 changes: 5 additions & 2 deletions src/MemoryPack.Generator/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,16 @@ public static bool TryGetMemoryPackableType(this ITypeSymbol symbol, ReferenceSy
var packableCtorArgs = memPackAttr?.ConstructorArguments;
generateType = GenerateType.Object;
serializeLayout = SerializeLayout.Sequential;

if (memPackAttr == null || packableCtorArgs == null)
{
generateType = GenerateType.NoGenerate;
var memPackUnionFormatterAttr = symbol.GetAttribute(references.MemoryPackUnionFormatterAttribute);
generateType = memPackUnionFormatterAttr != null ? GenerateType.Union : GenerateType.NoGenerate;
serializeLayout = SerializeLayout.Sequential;
return false;
}
else if (packableCtorArgs.Value.Length != 0)

if (packableCtorArgs.Value.Length != 0)
{
// MemoryPackable has three attribtues
// [GenerateType generateType]
Expand Down
6 changes: 6 additions & 0 deletions src/MemoryPack.Generator/MemoryPackGenerator.Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,12 @@ public bool Validate(TypeDeclarationSyntax syntax, IGeneratorContext context, bo
noError = false;
}

if (GenerateType != GenerateType.Union)
{
context.ReportDiagnostic(Diagnostic.Create(DiagnosticDescriptors.GenerateTypeCannotSpeciyToUnionBaseType, syntax.Identifier.GetLocation(), Symbol.Name, GenerateType));
noError = false;
}

if (UnionTags.Select(x => x.Tag).HasDuplicate())
{
context.ReportDiagnostic(Diagnostic.Create(DiagnosticDescriptors.UnionTagDuplicate, syntax.Identifier.GetLocation(), Symbol.Name));
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ public partial class MemberKindsAllUnmanaged

int[] kArray = new int[1];

public MemberKindsAllUnmanaged(int c, int d)
{
this.C = c;
this.D = d;
}

public ref int K
{
get { return ref kArray[0]; }
Expand Down
1 change: 1 addition & 0 deletions tests/MemoryPack.Tests/Models/Union.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ public partial class ForExternalUnionFormatter3
public partial class NoraType
{
public IForExternalUnion? ExtUnion { get; set; }
public UnionAbstractClass? AbstractUnion { get; set; }
}

// Union for record
Expand Down

0 comments on commit 27feb86

Please sign in to comment.