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

Simplify RuntimeHelpers.GetSubArray #66011

Merged
merged 1 commit into from
Mar 2, 2022
Merged

Simplify RuntimeHelpers.GetSubArray #66011

merged 1 commit into from
Mar 2, 2022

Conversation

jkotas
Copy link
Member

@jkotas jkotas commented Mar 1, 2022

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.

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.
@ghost
Copy link

ghost commented Mar 1, 2022

Tagging subscribers to this area: @dotnet/area-system-runtime-compilerservices
See info in area-owners.md if you want to be subscribed.

Issue Details

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.

Author: jkotas
Assignees: jkotas
Labels:

area-System.Runtime.CompilerServices

Milestone: -

@jkotas
Copy link
Member Author

jkotas commented Mar 1, 2022

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

@stephentoub
Copy link
Member

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.

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[]?

@jkotas
Copy link
Member Author

jkotas commented Mar 2, 2022

Correct.

Copy link
Member

@MichalStrehovsky MichalStrehovsky left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

@MichalStrehovsky
Copy link
Member

Cc @LakshanF who will be looking at other AOT-warnings-triggering patterns in CoreLib.

@jkotas jkotas merged commit ca3c95b into dotnet:main Mar 2, 2022
@jkotas jkotas deleted the getsubarray branch March 2, 2022 14:43
@ghost ghost locked as resolved and limited conversation to collaborators Apr 2, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants