Skip to content

Fixing System.Array constructor #107266

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

Merged
merged 2 commits into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ public ReadOnlyTensorSpan(T[]? array, int start, scoped ReadOnlySpan<nint> lengt
this = default;
return; // returns default
}

if (!typeof(T).IsValueType && array.GetType() != typeof(T[]))
ThrowHelper.ThrowArrayTypeMismatchException();

Expand Down Expand Up @@ -163,7 +164,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.

ThrowHelper.ThrowArrayTypeMismatchException();

strides = strides.IsEmpty ? (ReadOnlySpan<nint>)TensorSpanHelpers.CalculateStrides(lengths, linearLength) : strides;
Expand Down Expand Up @@ -207,7 +208,7 @@ public ReadOnlyTensorSpan(Array? array, scoped ReadOnlySpan<NIndex> startIndex,
this = default;
return; // returns default
}
if (!typeof(T).IsValueType && array.GetType() != typeof(T[]))
if (array.GetType().GetElementType() != typeof(T))
ThrowHelper.ThrowArrayTypeMismatchException();

strides = strides.IsEmpty ? (ReadOnlySpan<nint>)TensorSpanHelpers.CalculateStrides(lengths, linearLength) : strides;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ public TensorSpan(T[]? array, int start, scoped ReadOnlySpan<nint> lengths, scop
this = default;
return; // returns default
}

if (!typeof(T).IsValueType && array.GetType() != typeof(T[]))
ThrowHelper.ThrowArrayTypeMismatchException();

Expand Down Expand Up @@ -167,7 +168,7 @@ public TensorSpan(Array? array, scoped ReadOnlySpan<int> start, scoped ReadOnlyS
this = default;
return; // returns default
}
if (!typeof(T).IsValueType && array.GetType() != typeof(T[]))
if (array.GetType().GetElementType() != typeof(T))
ThrowHelper.ThrowArrayTypeMismatchException();

strides = strides.IsEmpty ? (ReadOnlySpan<nint>)TensorSpanHelpers.CalculateStrides(lengths, linearLength) : strides;
Expand Down Expand Up @@ -213,7 +214,7 @@ public TensorSpan(Array? array, scoped ReadOnlySpan<NIndex> startIndex, scoped R
this = default;
return; // returns default
}
if (!typeof(T).IsValueType && array.GetType() != typeof(T[]))
if (array.GetType().GetElementType() != typeof(T))
ThrowHelper.ThrowArrayTypeMismatchException();

nint startOffset = TensorSpanHelpers.ComputeStartOffsetSystemArray(array, startIndex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ public class ReadOnlyTensorSpanTests
[Fact]
public static void ReadOnlyTensorSpanSystemArrayConstructorTests()
{
// When using System.Array constructor make sure the type of the array matches T[]
Assert.Throws<ArrayTypeMismatchException>(() => new TensorSpan<double>(array: new[] { 1 }));

string[] stringArray = { "a", "b", "c" };
Assert.Throws<ArrayTypeMismatchException>(() => new TensorSpan<object>(array: stringArray));

// Make sure basic T[,] constructor works
int[,] a = new int[,] { { 91, 92, -93, 94 } };
scoped ReadOnlyTensorSpan<int> spanInt = new ReadOnlyTensorSpan<int>(a);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,12 @@ public void TensorExtensionsTwoSpanInFloatOut<T>(TensorPrimitivesTwoSpanInTOut<T
[Fact]
public static void TensorSpanSystemArrayConstructorTests()
{
// When using System.Array constructor make sure the type of the array matches T[]
Assert.Throws<ArrayTypeMismatchException>(() => new TensorSpan<double>(array: new[] { 1 }));

string[] stringArray = { "a", "b", "c" };
Assert.Throws<ArrayTypeMismatchException>(() => new TensorSpan<object>(array: stringArray));

// Make sure basic T[,] constructor works
int[,] a = new int[,] { { 91, 92, -93, 94 } };
scoped TensorSpan<int> spanInt = new TensorSpan<int>(a);
Expand Down
Loading