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. |
|
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.