From 1461b8d3998d2dbe7072a1e6f2ac25d335e6ada0 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Tue, 18 Aug 2026 00:49:44 +0000 Subject: [PATCH] Disallow partial structs as blittable structs --- src/ZeroSerializerGenerator.cs | 14 ++++++++++++++ tests/Diagnostics/DiagnosticTests.cs | 23 +++++++++++++++++++++++ 2 files changed, 37 insertions(+) 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() {