From 16506b76e865e5afae7fdffab6ea4a111c866c73 Mon Sep 17 00:00:00 2001 From: Michael Sharp <51342856+michaelgsharp@users.noreply.github.com> Date: Sun, 15 Sep 2024 22:01:43 -0600 Subject: [PATCH] Fixing System.Array constructor (#107266) * fixing System.Array constructor * adding back in co-varience check and tests --- .../System/Numerics/Tensors/netcore/ReadOnlyTensorSpan.cs | 5 +++-- .../src/System/Numerics/Tensors/netcore/TensorSpan.cs | 5 +++-- .../tests/ReadOnlyTensorSpanTests.cs | 6 ++++++ .../System.Numerics.Tensors/tests/TensorSpanTests.cs | 6 ++++++ 4 files changed, 18 insertions(+), 4 deletions(-) diff --git a/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/ReadOnlyTensorSpan.cs b/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/ReadOnlyTensorSpan.cs index 8abba1dfeb998..e20e4918d955a 100644 --- a/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/ReadOnlyTensorSpan.cs +++ b/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/ReadOnlyTensorSpan.cs @@ -83,6 +83,7 @@ public ReadOnlyTensorSpan(T[]? array, int start, scoped ReadOnlySpan lengt this = default; return; // returns default } + if (!typeof(T).IsValueType && array.GetType() != typeof(T[])) ThrowHelper.ThrowArrayTypeMismatchException(); @@ -163,7 +164,7 @@ public ReadOnlyTensorSpan(Array? array, scoped ReadOnlySpan 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)TensorSpanHelpers.CalculateStrides(lengths, linearLength) : strides; @@ -207,7 +208,7 @@ public ReadOnlyTensorSpan(Array? array, scoped ReadOnlySpan 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)TensorSpanHelpers.CalculateStrides(lengths, linearLength) : strides; diff --git a/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/TensorSpan.cs b/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/TensorSpan.cs index 16361d689dbcf..50d5bc55057ca 100644 --- a/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/TensorSpan.cs +++ b/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/TensorSpan.cs @@ -85,6 +85,7 @@ public TensorSpan(T[]? array, int start, scoped ReadOnlySpan lengths, scop this = default; return; // returns default } + if (!typeof(T).IsValueType && array.GetType() != typeof(T[])) ThrowHelper.ThrowArrayTypeMismatchException(); @@ -167,7 +168,7 @@ public TensorSpan(Array? array, scoped ReadOnlySpan 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)TensorSpanHelpers.CalculateStrides(lengths, linearLength) : strides; @@ -213,7 +214,7 @@ public TensorSpan(Array? array, scoped ReadOnlySpan 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); diff --git a/src/libraries/System.Numerics.Tensors/tests/ReadOnlyTensorSpanTests.cs b/src/libraries/System.Numerics.Tensors/tests/ReadOnlyTensorSpanTests.cs index e5e4a8c60bdc7..a962c84ada24f 100644 --- a/src/libraries/System.Numerics.Tensors/tests/ReadOnlyTensorSpanTests.cs +++ b/src/libraries/System.Numerics.Tensors/tests/ReadOnlyTensorSpanTests.cs @@ -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(() => new TensorSpan(array: new[] { 1 })); + + string[] stringArray = { "a", "b", "c" }; + Assert.Throws(() => new TensorSpan(array: stringArray)); + // Make sure basic T[,] constructor works int[,] a = new int[,] { { 91, 92, -93, 94 } }; scoped ReadOnlyTensorSpan spanInt = new ReadOnlyTensorSpan(a); diff --git a/src/libraries/System.Numerics.Tensors/tests/TensorSpanTests.cs b/src/libraries/System.Numerics.Tensors/tests/TensorSpanTests.cs index 737ba5cf5374b..145f619d9cd2c 100644 --- a/src/libraries/System.Numerics.Tensors/tests/TensorSpanTests.cs +++ b/src/libraries/System.Numerics.Tensors/tests/TensorSpanTests.cs @@ -558,6 +558,12 @@ public void TensorExtensionsTwoSpanInFloatOut(TensorPrimitivesTwoSpanInTOut(() => new TensorSpan(array: new[] { 1 })); + + string[] stringArray = { "a", "b", "c" }; + Assert.Throws(() => new TensorSpan(array: stringArray)); + // Make sure basic T[,] constructor works int[,] a = new int[,] { { 91, 92, -93, 94 } }; scoped TensorSpan spanInt = new TensorSpan(a);