From 7d14403632e8a5e38912e295e1ec23b197e87a98 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Thu, 28 Apr 2022 01:43:37 +0200 Subject: [PATCH] Update [AutoConstructor] attribute to ignore generated fields --- .../AutoConstructorGenerator.cs | 2 +- .../AutoConstructorAttributeTests.cs | 31 +++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/src/ComputeSharp.Core.SourceGenerators/AutoConstructorGenerator.cs b/src/ComputeSharp.Core.SourceGenerators/AutoConstructorGenerator.cs index 8a5771e5..c75b86ee 100644 --- a/src/ComputeSharp.Core.SourceGenerators/AutoConstructorGenerator.cs +++ b/src/ComputeSharp.Core.SourceGenerators/AutoConstructorGenerator.cs @@ -59,7 +59,7 @@ public sealed partial class AutoConstructorGenerator : IIncrementalGenerator { return ( from fieldSymbol in structDeclarationSymbol.GetMembers().OfType() - where fieldSymbol is { IsConst: false, IsStatic: false, IsFixedSizeBuffer: false } + where fieldSymbol is { IsConst: false, IsStatic: false, IsFixedSizeBuffer: false, IsImplicitlyDeclared: false } let typeName = fieldSymbol.Type.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat) select new ParameterInfo(typeName, fieldSymbol.Name)).ToImmutableArray(); } diff --git a/tests/ComputeSharp.Tests/AutoConstructorAttributeTests.cs b/tests/ComputeSharp.Tests/AutoConstructorAttributeTests.cs index 469c9e25..d63de33c 100644 --- a/tests/ComputeSharp.Tests/AutoConstructorAttributeTests.cs +++ b/tests/ComputeSharp.Tests/AutoConstructorAttributeTests.cs @@ -54,4 +54,35 @@ public unsafe partial class AutoConstructorAttributeTests Assert.AreEqual(instance.a, a); Assert.AreEqual(instance.b, b); } + + public partial interface IAnotherInterface + { + public int B { get; } + + public void Foo(); + } + + public partial struct AnotherStruct : IAnotherInterface + { + public int a; + public float b; + + int IAnotherInterface.B { get; } + + void IAnotherInterface.Foo() + { + } + } + + [TestMethod] + public void GenerateConstructorWithExplicitInterfaceMembers() + { + int a = 42; + float b = 3.14f; + + AnotherStruct instance = new(a, b); + + Assert.AreEqual(instance.a, a); + Assert.AreEqual(instance.b, b); + } }