Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

C#: Enable nullability for generated & compatibility code #88340

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
128 changes: 61 additions & 67 deletions modules/mono/editor/bindings_generator.cpp

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions modules/mono/editor/bindings_generator.h
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,11 @@ class BindingsGenerator {
*/
String cs_type;

/**
* Value used when returning a default argument. Usually 'default'.
*/
String cs_default;

/**
* Formatting elements:
* %0: input expression of type `in godot_variant`
Expand Down Expand Up @@ -600,8 +605,10 @@ class BindingsGenerator {
TypeInterface() {
static String default_cs_variant_to_managed = "VariantUtils.ConvertTo<%1>(%0)";
static String default_cs_managed_to_variant = "VariantUtils.CreateFrom<%1>(%0)";
static String default_cs_default = "default";
cs_variant_to_managed = default_cs_variant_to_managed;
cs_managed_to_variant = default_cs_managed_to_variant;
cs_default = default_cs_default;
}
};

Expand Down
18 changes: 10 additions & 8 deletions modules/mono/glue/GodotSharp/GodotSharp/Compat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

namespace Godot;

#nullable enable

#pragma warning disable CS1734 // XML comment on 'X' has a paramref tag for 'Y', but there is no parameter by that name.
// TODO: This is currently disabled because of https://github.com/dotnet/roslyn/issues/52904
#pragma warning disable IDE0040 // Add accessibility modifiers.
Expand Down Expand Up @@ -70,7 +72,7 @@ partial class CodeEdit
{
/// <inheritdoc cref="AddCodeCompletionOption(CodeCompletionKind, string, string, Nullable{Color}, Resource, Variant, int)"/>
[EditorBrowsable(EditorBrowsableState.Never)]
public void AddCodeCompletionOption(CodeCompletionKind type, string displayText, string insertText, Nullable<Color> textColor, Resource icon, Nullable<Variant> value)
public void AddCodeCompletionOption(CodeCompletionKind type, string displayText, string insertText, Nullable<Color> textColor, Resource? icon, Nullable<Variant> value)
{
AddCodeCompletionOption(type, displayText, insertText, textColor, icon, value, location: 1024);
}
Expand Down Expand Up @@ -98,7 +100,7 @@ public bool ArrangeNodesButtonHidden

/// <inheritdoc cref="GetMenuHBox()"/>
[EditorBrowsable(EditorBrowsableState.Never)]
public HBoxContainer GetZoomHBox()
public HBoxContainer? GetZoomHBox()
{
return GetMenuHBox();
}
Expand Down Expand Up @@ -134,7 +136,7 @@ partial class ImporterMesh
{
/// <inheritdoc cref="AddSurface(Mesh.PrimitiveType, Godot.Collections.Array, Godot.Collections.Array{Godot.Collections.Array}, Godot.Collections.Dictionary, Material, string, ulong)"/>
[EditorBrowsable(EditorBrowsableState.Never)]
public void AddSurface(Mesh.PrimitiveType primitive, Godot.Collections.Array arrays, Godot.Collections.Array<Godot.Collections.Array> blendShapes, Godot.Collections.Dictionary lods, Material material, string name, uint flags)
public void AddSurface(Mesh.PrimitiveType primitive, Godot.Collections.Array arrays, Godot.Collections.Array<Godot.Collections.Array>? blendShapes, Godot.Collections.Dictionary? lods, Material? material, string name, uint flags)
{
AddSurface(primitive, arrays, blendShapes, lods, material, name, (ulong)flags);
}
Expand Down Expand Up @@ -172,9 +174,9 @@ partial class RenderingDevice
{
/// <inheritdoc cref="DrawListBegin(Rid, InitialAction, FinalAction, InitialAction, FinalAction, Color[], float, uint, Nullable{Rect2}, Godot.Collections.Array{Rid})"/>
[EditorBrowsable(EditorBrowsableState.Never)]
public long DrawListBegin(Rid framebuffer, InitialAction initialColorAction, FinalAction finalColorAction, InitialAction initialDepthAction, FinalAction finalDepthAction, Color[] clearColorValues, float clearDepth, uint clearStencil, Nullable<Rect2> region, Godot.Collections.Array storageTextures)
public long DrawListBegin(Rid framebuffer, InitialAction initialColorAction, FinalAction finalColorAction, InitialAction initialDepthAction, FinalAction finalDepthAction, Color[]? clearColorValues, float clearDepth, uint clearStencil, Nullable<Rect2> region, Godot.Collections.Array? storageTextures)
{
return DrawListBegin(framebuffer, initialColorAction, finalColorAction, initialDepthAction, finalDepthAction, clearColorValues, clearDepth, clearStencil, region, new Godot.Collections.Array<Rid>(storageTextures));
return DrawListBegin(framebuffer, initialColorAction, finalColorAction, initialDepthAction, finalDepthAction, clearColorValues, clearDepth, clearStencil, region, new Godot.Collections.Array<Rid>(storageTextures ?? new()));
}
}

Expand All @@ -199,14 +201,14 @@ partial class SurfaceTool
{
/// <inheritdoc cref="AddTriangleFan(Vector3[], Vector2[], Color[], Vector2[], Vector3[], Godot.Collections.Array{Plane})"/>
[EditorBrowsable(EditorBrowsableState.Never)]
public void AddTriangleFan(Vector3[] vertices, Vector2[] uvs, Color[] colors, Vector2[] uv2S, Vector3[] normals, Godot.Collections.Array tangents)
public void AddTriangleFan(Vector3[] vertices, Vector2[]? uvs, Color[]? colors, Vector2[]? uv2S, Vector3[]? normals, Godot.Collections.Array? tangents)
{
AddTriangleFan(vertices, uvs, colors, uv2S, normals, new Godot.Collections.Array<Plane>(tangents));
AddTriangleFan(vertices, uvs, colors, uv2S, normals, new Godot.Collections.Array<Plane>(tangents ?? new()));
}

/// <inheritdoc cref="Commit(ArrayMesh, ulong)"/>
[EditorBrowsable(EditorBrowsableState.Never)]
public ArrayMesh Commit(ArrayMesh existing, uint flags)
public ArrayMesh? Commit(ArrayMesh existing, uint flags)
{
return Commit(existing, (ulong)flags);
}
Expand Down
2 changes: 1 addition & 1 deletion modules/mono/glue/GodotSharp/GodotSharp/Core/Array.cs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ public Array(Span<Rid> array)
/// The <paramref name="array"/> is <see langword="null"/>.
/// </exception>
/// <returns>A new Godot Array.</returns>
public Array(ReadOnlySpan<GodotObject> array)
public Array(ReadOnlySpan<GodotObject?> array)
{
if (array == null)
throw new ArgumentNullException(nameof(array));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ public static godot_variant CreateFromSystemArrayOfNodePath(Span<NodePath> from)
public static godot_variant CreateFromSystemArrayOfRid(Span<Rid> from)
=> CreateFromArray(new Collections.Array(from));

public static godot_variant CreateFromSystemArrayOfGodotObject(GodotObject[]? from)
public static godot_variant CreateFromSystemArrayOfGodotObject(GodotObject?[]? from)
{
if (from == null)
return default; // Nil
Expand Down
12 changes: 6 additions & 6 deletions modules/mono/glue/GodotSharp/GodotSharp/Core/Variant.cs
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ public Rid[] AsSystemArrayOfRid() =>
VariantUtils.ConvertToSystemArrayOfRid((godot_variant)NativeVar);

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public GodotObject AsGodotObject() =>
public GodotObject? AsGodotObject() =>
VariantUtils.ConvertToGodotObject((godot_variant)NativeVar);

[MethodImpl(MethodImplOptions.AggressiveInlining)]
Expand Down Expand Up @@ -501,7 +501,7 @@ public Collections.Array AsGodotArray() =>
public static explicit operator Rid[](Variant from) => from.AsSystemArrayOfRid();

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static explicit operator GodotObject(Variant from) => from.AsGodotObject();
public static explicit operator GodotObject?(Variant from) => from.AsGodotObject();

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static explicit operator StringName(Variant from) => from.AsStringName();
Expand Down Expand Up @@ -642,7 +642,7 @@ public Collections.Array AsGodotArray() =>
public static Variant CreateFrom(Span<Color> from) => from;

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Variant CreateFrom(GodotObject[] from) => from;
public static Variant CreateFrom(GodotObject?[] from) => from;

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Variant CreateFrom<[MustBeVariant] TKey, [MustBeVariant] TValue>(Collections.Dictionary<TKey, TValue> from) =>
Expand All @@ -662,7 +662,7 @@ public static Variant CreateFrom<[MustBeVariant] T>(Collections.Array<T> from) =
public static Variant CreateFrom(Span<Rid> from) => from;

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Variant CreateFrom(GodotObject from) => from;
public static Variant CreateFrom(GodotObject? from) => from;

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Variant CreateFrom(StringName from) => from;
Expand Down Expand Up @@ -842,7 +842,7 @@ public static implicit operator Variant(Color[] from) =>
(Variant)from.AsSpan();

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static implicit operator Variant(GodotObject[] from) =>
public static implicit operator Variant(GodotObject?[] from) =>
CreateTakingOwnershipOfDisposableValue(VariantUtils.CreateFromSystemArrayOfGodotObject(from));

[MethodImpl(MethodImplOptions.AggressiveInlining)]
Expand Down Expand Up @@ -906,7 +906,7 @@ public static implicit operator Variant(Span<Rid> from) =>
CreateTakingOwnershipOfDisposableValue(VariantUtils.CreateFromSystemArrayOfRid(from));

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static implicit operator Variant(GodotObject from) =>
public static implicit operator Variant(GodotObject? from) =>
CreateTakingOwnershipOfDisposableValue(VariantUtils.CreateFromGodotObject(from));

[MethodImpl(MethodImplOptions.AggressiveInlining)]
Expand Down
5 changes: 4 additions & 1 deletion modules/mono/glue/GodotSharp/GodotSharpEditor/Compat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,17 @@

namespace Godot;

#nullable enable

#pragma warning disable CS1734 // XML comment on 'X' has a paramref tag for 'Y', but there is no parameter by that name.
// TODO: This is currently disabled because of https://github.com/dotnet/roslyn/issues/52904
#pragma warning disable IDE0040 // Add accessibility modifiers.

partial class EditorUndoRedoManager
{
/// <inheritdoc cref="CreateAction(string, UndoRedo.MergeMode, GodotObject, bool)"/>
[EditorBrowsable(EditorBrowsableState.Never)]
public void CreateAction(string name, UndoRedo.MergeMode mergeMode, GodotObject customContext)
public void CreateAction(string name, UndoRedo.MergeMode mergeMode, GodotObject? customContext)
{
CreateAction(name, mergeMode, customContext, backwardUndoOps: false);
}
Expand Down
Loading