-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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
Simplify RuntimeHelpers.GetSubArray #66011
Conversation
The special casing of co-variant arrays was added in the original safe implementation to avoid exceptions from AsSpan (dotnet/coreclr#22331 (comment)). ArrayTypeMismatchException is no longer a concern with the new unsafe implementation. There is a subtle behavior change in the actual type for co-variant arrays of reference types. However, the new behavior matches Array.Resize and it is very unlikely for any code out there to depend on this co-variant arrays corner case.
Tagging subscribers to this area: @dotnet/area-system-runtime-compilerservices Issue DetailsThe special casing of co-variant arrays was added in the original safe implementation to avoid exceptions from AsSpan (dotnet/coreclr#22331 (comment)). ArrayTypeMismatchException is no longer a concern with the new unsafe implementation. There is a subtle behavior change in the actual type for co-variant arrays of reference types. However, the new behavior matches Array.Resize and it is very unlikely for any code out there to depend on this co-variant arrays corner case.
|
My motivation for this change are AOT compatibility warnings caused by Array.CreateInstance. An alternative is to add suppression of the AOT compatibility warning (the warning is harmless - the array type is guaranteed to exist). |
The issue would be if I had code like: static T[] Slice<T>(T[] array) => array[low..high];
class A : B { }
A[] array = new A[42];
A[] sliced = (A[])Slice<B>(array); that would now fail to cast whereas previously it would succeed? Or conversely: static T[] Slice<T>(T[] array) => array[low..high];
class A : B { }
class C : B { }
A[] array = new A[42];
B[] sliced = Slice<B>(array);
sliced[0] = new C(); that would now succeed whereas previously it would fail due to trying to store a C into an A[]? |
Correct. |
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.
Thank you!
Cc @LakshanF who will be looking at other AOT-warnings-triggering patterns in CoreLib. |
The special casing of co-variant arrays was added in the original safe implementation to avoid exceptions from AsSpan (dotnet/coreclr#22331 (comment)). ArrayTypeMismatchException is no longer a concern with the new unsafe implementation. There is a subtle behavior change in the actual type for co-variant arrays of reference types. However, the new behavior matches Array.Resize and it is very unlikely for any code out there to depend on this co-variant arrays corner case.