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

[Breaking change]: Change the type of array instance returned RuntimeHelpers.GetSubArray to match the source array #38474

Closed
1 of 3 tasks
jkotas opened this issue Nov 29, 2023 · 2 comments · Fixed by #39198
Closed
1 of 3 tasks
Assignees
Labels
breaking-change Indicates a .NET Core breaking change 🏁 Release: .NET 9 Work items for the .NET 9 release doc-idea Indicates issues that are suggestions for new topics [org][type][category] Pri1 High priority, do before Pri2 and Pri3 📌 seQUESTered Identifies that an issue has been imported into Quest.

Comments

@jkotas
Copy link
Member

jkotas commented Nov 29, 2023

Description

The type of array instance returned by RuntimeHelpers.GetSubArray is changed to match the source array. RuntimeHelpers.GetSubArray is used by C# compiler to implement range operator for arrays.

This behavior change can be only observable by code that uses covariant array conversions.

Version

.NET 9 Preview 1

Previous behavior

RuntimeHelpers.GetSubArray<T>(T[] array, Range range) method returns an array instance of type T[].

For example, the type of array instance returned by RuntimeHelpers.GetSubArray<object>(new string[1], ...) is object[]

New behavior

RuntimeHelpers.GetSubArray<T>(T[] array, Range range) method returns an array instance of the same type as array argument.

For example, the type of array instance returned by RuntimeHelpers.GetSubArray<object>(new string[1], ...) is string[].

Type of breaking change

  • Binary incompatible: Existing binaries may encounter a breaking change in behavior, such as failure to load or execute, and if so, require recompilation.
  • Source incompatible: When recompiled using the new SDK or component or to target the new runtime, existing source code may require source changes to compile successfully.
  • Behavioral change: Existing binaries may behave differently at run time.

Reason for change

The design of C# pattern matching features assume that the type of array instance returned RuntimeHelpers.GetSubArray match the source array. The previous behavior led to unexpected behavior of certain complex pattern expressions that used slicing of co-variant arrays. See dotnet/roslyn#69053 for details.

Recommended action

The recommended action is to remove dependency of the affected code on array co-variance.

For example, change:

object[] arr = new string[1];
M(arr[1..2]);

to

string[] arr = new string[1];
M(arr[1..2]);

Feature area

Core .NET libraries

Affected APIs

System.Runtme.CompilerServices.RuntimeHelpers.GetSubArray


Associated WorkItem - 196551

@jkotas jkotas added doc-idea Indicates issues that are suggestions for new topics [org][type][category] breaking-change Indicates a .NET Core breaking change Pri1 High priority, do before Pri2 and Pri3 labels Nov 29, 2023
@dotnet-bot dotnet-bot added the ⌚ Not Triaged Not triaged label Nov 29, 2023
@jkotas
Copy link
Member Author

jkotas commented Nov 29, 2023

Context:
dotnet/roslyn#69053
dotnet/runtime#95296

@jkotas
Copy link
Member Author

jkotas commented Nov 29, 2023

cc @jcouv

@gewarren gewarren added the 🏁 Release: .NET 9 Work items for the .NET 9 release label Nov 29, 2023
@gewarren gewarren added the 🗺️ reQUEST Triggers an issue to be imported into Quest. label Jan 2, 2024
@github-actions github-actions bot added 📌 seQUESTered Identifies that an issue has been imported into Quest. and removed 🗺️ reQUEST Triggers an issue to be imported into Quest. labels Jan 3, 2024
@gewarren gewarren moved this from 🔖 Ready to 👀 In review in dotnet/docs January 2024 sprint Jan 18, 2024
@ghost ghost added the in-pr This issue will be closed (fixed) by an active pull request. label Jan 18, 2024
@github-project-automation github-project-automation bot moved this from 👀 In review to ✅ Done in dotnet/docs January 2024 sprint Jan 19, 2024
@dotnet-bot dotnet-bot removed the ⌚ Not Triaged Not triaged label Jan 19, 2024
@ghost ghost removed the in-pr This issue will be closed (fixed) by an active pull request. label Jan 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking-change Indicates a .NET Core breaking change 🏁 Release: .NET 9 Work items for the .NET 9 release doc-idea Indicates issues that are suggestions for new topics [org][type][category] Pri1 High priority, do before Pri2 and Pri3 📌 seQUESTered Identifies that an issue has been imported into Quest.
Projects
Status: ✅ Done
Development

Successfully merging a pull request may close this issue.

3 participants