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

Fixing System.Array constructor #107266

Merged
merged 2 commits into from
Sep 16, 2024

Conversation

michaelgsharp
Copy link
Member

Fixes #106534. Fixes the check in the System.Array constructor to make sure the type of the Array matches the T that was used.

@@ -166,7 +167,7 @@ public ReadOnlyTensorSpan(Array? array, scoped ReadOnlySpan<int> start, scoped R
this = default;
return; // returns default
}
if (!typeof(T).IsValueType && array.GetType() != typeof(T[]))
if (array.GetType().GetElementType() != typeof(T))
Copy link
Member

Choose a reason for hiding this comment

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

This looks like a correct fix. But I'd like us to get an issue logged around this, as this seems like a pit of failure here.

In particular, since Array is "untyped" and due to how new[] {...} works, it means that users can get into these types of silent failures; when they likely expected it to be implicitly typed as double[] like [...] would've been.

So I think we may want to consider if this should be a constructor or instead some named Create* method where * is an appropriate suffix.

@michaelgsharp
Copy link
Member Author

/backport to release/9.0

Copy link
Contributor

github-actions bot commented Sep 9, 2024

Started backporting to release/9.0: https://github.com/dotnet/runtime/actions/runs/10779752164

@michaelgsharp
Copy link
Member Author

/azp run

Copy link

You have several pipelines (over 10) configured to build pull requests in this repository. Specify which pipelines you would like to run by using /azp run [pipelines] command. You can specify multiple pipelines using a comma separated list.

@michaelgsharp michaelgsharp merged commit 16506b7 into dotnet:main Sep 16, 2024
85 checks passed
@michaelgsharp michaelgsharp deleted the array-constructor-check branch September 16, 2024 04:01
jtschuster pushed a commit to jtschuster/runtime that referenced this pull request Sep 17, 2024
* fixing System.Array constructor

* adding back in co-varience check and tests
sirntar pushed a commit to sirntar/runtime that referenced this pull request Sep 30, 2024
* fixing System.Array constructor

* adding back in co-varience check and tests
@github-actions github-actions bot locked and limited conversation to collaborators Oct 16, 2024
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.

System.Numerics.Tensors constructor safety
3 participants