Skip to content

Commit

Permalink
Fixing System.Array constructor (#107266)
Browse files Browse the repository at this point in the history
* fixing System.Array constructor

* adding back in co-varience check and tests
  • Loading branch information
michaelgsharp committed Sep 16, 2024
1 parent 4c10eff commit 16506b7
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 4 deletions.
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))
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

0 comments on commit 16506b7

Please sign in to comment.