-
-
Notifications
You must be signed in to change notification settings - Fork 21.4k
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#: Fix errors when creating Variant
from null array
#89756
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch! I think this is fine, yep.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the contribution, I'd like to understand the PR a bit better before approving.
Returns default
Variant
(nil) if theSpan<*>
is null.
I'm not sure what you mean by this, Spans can't be null because they are value types. Can you explain why this change is needed? Do empty arrays work before/after this PR?
The current issue is probably because of checks like:
godot/modules/mono/glue/GodotSharp/GodotSharp/Core/Array.cs
Lines 88 to 91 in fe01776
public Array(Span<StringName> array) | |
{ | |
if (array == null) | |
throw new ArgumentNullException(nameof(array)); |
I imagine this was a copy-paste mistake. But then Array.Empty<StringName>()
should throw as well.
@@ -235,13 +235,28 @@ public static godot_variant CreateFromPackedColorArray(Span<Color> from) | |||
} | |||
|
|||
public static godot_variant CreateFromSystemArrayOfStringName(Span<StringName> from) | |||
=> CreateFromArray(new Collections.Array(from)); | |||
{ | |||
if (from == null) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks weird to me, Spans are value types so they can't be null. The only reason why they can be compared to null is because there is an implicit conversion from arrays to spans and the null
literal can be converted to array so basically this is doing this:
if (from == (Span<StringName>)((StringName[])null))
return default;
Which means this also returns a default Variant
for empty arrays and I'm not sure we want that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's ok because for empty arrays, the span won't be null (as its _pointer
points to that array). So, it returns default Variant
only when the array is null.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's true, I was thinking whether we should allow creating Godot.Array
from null but on second thought I think the changes in this PR are more correct.
06abc86
C#: Fix errors when creating `Variant` from null array
Thanks! |
Returns default
Variant
(nil) if theSpan<*>
is null.This also adds the disposal of temporary array.
Fix C# Array of StringNames Exported property with null default value causes null value exceptions in editor and in game. #89586
Fix Exporting NodePath as a C# array don't work #82362
Supersedes Fix C# array null handling #82388