diff --git a/src/ZeroSerializerGenerator.cs b/src/ZeroSerializerGenerator.cs index 52628b7..5efac09 100644 --- a/src/ZeroSerializerGenerator.cs +++ b/src/ZeroSerializerGenerator.cs @@ -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) diff --git a/tests/Diagnostics/DiagnosticTests.cs b/tests/Diagnostics/DiagnosticTests.cs index 54f16fd..37f257d 100644 --- a/tests/Diagnostics/DiagnosticTests.cs +++ b/tests/Diagnostics/DiagnosticTests.cs @@ -234,6 +234,29 @@ await CSharpSourceGeneratorVerifier.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.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() {