-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Adding null check for implicit cast from array to Span. #25257
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.
Probably best solution; going through a .ctor would do more work
What about the cast from ArraySegment? |
@@ -34,6 +34,24 @@ public static void TryCopyToSingle() | |||
} | |||
|
|||
[Fact] | |||
public static void TryCopyToDefaultImplicit() | |||
{ | |||
int[] dst = default; |
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'd be easier to understand as null instead of default
int[] dst = null;
int[] dst = default; | ||
Span<int> srcSpan = default; | ||
bool success = srcSpan.TryCopyTo(dst); | ||
Assert.True(success); |
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.
Nit: why store into a bool rather than just asserting the expression?
Assert.True(srcSpan.TryCopyTo(dst));
Span<int> srcSpan = default; | ||
bool success = srcSpan.TryCopyTo(dst); | ||
Assert.True(success); | ||
} |
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.
How is this test different from the previous one? We don't need a rest that verifies that the C# compiler knows that default for an array is null.
{ | ||
int[] dst = null; | ||
Span<int> srcSpan = default; | ||
bool success = srcSpan.TryCopyTo(dst); |
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.
Why call a method rather than just doing the cast?
int[] dst = null;
Span<int> srcSpan = dst;
Assert.True(Span<int>.Empty == srcSpan);
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.
Right. Why does that require calling a method? My example does that without TryCopyTo.
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.
Nvm, I see what you mean.
I was thinking of adding it but array segment can't be null. However, I realize that the array property can be null (for default value of ArraySegment). Will add the check.
I will do this in a future PR (want to reduce conflicts in Memory, so will add it after the current PRs go through). |
This looks like a better fix for https://github.com/dotnet/corefx/issues/16730 than my suggestion on that thread. |
@dotnet-bot test Windows x86 Release Build |
@dotnet-bot test this please |
Windows.7.Amd64.Open-Release-x86 System.Diagnostics.PerformanceCounter.Tests xunit System.Diagnostics.Tests.PerformanceCounterTests/PerformanceCounter_IncrementBy_IncrementByReadOnly
OSX.1013.Amd64.Open:Debug-x64
cc @safern |
@dotnet-bot test OSX x64 Debug Build |
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.
LGTM
…x#25257) * Adding null check for implicit cast from array to Span. * Addressing PR feedback - adding check for ArraySegment Commit migrated from dotnet/corefx@8a4934f
Fixes https://github.com/dotnet/corefx/issues/24409
Edit: Also fixes https://github.com/dotnet/corefx/issues/16730
Will depend on the corresponding PR in coreclr (which will be identical to this one and I will submit it after I address the feedback here).Depends on dotnet/coreclr#15044
cc @stephentoub, @KrzysztofCwalina, @benaadams, @ericstj