Skip to content
Merged
Show file tree
Hide file tree
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
14 changes: 14 additions & 0 deletions src/ZeroSerializerGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -683,6 +683,20 @@ private static bool TryGetBlittableStructByteCount(INamedTypeSymbol candidateTyp

private static bool IsEligibleForBlittable(INamedTypeSymbol structType)
{
foreach (SyntaxReference syntaxReference in structType.DeclaringSyntaxReferences)
{
if (syntaxReference.GetSyntax() is TypeDeclarationSyntax declaration)
{
foreach (SyntaxToken modifier in declaration.Modifiers)
{
if (modifier.IsKind(SyntaxKind.PartialKeyword))
{
return false;
}
}
}
}

foreach (ISymbol member in structType.GetMembers())
{
if (member is IFieldSymbol field)
Expand Down
23 changes: 23 additions & 0 deletions tests/Diagnostics/DiagnosticTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,29 @@ await CSharpSourceGeneratorVerifier<ZeroSerializerGenerator>.VerifySourceGenerat
);
}

[Fact]
public async Task ZEROS002_Violation_PartialStructWithLayout()
{
string source = @"
using System.Runtime.InteropServices;
using ZeroSerializer;

[{|#0:StructLayout(LayoutKind.Sequential, Pack = 1)|}]
[ZeroSerializer]
public partial struct MyPartialStruct
{
public int Value { get; set; }
}
";

await CSharpSourceGeneratorVerifier<ZeroSerializerGenerator>.VerifySourceGeneratorAsync(
source,
new DiagnosticResult("ZEROS002", DiagnosticSeverity.Warning)
.WithLocation(0)
.WithMessage("Struct 'MyPartialStruct' is marked with StructLayout(LayoutKind.Sequential, Pack = 1) but does not meet the requirements to be a blittable struct")
);
}

[Fact]
public async Task ZEROS003_Violation_UnmarkedBlittableNestedStruct()
{
Expand Down
Loading