From 692412562de8beda5fa2d639be8372758ec30664 Mon Sep 17 00:00:00 2001 From: Alberto Vilches <194074+avilches@users.noreply.github.com> Date: Fri, 8 Dec 2023 01:17:09 +0100 Subject: [PATCH] C#: Fix to allow usage of [MustBeVariant] in generic typed attributes --- .../MustBeVariantSamples.cs | 494 ++++++++++++++- .../CSharpAnalyzerVerifier.cs | 7 +- .../Godot.SourceGenerators.Tests.csproj | 1 + .../TestData/Sources/MustBeVariant.GD0301.cs | 582 ++++++++++++++++++ .../Godot.SourceGenerators.csproj | 2 +- .../MustBeVariantAnalyzer.cs | 16 +- 6 files changed, 1093 insertions(+), 9 deletions(-) diff --git a/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Sample/MustBeVariantSamples.cs b/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Sample/MustBeVariantSamples.cs index 1e06091e809..f19da77be6d 100644 --- a/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Sample/MustBeVariantSamples.cs +++ b/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Sample/MustBeVariantSamples.cs @@ -131,8 +131,253 @@ public class ClassWithGenericVariant<[MustBeVariant] T> public class MustBeVariantAnnotatedMethods { + [GenericTypeAttribute()] + public void MethodWithAttributeBool() + { + } + + [GenericTypeAttribute()] + public void MethodWithAttributeChar() + { + } + + [GenericTypeAttribute()] + public void MethodWithAttributeSByte() + { + } + + [GenericTypeAttribute()] + public void MethodWithAttributeByte() + { + } + + [GenericTypeAttribute()] + public void MethodWithAttributeInt16() + { + } + + [GenericTypeAttribute()] + public void MethodWithAttributeUInt16() + { + } + + [GenericTypeAttribute()] + public void MethodWithAttributeInt32() + { + } + + [GenericTypeAttribute()] + public void MethodWithAttributeUInt32() + { + } + + [GenericTypeAttribute()] + public void MethodWithAttributeInt64() + { + } + + [GenericTypeAttribute()] + public void MethodWithAttributeUInt64() + { + } + + [GenericTypeAttribute()] + public void MethodWithAttributeSingle() + { + } + + [GenericTypeAttribute()] + public void MethodWithAttributeDouble() + { + } + [GenericTypeAttribute()] - public void MethodWithAttributeOk() + public void MethodWithAttributeString() + { + } + + [GenericTypeAttribute()] + public void MethodWithAttributeVector2() + { + } + + [GenericTypeAttribute()] + public void MethodWithAttributeVector2I() + { + } + + [GenericTypeAttribute()] + public void MethodWithAttributeRect2() + { + } + + [GenericTypeAttribute()] + public void MethodWithAttributeRect2I() + { + } + + [GenericTypeAttribute()] + public void MethodWithAttributeTransform2D() + { + } + + [GenericTypeAttribute()] + public void MethodWithAttributeVector3() + { + } + + [GenericTypeAttribute()] + public void MethodWithAttributeVector3I() + { + } + + [GenericTypeAttribute()] + public void MethodWithAttributeVector4() + { + } + + [GenericTypeAttribute()] + public void MethodWithAttributeVector4I() + { + } + + [GenericTypeAttribute()] + public void MethodWithAttributeBasis() + { + } + + [GenericTypeAttribute()] + public void MethodWithAttributeQuaternion() + { + } + + [GenericTypeAttribute()] + public void MethodWithAttributeTransform3D() + { + } + + [GenericTypeAttribute()] + public void MethodWithAttributeProjection() + { + } + + [GenericTypeAttribute()] + public void MethodWithAttributeAabb() + { + } + + [GenericTypeAttribute()] + public void MethodWithAttributeColor() + { + } + + [GenericTypeAttribute()] + public void MethodWithAttributePlane() + { + } + + [GenericTypeAttribute()] + public void MethodWithAttributeCallable() + { + } + + [GenericTypeAttribute()] + public void MethodWithAttributeSignal() + { + } + + [GenericTypeAttribute()] + public void MethodWithAttributeGodotObject() + { + } + + [GenericTypeAttribute()] + public void MethodWithAttributeStringName() + { + } + + [GenericTypeAttribute()] + public void MethodWithAttributeNodePath() + { + } + + [GenericTypeAttribute()] + public void MethodWithAttributeRid() + { + } + + [GenericTypeAttribute()] + public void MethodWithAttributeDictionary() + { + } + + [GenericTypeAttribute()] + public void MethodWithAttributeArray() + { + } + + [GenericTypeAttribute()] + public void MethodWithAttributeByteArray() + { + } + + [GenericTypeAttribute()] + public void MethodWithAttributeInt32Array() + { + } + + [GenericTypeAttribute()] + public void MethodWithAttributeInt64Array() + { + } + + [GenericTypeAttribute()] + public void MethodWithAttributeSingleArray() + { + } + + [GenericTypeAttribute()] + public void MethodWithAttributeDoubleArray() + { + } + + [GenericTypeAttribute()] + public void MethodWithAttributeStringArray() + { + } + + [GenericTypeAttribute()] + public void MethodWithAttributeVector2Array() + { + } + + [GenericTypeAttribute()] + public void MethodWithAttributeVector3Array() + { + } + + [GenericTypeAttribute()] + public void MethodWithAttributeColorArray() + { + } + + [GenericTypeAttribute()] + public void MethodWithAttributeGodotObjectArray() + { + } + + [GenericTypeAttribute()] + public void MethodWithAttributeStringNameArray() + { + } + + [GenericTypeAttribute()] + public void MethodWithAttributeNodePathArray() + { + } + + [GenericTypeAttribute()] + public void MethodWithAttributeRidArray() { } @@ -145,8 +390,253 @@ public class MustBeVariantAnnotatedMethods */ } +[GenericTypeAttribute()] +public class ClassVariantAnnotatedBool +{ +} + +[GenericTypeAttribute()] +public class ClassVariantAnnotatedChar +{ +} + +[GenericTypeAttribute()] +public class ClassVariantAnnotatedSByte +{ +} + +[GenericTypeAttribute()] +public class ClassVariantAnnotatedByte +{ +} + +[GenericTypeAttribute()] +public class ClassVariantAnnotatedInt16 +{ +} + +[GenericTypeAttribute()] +public class ClassVariantAnnotatedUInt16 +{ +} + +[GenericTypeAttribute()] +public class ClassVariantAnnotatedInt32 +{ +} + +[GenericTypeAttribute()] +public class ClassVariantAnnotatedUInt32 +{ +} + +[GenericTypeAttribute()] +public class ClassVariantAnnotatedInt64 +{ +} + +[GenericTypeAttribute()] +public class ClassVariantAnnotatedUInt64 +{ +} + +[GenericTypeAttribute()] +public class ClassVariantAnnotatedSingle +{ +} + +[GenericTypeAttribute()] +public class ClassVariantAnnotatedDouble +{ +} + [GenericTypeAttribute()] -public class ClassVariantAnnotated +public class ClassVariantAnnotatedString +{ +} + +[GenericTypeAttribute()] +public class ClassVariantAnnotatedVector2 +{ +} + +[GenericTypeAttribute()] +public class ClassVariantAnnotatedVector2I +{ +} + +[GenericTypeAttribute()] +public class ClassVariantAnnotatedRect2 +{ +} + +[GenericTypeAttribute()] +public class ClassVariantAnnotatedRect2I +{ +} + +[GenericTypeAttribute()] +public class ClassVariantAnnotatedTransform2D +{ +} + +[GenericTypeAttribute()] +public class ClassVariantAnnotatedVector3 +{ +} + +[GenericTypeAttribute()] +public class ClassVariantAnnotatedVector3I +{ +} + +[GenericTypeAttribute()] +public class ClassVariantAnnotatedVector4 +{ +} + +[GenericTypeAttribute()] +public class ClassVariantAnnotatedVector4I +{ +} + +[GenericTypeAttribute()] +public class ClassVariantAnnotatedBasis +{ +} + +[GenericTypeAttribute()] +public class ClassVariantAnnotatedQuaternion +{ +} + +[GenericTypeAttribute()] +public class ClassVariantAnnotatedTransform3D +{ +} + +[GenericTypeAttribute()] +public class ClassVariantAnnotatedProjection +{ +} + +[GenericTypeAttribute()] +public class ClassVariantAnnotatedAabb +{ +} + +[GenericTypeAttribute()] +public class ClassVariantAnnotatedColor +{ +} + +[GenericTypeAttribute()] +public class ClassVariantAnnotatedPlane +{ +} + +[GenericTypeAttribute()] +public class ClassVariantAnnotatedCallable +{ +} + +[GenericTypeAttribute()] +public class ClassVariantAnnotatedSignal +{ +} + +[GenericTypeAttribute()] +public class ClassVariantAnnotatedGodotObject +{ +} + +[GenericTypeAttribute()] +public class ClassVariantAnnotatedStringName +{ +} + +[GenericTypeAttribute()] +public class ClassVariantAnnotatedNodePath +{ +} + +[GenericTypeAttribute()] +public class ClassVariantAnnotatedRid +{ +} + +[GenericTypeAttribute()] +public class ClassVariantAnnotatedDictionary +{ +} + +[GenericTypeAttribute()] +public class ClassVariantAnnotatedArray +{ +} + +[GenericTypeAttribute()] +public class ClassVariantAnnotatedByteArray +{ +} + +[GenericTypeAttribute()] +public class ClassVariantAnnotatedInt32Array +{ +} + +[GenericTypeAttribute()] +public class ClassVariantAnnotatedInt64Array +{ +} + +[GenericTypeAttribute()] +public class ClassVariantAnnotatedSingleArray +{ +} + +[GenericTypeAttribute()] +public class ClassVariantAnnotatedDoubleArray +{ +} + +[GenericTypeAttribute()] +public class ClassVariantAnnotatedStringArray +{ +} + +[GenericTypeAttribute()] +public class ClassVariantAnnotatedVector2Array +{ +} + +[GenericTypeAttribute()] +public class ClassVariantAnnotatedVector3Array +{ +} + +[GenericTypeAttribute()] +public class ClassVariantAnnotatedColorArray +{ +} + +[GenericTypeAttribute()] +public class ClassVariantAnnotatedGodotObjectArray +{ +} + +[GenericTypeAttribute()] +public class ClassVariantAnnotatedStringNameArray +{ +} + +[GenericTypeAttribute()] +public class ClassVariantAnnotatedNodePathArray +{ +} + +[GenericTypeAttribute()] +public class ClassVariantAnnotatedRidArray { } diff --git a/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Tests/CSharpAnalyzerVerifier.cs b/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Tests/CSharpAnalyzerVerifier.cs index e3e7373b2e1..253889296b1 100644 --- a/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Tests/CSharpAnalyzerVerifier.cs +++ b/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Tests/CSharpAnalyzerVerifier.cs @@ -3,6 +3,7 @@ using System.IO; using System.Linq; using System.Threading.Tasks; using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.CSharp.Testing; using Microsoft.CodeAnalysis.Diagnostics; using Microsoft.CodeAnalysis.Testing; @@ -12,8 +13,10 @@ using Microsoft.CodeAnalysis.Text; namespace Godot.SourceGenerators.Tests; public static class CSharpAnalyzerVerifier -where TAnalyzer : DiagnosticAnalyzer, new() + where TAnalyzer : DiagnosticAnalyzer, new() { + public const LanguageVersion LangVersion = LanguageVersion.CSharp11; + public class Test : CSharpAnalyzerTest { public Test() @@ -24,7 +27,7 @@ where TAnalyzer : DiagnosticAnalyzer, new() { Project project = solution.GetProject(projectId)!.AddMetadataReference(Constants.GodotSharpAssembly - .CreateMetadataReference()); + .CreateMetadataReference()).WithParseOptions(new CSharpParseOptions(LangVersion)); return project.Solution; }); diff --git a/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Tests/Godot.SourceGenerators.Tests.csproj b/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Tests/Godot.SourceGenerators.Tests.csproj index 13e54a543f1..32d7ca74867 100644 --- a/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Tests/Godot.SourceGenerators.Tests.csproj +++ b/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Tests/Godot.SourceGenerators.Tests.csproj @@ -15,6 +15,7 @@ + diff --git a/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Tests/TestData/Sources/MustBeVariant.GD0301.cs b/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Tests/TestData/Sources/MustBeVariant.GD0301.cs index 031039cba1f..462da31d668 100644 --- a/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Tests/TestData/Sources/MustBeVariant.GD0301.cs +++ b/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Tests/TestData/Sources/MustBeVariant.GD0301.cs @@ -10,6 +10,7 @@ public class MustBeVariantGD0301 // This raises a GD0301 diagnostic error: object is not Variant (and Method requires a variant generic type). Method<{|GD0301:object|}>(); } + public void MethodCallsOk() { // All these calls are valid because they are Variant types. @@ -68,4 +69,585 @@ public class MustBeVariantGD0301 public void Method<[MustBeVariant] T>() { } + + public void MustBeVariantClasses() + { + new ClassWithGenericVariant(); + new ClassWithGenericVariant(); + new ClassWithGenericVariant(); + new ClassWithGenericVariant(); + new ClassWithGenericVariant(); + new ClassWithGenericVariant(); + new ClassWithGenericVariant(); + new ClassWithGenericVariant(); + new ClassWithGenericVariant(); + new ClassWithGenericVariant(); + new ClassWithGenericVariant(); + new ClassWithGenericVariant(); + new ClassWithGenericVariant(); + new ClassWithGenericVariant(); + new ClassWithGenericVariant(); + new ClassWithGenericVariant(); + new ClassWithGenericVariant(); + new ClassWithGenericVariant(); + new ClassWithGenericVariant(); + new ClassWithGenericVariant(); + new ClassWithGenericVariant(); + new ClassWithGenericVariant(); + new ClassWithGenericVariant(); + new ClassWithGenericVariant(); + new ClassWithGenericVariant(); + new ClassWithGenericVariant(); + new ClassWithGenericVariant(); + new ClassWithGenericVariant(); + new ClassWithGenericVariant(); + new ClassWithGenericVariant(); + new ClassWithGenericVariant(); + new ClassWithGenericVariant(); + new ClassWithGenericVariant(); + new ClassWithGenericVariant(); + new ClassWithGenericVariant(); + new ClassWithGenericVariant(); + new ClassWithGenericVariant(); + new ClassWithGenericVariant(); + new ClassWithGenericVariant(); + new ClassWithGenericVariant(); + new ClassWithGenericVariant(); + new ClassWithGenericVariant(); + new ClassWithGenericVariant(); + new ClassWithGenericVariant(); + new ClassWithGenericVariant(); + new ClassWithGenericVariant(); + new ClassWithGenericVariant(); + new ClassWithGenericVariant(); + new ClassWithGenericVariant(); + new ClassWithGenericVariant(); + + // This class fails because generic type is not Variant-compatible. + new ClassWithGenericVariant<{|GD0301:object|}>(); + } +} + +public class ClassWithGenericVariant<[MustBeVariant] T> +{ +} + +public class MustBeVariantAnnotatedMethods +{ + [GenericTypeAttribute()] + public void MethodWithAttributeBool() + { + } + + [GenericTypeAttribute()] + public void MethodWithAttributeChar() + { + } + + [GenericTypeAttribute()] + public void MethodWithAttributeSByte() + { + } + + [GenericTypeAttribute()] + public void MethodWithAttributeByte() + { + } + + [GenericTypeAttribute()] + public void MethodWithAttributeInt16() + { + } + + [GenericTypeAttribute()] + public void MethodWithAttributeUInt16() + { + } + + [GenericTypeAttribute()] + public void MethodWithAttributeInt32() + { + } + + [GenericTypeAttribute()] + public void MethodWithAttributeUInt32() + { + } + + [GenericTypeAttribute()] + public void MethodWithAttributeInt64() + { + } + + [GenericTypeAttribute()] + public void MethodWithAttributeUInt64() + { + } + + [GenericTypeAttribute()] + public void MethodWithAttributeSingle() + { + } + + [GenericTypeAttribute()] + public void MethodWithAttributeDouble() + { + } + + [GenericTypeAttribute()] + public void MethodWithAttributeString() + { + } + + [GenericTypeAttribute()] + public void MethodWithAttributeVector2() + { + } + + [GenericTypeAttribute()] + public void MethodWithAttributeVector2I() + { + } + + [GenericTypeAttribute()] + public void MethodWithAttributeRect2() + { + } + + [GenericTypeAttribute()] + public void MethodWithAttributeRect2I() + { + } + + [GenericTypeAttribute()] + public void MethodWithAttributeTransform2D() + { + } + + [GenericTypeAttribute()] + public void MethodWithAttributeVector3() + { + } + + [GenericTypeAttribute()] + public void MethodWithAttributeVector3I() + { + } + + [GenericTypeAttribute()] + public void MethodWithAttributeVector4() + { + } + + [GenericTypeAttribute()] + public void MethodWithAttributeVector4I() + { + } + + [GenericTypeAttribute()] + public void MethodWithAttributeBasis() + { + } + + [GenericTypeAttribute()] + public void MethodWithAttributeQuaternion() + { + } + + [GenericTypeAttribute()] + public void MethodWithAttributeTransform3D() + { + } + + [GenericTypeAttribute()] + public void MethodWithAttributeProjection() + { + } + + [GenericTypeAttribute()] + public void MethodWithAttributeAabb() + { + } + + [GenericTypeAttribute()] + public void MethodWithAttributeColor() + { + } + + [GenericTypeAttribute()] + public void MethodWithAttributePlane() + { + } + + [GenericTypeAttribute()] + public void MethodWithAttributeCallable() + { + } + + [GenericTypeAttribute()] + public void MethodWithAttributeSignal() + { + } + + [GenericTypeAttribute()] + public void MethodWithAttributeGodotObject() + { + } + + [GenericTypeAttribute()] + public void MethodWithAttributeStringName() + { + } + + [GenericTypeAttribute()] + public void MethodWithAttributeNodePath() + { + } + + [GenericTypeAttribute()] + public void MethodWithAttributeRid() + { + } + + [GenericTypeAttribute()] + public void MethodWithAttributeDictionary() + { + } + + [GenericTypeAttribute()] + public void MethodWithAttributeArray() + { + } + + [GenericTypeAttribute()] + public void MethodWithAttributeByteArray() + { + } + + [GenericTypeAttribute()] + public void MethodWithAttributeInt32Array() + { + } + + [GenericTypeAttribute()] + public void MethodWithAttributeInt64Array() + { + } + + [GenericTypeAttribute()] + public void MethodWithAttributeSingleArray() + { + } + + [GenericTypeAttribute()] + public void MethodWithAttributeDoubleArray() + { + } + + [GenericTypeAttribute()] + public void MethodWithAttributeStringArray() + { + } + + [GenericTypeAttribute()] + public void MethodWithAttributeVector2Array() + { + } + + [GenericTypeAttribute()] + public void MethodWithAttributeVector3Array() + { + } + + [GenericTypeAttribute()] + public void MethodWithAttributeColorArray() + { + } + + [GenericTypeAttribute()] + public void MethodWithAttributeGodotObjectArray() + { + } + + [GenericTypeAttribute()] + public void MethodWithAttributeStringNameArray() + { + } + + [GenericTypeAttribute()] + public void MethodWithAttributeNodePathArray() + { + } + + [GenericTypeAttribute()] + public void MethodWithAttributeRidArray() + { + } + + // This method definition fails because generic type is not Variant-compatible. + [GenericTypeAttribute<{|GD0301:object|}>()] + public void MethodWithWrongAttribute() + { + } +} + +[GenericTypeAttribute()] +public class ClassVariantAnnotatedBool +{ +} + +[GenericTypeAttribute()] +public class ClassVariantAnnotatedChar +{ +} + +[GenericTypeAttribute()] +public class ClassVariantAnnotatedSByte +{ +} + +[GenericTypeAttribute()] +public class ClassVariantAnnotatedByte +{ +} + +[GenericTypeAttribute()] +public class ClassVariantAnnotatedInt16 +{ +} + +[GenericTypeAttribute()] +public class ClassVariantAnnotatedUInt16 +{ +} + +[GenericTypeAttribute()] +public class ClassVariantAnnotatedInt32 +{ +} + +[GenericTypeAttribute()] +public class ClassVariantAnnotatedUInt32 +{ +} + +[GenericTypeAttribute()] +public class ClassVariantAnnotatedInt64 +{ +} + +[GenericTypeAttribute()] +public class ClassVariantAnnotatedUInt64 +{ +} + +[GenericTypeAttribute()] +public class ClassVariantAnnotatedSingle +{ +} + +[GenericTypeAttribute()] +public class ClassVariantAnnotatedDouble +{ +} + +[GenericTypeAttribute()] +public class ClassVariantAnnotatedString +{ +} + +[GenericTypeAttribute()] +public class ClassVariantAnnotatedVector2 +{ +} + +[GenericTypeAttribute()] +public class ClassVariantAnnotatedVector2I +{ +} + +[GenericTypeAttribute()] +public class ClassVariantAnnotatedRect2 +{ +} + +[GenericTypeAttribute()] +public class ClassVariantAnnotatedRect2I +{ +} + +[GenericTypeAttribute()] +public class ClassVariantAnnotatedTransform2D +{ +} + +[GenericTypeAttribute()] +public class ClassVariantAnnotatedVector3 +{ +} + +[GenericTypeAttribute()] +public class ClassVariantAnnotatedVector3I +{ +} + +[GenericTypeAttribute()] +public class ClassVariantAnnotatedVector4 +{ +} + +[GenericTypeAttribute()] +public class ClassVariantAnnotatedVector4I +{ +} + +[GenericTypeAttribute()] +public class ClassVariantAnnotatedBasis +{ +} + +[GenericTypeAttribute()] +public class ClassVariantAnnotatedQuaternion +{ +} + +[GenericTypeAttribute()] +public class ClassVariantAnnotatedTransform3D +{ +} + +[GenericTypeAttribute()] +public class ClassVariantAnnotatedProjection +{ +} + +[GenericTypeAttribute()] +public class ClassVariantAnnotatedAabb +{ +} + +[GenericTypeAttribute()] +public class ClassVariantAnnotatedColor +{ +} + +[GenericTypeAttribute()] +public class ClassVariantAnnotatedPlane +{ +} + +[GenericTypeAttribute()] +public class ClassVariantAnnotatedCallable +{ +} + +[GenericTypeAttribute()] +public class ClassVariantAnnotatedSignal +{ +} + +[GenericTypeAttribute()] +public class ClassVariantAnnotatedGodotObject +{ +} + +[GenericTypeAttribute()] +public class ClassVariantAnnotatedStringName +{ +} + +[GenericTypeAttribute()] +public class ClassVariantAnnotatedNodePath +{ +} + +[GenericTypeAttribute()] +public class ClassVariantAnnotatedRid +{ +} + +[GenericTypeAttribute()] +public class ClassVariantAnnotatedDictionary +{ +} + +[GenericTypeAttribute()] +public class ClassVariantAnnotatedArray +{ +} + +[GenericTypeAttribute()] +public class ClassVariantAnnotatedByteArray +{ +} + +[GenericTypeAttribute()] +public class ClassVariantAnnotatedInt32Array +{ +} + +[GenericTypeAttribute()] +public class ClassVariantAnnotatedInt64Array +{ +} + +[GenericTypeAttribute()] +public class ClassVariantAnnotatedSingleArray +{ +} + +[GenericTypeAttribute()] +public class ClassVariantAnnotatedDoubleArray +{ +} + +[GenericTypeAttribute()] +public class ClassVariantAnnotatedStringArray +{ +} + +[GenericTypeAttribute()] +public class ClassVariantAnnotatedVector2Array +{ +} + +[GenericTypeAttribute()] +public class ClassVariantAnnotatedVector3Array +{ +} + +[GenericTypeAttribute()] +public class ClassVariantAnnotatedColorArray +{ +} + +[GenericTypeAttribute()] +public class ClassVariantAnnotatedGodotObjectArray +{ +} + +[GenericTypeAttribute()] +public class ClassVariantAnnotatedStringNameArray +{ +} + +[GenericTypeAttribute()] +public class ClassVariantAnnotatedNodePathArray +{ +} + +[GenericTypeAttribute()] +public class ClassVariantAnnotatedRidArray +{ +} + +// This class definition fails because generic type is not Variant-compatible. +[GenericTypeAttribute<{|GD0301:object|}>()] +public class ClassNonVariantAnnotated +{ +} + +[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true)] +public class GenericTypeAttribute<[MustBeVariant] T> : Attribute +{ } diff --git a/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/Godot.SourceGenerators.csproj b/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/Godot.SourceGenerators.csproj index 7d2395ba617..b1578fb5f44 100644 --- a/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/Godot.SourceGenerators.csproj +++ b/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/Godot.SourceGenerators.csproj @@ -1,7 +1,7 @@ netstandard2.0 - 9.0 + 10 enable diff --git a/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/MustBeVariantAnalyzer.cs b/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/MustBeVariantAnalyzer.cs index 2a9758516c6..91a1eadf359 100644 --- a/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/MustBeVariantAnalyzer.cs +++ b/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/MustBeVariantAnalyzer.cs @@ -109,11 +109,19 @@ namespace Godot.SourceGenerators /// if the type must be variant and must be analyzed. private bool ShouldCheckTypeArgument(SyntaxNodeAnalysisContext context, SyntaxNode parentSyntax, ISymbol parentSymbol, TypeSyntax typeArgumentSyntax, ITypeSymbol typeArgumentSymbol, int typeArgumentIndex) { - var typeParamSymbol = parentSymbol switch + ITypeParameterSymbol? typeParamSymbol = parentSymbol switch { - IMethodSymbol methodSymbol => methodSymbol.TypeParameters[typeArgumentIndex], - INamedTypeSymbol typeSymbol => typeSymbol.TypeParameters[typeArgumentIndex], - _ => null, + IMethodSymbol methodSymbol when parentSyntax.Parent is AttributeSyntax && + methodSymbol.ContainingType.TypeParameters.Length > 0 + => methodSymbol.ContainingType.TypeParameters[typeArgumentIndex], + + IMethodSymbol { TypeParameters.Length: > 0 } methodSymbol + => methodSymbol.TypeParameters[typeArgumentIndex], + + INamedTypeSymbol { TypeParameters.Length: > 0 } typeSymbol + => typeSymbol.TypeParameters[typeArgumentIndex], + _ + => null }; if (typeParamSymbol == null)