From 0a0c730805af95529941207eb6d252f75ac3260b Mon Sep 17 00:00:00 2001 From: Raul Santos Date: Fri, 23 Dec 2022 02:44:30 +0100 Subject: C#: Annotate API with MustBeVariant - Add MustBeVariant attribute to generic parameters that are used in a Variant context --- .../GodotSharp/Core/Callable.generics.cs | 38 +++++++++++----------- .../GodotSharp/Core/NativeInterop/VariantUtils.cs | 8 ++--- .../glue/GodotSharp/GodotSharp/Core/Variant.cs | 8 ++--- 3 files changed, 27 insertions(+), 27 deletions(-) diff --git a/modules/mono/glue/GodotSharp/GodotSharp/Core/Callable.generics.cs b/modules/mono/glue/GodotSharp/GodotSharp/Core/Callable.generics.cs index ff385da1c9..3005582bea 100644 --- a/modules/mono/glue/GodotSharp/GodotSharp/Core/Callable.generics.cs +++ b/modules/mono/glue/GodotSharp/GodotSharp/Core/Callable.generics.cs @@ -45,7 +45,7 @@ public readonly partial struct Callable } /// - public static unsafe Callable From( + public static unsafe Callable From<[MustBeVariant] T0>( Action action ) { @@ -64,7 +64,7 @@ public readonly partial struct Callable } /// - public static unsafe Callable From( + public static unsafe Callable From<[MustBeVariant] T0, [MustBeVariant] T1>( Action action ) { @@ -84,7 +84,7 @@ public readonly partial struct Callable } /// - public static unsafe Callable From( + public static unsafe Callable From<[MustBeVariant] T0, [MustBeVariant] T1, [MustBeVariant] T2>( Action action ) { @@ -105,7 +105,7 @@ public readonly partial struct Callable } /// - public static unsafe Callable From( + public static unsafe Callable From<[MustBeVariant] T0, [MustBeVariant] T1, [MustBeVariant] T2, [MustBeVariant] T3>( Action action ) { @@ -127,7 +127,7 @@ public readonly partial struct Callable } /// - public static unsafe Callable From( + public static unsafe Callable From<[MustBeVariant] T0, [MustBeVariant] T1, [MustBeVariant] T2, [MustBeVariant] T3, [MustBeVariant] T4>( Action action ) { @@ -150,7 +150,7 @@ public readonly partial struct Callable } /// - public static unsafe Callable From( + public static unsafe Callable From<[MustBeVariant] T0, [MustBeVariant] T1, [MustBeVariant] T2, [MustBeVariant] T3, [MustBeVariant] T4, [MustBeVariant] T5>( Action action ) { @@ -174,7 +174,7 @@ public readonly partial struct Callable } /// - public static unsafe Callable From( + public static unsafe Callable From<[MustBeVariant] T0, [MustBeVariant] T1, [MustBeVariant] T2, [MustBeVariant] T3, [MustBeVariant] T4, [MustBeVariant] T5, [MustBeVariant] T6>( Action action ) { @@ -199,7 +199,7 @@ public readonly partial struct Callable } /// - public static unsafe Callable From( + public static unsafe Callable From<[MustBeVariant] T0, [MustBeVariant] T1, [MustBeVariant] T2, [MustBeVariant] T3, [MustBeVariant] T4, [MustBeVariant] T5, [MustBeVariant] T6, [MustBeVariant] T7>( Action action ) { @@ -225,7 +225,7 @@ public readonly partial struct Callable } /// - public static unsafe Callable From( + public static unsafe Callable From<[MustBeVariant] T0, [MustBeVariant] T1, [MustBeVariant] T2, [MustBeVariant] T3, [MustBeVariant] T4, [MustBeVariant] T5, [MustBeVariant] T6, [MustBeVariant] T7, [MustBeVariant] T8>( Action action ) { @@ -255,7 +255,7 @@ public readonly partial struct Callable /// Constructs a new for the given . /// /// Action method that will be called. - public static unsafe Callable From( + public static unsafe Callable From<[MustBeVariant] TResult>( Func func ) { @@ -272,7 +272,7 @@ public readonly partial struct Callable } /// - public static unsafe Callable From( + public static unsafe Callable From<[MustBeVariant] T0, [MustBeVariant] TResult>( Func func ) { @@ -291,7 +291,7 @@ public readonly partial struct Callable } /// - public static unsafe Callable From( + public static unsafe Callable From<[MustBeVariant] T0, [MustBeVariant] T1, [MustBeVariant] TResult>( Func func ) { @@ -311,7 +311,7 @@ public readonly partial struct Callable } /// - public static unsafe Callable From( + public static unsafe Callable From<[MustBeVariant] T0, [MustBeVariant] T1, [MustBeVariant] T2, [MustBeVariant] TResult>( Func func ) { @@ -332,7 +332,7 @@ public readonly partial struct Callable } /// - public static unsafe Callable From( + public static unsafe Callable From<[MustBeVariant] T0, [MustBeVariant] T1, [MustBeVariant] T2, [MustBeVariant] T3, [MustBeVariant] TResult>( Func func ) { @@ -354,7 +354,7 @@ public readonly partial struct Callable } /// - public static unsafe Callable From( + public static unsafe Callable From<[MustBeVariant] T0, [MustBeVariant] T1, [MustBeVariant] T2, [MustBeVariant] T3, [MustBeVariant] T4, [MustBeVariant] TResult>( Func func ) { @@ -377,7 +377,7 @@ public readonly partial struct Callable } /// - public static unsafe Callable From( + public static unsafe Callable From<[MustBeVariant] T0, [MustBeVariant] T1, [MustBeVariant] T2, [MustBeVariant] T3, [MustBeVariant] T4, [MustBeVariant] T5, [MustBeVariant] TResult>( Func func ) { @@ -401,7 +401,7 @@ public readonly partial struct Callable } /// - public static unsafe Callable From( + public static unsafe Callable From<[MustBeVariant] T0, [MustBeVariant] T1, [MustBeVariant] T2, [MustBeVariant] T3, [MustBeVariant] T4, [MustBeVariant] T5, [MustBeVariant] T6, [MustBeVariant] TResult>( Func func ) { @@ -426,7 +426,7 @@ public readonly partial struct Callable } /// - public static unsafe Callable From( + public static unsafe Callable From<[MustBeVariant] T0, [MustBeVariant] T1, [MustBeVariant] T2, [MustBeVariant] T3, [MustBeVariant] T4, [MustBeVariant] T5, [MustBeVariant] T6, [MustBeVariant] T7, [MustBeVariant] TResult>( Func func ) { @@ -452,7 +452,7 @@ public readonly partial struct Callable } /// - public static unsafe Callable From( + public static unsafe Callable From<[MustBeVariant] T0, [MustBeVariant] T1, [MustBeVariant] T2, [MustBeVariant] T3, [MustBeVariant] T4, [MustBeVariant] T5, [MustBeVariant] T6, [MustBeVariant] T7, [MustBeVariant] T8, [MustBeVariant] TResult>( Func func ) { diff --git a/modules/mono/glue/GodotSharp/GodotSharp/Core/NativeInterop/VariantUtils.cs b/modules/mono/glue/GodotSharp/GodotSharp/Core/NativeInterop/VariantUtils.cs index 9c9258dd9e..bb306ddaea 100644 --- a/modules/mono/glue/GodotSharp/GodotSharp/Core/NativeInterop/VariantUtils.cs +++ b/modules/mono/glue/GodotSharp/GodotSharp/Core/NativeInterop/VariantUtils.cs @@ -260,7 +260,7 @@ namespace Godot.NativeInterop => from != null ? CreateFromArray((godot_array)from.NativeValue) : default; [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static godot_variant CreateFromArray(Array? from) + public static godot_variant CreateFromArray<[MustBeVariant] T>(Array? from) => from != null ? CreateFromArray((godot_array)((Collections.Array)from).NativeValue) : default; public static godot_variant CreateFromDictionary(godot_dictionary from) @@ -274,7 +274,7 @@ namespace Godot.NativeInterop => from != null ? CreateFromDictionary((godot_dictionary)from.NativeValue) : default; [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static godot_variant CreateFromDictionary(Dictionary? from) + public static godot_variant CreateFromDictionary<[MustBeVariant] TKey, [MustBeVariant] TValue>(Dictionary? from) => from != null ? CreateFromDictionary((godot_dictionary)((Dictionary)from).NativeValue) : default; public static godot_variant CreateFromStringName(godot_string_name from) @@ -526,7 +526,7 @@ namespace Godot.NativeInterop => Collections.Array.CreateTakingOwnershipOfDisposableValue(ConvertToNativeArray(p_var)); [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static Array ConvertToArray(in godot_variant p_var) + public static Array ConvertToArray<[MustBeVariant] T>(in godot_variant p_var) => Array.CreateTakingOwnershipOfDisposableValue(ConvertToNativeArray(p_var)); public static godot_dictionary ConvertToNativeDictionary(in godot_variant p_var) @@ -539,7 +539,7 @@ namespace Godot.NativeInterop => Dictionary.CreateTakingOwnershipOfDisposableValue(ConvertToNativeDictionary(p_var)); [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static Dictionary ConvertToDictionary(in godot_variant p_var) + public static Dictionary ConvertToDictionary<[MustBeVariant] TKey, [MustBeVariant] TValue>(in godot_variant p_var) => Dictionary.CreateTakingOwnershipOfDisposableValue(ConvertToNativeDictionary(p_var)); public static byte[] ConvertAsPackedByteArrayToSystemArray(in godot_variant p_var) diff --git a/modules/mono/glue/GodotSharp/GodotSharp/Core/Variant.cs b/modules/mono/glue/GodotSharp/GodotSharp/Core/Variant.cs index da12309217..0ac2e2969c 100644 --- a/modules/mono/glue/GodotSharp/GodotSharp/Core/Variant.cs +++ b/modules/mono/glue/GodotSharp/GodotSharp/Core/Variant.cs @@ -328,11 +328,11 @@ public partial struct Variant : IDisposable VariantUtils.ConvertToSystemArrayOfGodotObject((godot_variant)NativeVar); [MethodImpl(MethodImplOptions.AggressiveInlining)] - public Collections.Dictionary AsGodotDictionary() => + public Collections.Dictionary AsGodotDictionary<[MustBeVariant] TKey, [MustBeVariant] TValue>() => VariantUtils.ConvertToDictionary((godot_variant)NativeVar); [MethodImpl(MethodImplOptions.AggressiveInlining)] - public Collections.Array AsGodotArray() => + public Collections.Array AsGodotArray<[MustBeVariant] T>() => VariantUtils.ConvertToArray((godot_variant)NativeVar); [MethodImpl(MethodImplOptions.AggressiveInlining)] @@ -647,11 +647,11 @@ public partial struct Variant : IDisposable public static Variant CreateFrom(Godot.Object[] from) => from; [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static Variant CreateFrom(Collections.Dictionary from) => + public static Variant CreateFrom<[MustBeVariant] TKey, [MustBeVariant] TValue>(Collections.Dictionary from) => CreateTakingOwnershipOfDisposableValue(VariantUtils.CreateFromDictionary(from)); [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static Variant CreateFrom(Collections.Array from) => + public static Variant CreateFrom<[MustBeVariant] T>(Collections.Array from) => CreateTakingOwnershipOfDisposableValue(VariantUtils.CreateFromArray(from)); [MethodImpl(MethodImplOptions.AggressiveInlining)] -- cgit v1.2.3 From 256632a07e99d94e949d0c190f9a0f3de8e39ed1 Mon Sep 17 00:00:00 2001 From: Raul Santos Date: Fri, 23 Dec 2022 02:47:34 +0100 Subject: C#: Skip documentation syntax in MustBeVariant analyzer --- .../MustBeVariantAnalyzer.cs | 24 ++++++++++++++++++++++ 1 file changed, 24 insertions(+) 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 98ca534c66..2a9758516c 100644 --- a/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/MustBeVariantAnalyzer.cs +++ b/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/MustBeVariantAnalyzer.cs @@ -26,6 +26,10 @@ namespace Godot.SourceGenerators private void AnalyzeNode(SyntaxNodeAnalysisContext context) { + // Ignore syntax inside comments + if (IsInsideDocumentation(context.Node)) + return; + var typeArgListSyntax = (TypeArgumentListSyntax)context.Node; // Method invocation or variable declaration that contained the type arguments @@ -73,6 +77,26 @@ namespace Godot.SourceGenerators } } + /// + /// Check if the syntax node is inside a documentation syntax. + /// + /// Syntax node to check. + /// if the syntax node is inside a documentation syntax. + private bool IsInsideDocumentation(SyntaxNode? syntax) + { + while (syntax != null) + { + if (syntax is DocumentationCommentTriviaSyntax) + { + return true; + } + + syntax = syntax.Parent; + } + + return false; + } + /// /// Check if the given type argument is being used in a type parameter that contains /// the MustBeVariantAttribute; otherwise, we ignore the attribute. -- cgit v1.2.3