diff --git a/src/ZeroSerializerGenerator.cs b/src/ZeroSerializerGenerator.cs index 52628b7..c02ae78 100644 --- a/src/ZeroSerializerGenerator.cs +++ b/src/ZeroSerializerGenerator.cs @@ -357,7 +357,7 @@ private static TypeGenerationModel CreateGenerationModel( generationModel.IsValid = false; executionContext.ReportDiagnostic(Diagnostic.Create( UnsupportedSerializableField, - serializableProperty.Locations.IsDefaultOrEmpty ? null : serializableProperty.Locations[0], + GetPropertyTypeLocation(serializableProperty), serializableProperty.Name, serializableProperty.Type.ToDisplayString())); continue; @@ -368,7 +368,7 @@ private static TypeGenerationModel CreateGenerationModel( generationModel.IsValid = false; executionContext.ReportDiagnostic(Diagnostic.Create( InvalidBlittableArrayElement, - serializableProperty.Locations.IsDefaultOrEmpty ? null : serializableProperty.Locations[0], + GetPropertyTypeLocation(serializableProperty), serializableProperty.Name)); continue; } @@ -790,6 +790,19 @@ private static void ReportBlittableCompatibleNestedStructDiagnostics( return declaredType.Locations.IsDefaultOrEmpty ? null : declaredType.Locations[0]; } + private static Location? GetPropertyTypeLocation(IPropertySymbol propertySymbol) + { + foreach (SyntaxReference declaringSyntaxReference in propertySymbol.DeclaringSyntaxReferences) + { + if (declaringSyntaxReference.GetSyntax() is PropertyDeclarationSyntax propertyDeclaration) + { + return propertyDeclaration.Type.GetLocation(); + } + } + + return propertySymbol.Locations.IsDefaultOrEmpty ? null : propertySymbol.Locations[0]; + } + private static bool TryGetPrimitiveByteCount(ITypeSymbol candidateType, out int byteCount) { switch (candidateType.SpecialType) diff --git a/tests/Diagnostics/DiagnosticTests.cs b/tests/Diagnostics/DiagnosticTests.cs index 54f16fd..1c93cfc 100644 --- a/tests/Diagnostics/DiagnosticTests.cs +++ b/tests/Diagnostics/DiagnosticTests.cs @@ -250,8 +250,8 @@ public struct PackedValue [ZeroSerializer] public class Container { - public PackedValue {|#0:Value|} { get; set; } - public PackedValue? {|#1:OptionalValue|} { get; set; } + public {|#0:PackedValue|} Value { get; set; } + public {|#1:PackedValue?|} OptionalValue { get; set; } } "; @@ -280,7 +280,7 @@ public class UnmarkedClass [ZeroSerializer] public class Container { - public UnmarkedClass {|#0:Value|} { get; set; } + public {|#0:UnmarkedClass|} Value { get; set; } } "; @@ -335,7 +335,7 @@ public struct PackedValue [ZeroSerializer] public class Container { - public PackedValue[] {|#0:Values|} { get; set; } + public {|#0:PackedValue[]|} Values { get; set; } } "; @@ -389,7 +389,7 @@ public struct PackedValue [ZeroSerializer] public class InvalidType { - public PackedValue {|#0:Value|} { get; set; } + public {|#0:PackedValue|} Value { get; set; } } [ZeroSerializer]