From a62b7417dce1aca98e3684c09bb9ad89f0503868 Mon Sep 17 00:00:00 2001 From: Michael Sharp Date: Mon, 2 Sep 2024 15:42:58 -0600 Subject: [PATCH 1/2] fixing System.Array constructor --- .../System/Numerics/Tensors/netcore/ReadOnlyTensorSpan.cs | 6 ++---- .../src/System/Numerics/Tensors/netcore/TensorSpan.cs | 6 ++---- .../tests/ReadOnlyTensorSpanTests.cs | 3 +++ .../System.Numerics.Tensors/tests/TensorSpanTests.cs | 3 +++ 4 files changed, 10 insertions(+), 8 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 0e29b57c2fe544..35c76fdca67452 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 @@ -84,8 +84,6 @@ public ReadOnlyTensorSpan(T[]? array, int start, scoped ReadOnlySpan lengt this = default; return; // returns default } - if (!typeof(T).IsValueType && array.GetType() != typeof(T[])) - ThrowHelper.ThrowArrayTypeMismatchException(); strides = strides.IsEmpty ? (ReadOnlySpan)TensorSpanHelpers.CalculateStrides(lengths, linearLength) : strides; TensorSpanHelpers.ValidateStrides(strides, lengths); @@ -166,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; @@ -211,7 +209,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 eacdc662c72d1f..a363463de5f391 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,8 +85,6 @@ 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(); strides = strides.IsEmpty ? (ReadOnlySpan)TensorSpanHelpers.CalculateStrides(lengths, linearLength) : strides; TensorSpanHelpers.ValidateStrides(strides, lengths); @@ -169,7 +167,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; @@ -216,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 e5e4a8c60bdc7b..13141546184a8c 100644 --- a/src/libraries/System.Numerics.Tensors/tests/ReadOnlyTensorSpanTests.cs +++ b/src/libraries/System.Numerics.Tensors/tests/ReadOnlyTensorSpanTests.cs @@ -16,6 +16,9 @@ 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 })); + // 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 f03f682f94f704..0d7fe4eb1393db 100644 --- a/src/libraries/System.Numerics.Tensors/tests/TensorSpanTests.cs +++ b/src/libraries/System.Numerics.Tensors/tests/TensorSpanTests.cs @@ -557,6 +557,9 @@ public void TensorExtensionsTwoSpanInFloatOut(TensorPrimitivesTwoSpanInTOut(() => new TensorSpan(array: new[] { 1 })); + // Make sure basic T[,] constructor works int[,] a = new int[,] { { 91, 92, -93, 94 } }; scoped TensorSpan spanInt = new TensorSpan(a); From 84bdb7c4417fb2b0baaf8ce411fce2e9f1a62d86 Mon Sep 17 00:00:00 2001 From: Michael Sharp Date: Thu, 5 Sep 2024 14:13:11 -0600 Subject: [PATCH 2/2] adding back in co-varience check and tests --- .../src/System/Numerics/Tensors/netcore/ReadOnlyTensorSpan.cs | 3 +++ .../src/System/Numerics/Tensors/netcore/TensorSpan.cs | 3 +++ .../System.Numerics.Tensors/tests/ReadOnlyTensorSpanTests.cs | 3 +++ src/libraries/System.Numerics.Tensors/tests/TensorSpanTests.cs | 3 +++ 4 files changed, 12 insertions(+) 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 35c76fdca67452..b71e560bdd17dc 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 @@ -85,6 +85,9 @@ public ReadOnlyTensorSpan(T[]? array, int start, scoped ReadOnlySpan lengt return; // returns default } + if (!typeof(T).IsValueType && array.GetType() != typeof(T[])) + ThrowHelper.ThrowArrayTypeMismatchException(); + strides = strides.IsEmpty ? (ReadOnlySpan)TensorSpanHelpers.CalculateStrides(lengths, linearLength) : strides; TensorSpanHelpers.ValidateStrides(strides, lengths); nint maxElements = TensorSpanHelpers.ComputeMaxLinearIndex(strides, lengths); 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 a363463de5f391..8bd8f989f38959 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 @@ -86,6 +86,9 @@ public TensorSpan(T[]? array, int start, scoped ReadOnlySpan lengths, scop return; // returns default } + if (!typeof(T).IsValueType && array.GetType() != typeof(T[])) + ThrowHelper.ThrowArrayTypeMismatchException(); + strides = strides.IsEmpty ? (ReadOnlySpan)TensorSpanHelpers.CalculateStrides(lengths, linearLength) : strides; TensorSpanHelpers.ValidateStrides(strides, lengths); nint maxElements = TensorSpanHelpers.ComputeMaxLinearIndex(strides, lengths); diff --git a/src/libraries/System.Numerics.Tensors/tests/ReadOnlyTensorSpanTests.cs b/src/libraries/System.Numerics.Tensors/tests/ReadOnlyTensorSpanTests.cs index 13141546184a8c..a962c84ada24f8 100644 --- a/src/libraries/System.Numerics.Tensors/tests/ReadOnlyTensorSpanTests.cs +++ b/src/libraries/System.Numerics.Tensors/tests/ReadOnlyTensorSpanTests.cs @@ -19,6 +19,9 @@ 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 0d7fe4eb1393db..644a3de4290830 100644 --- a/src/libraries/System.Numerics.Tensors/tests/TensorSpanTests.cs +++ b/src/libraries/System.Numerics.Tensors/tests/TensorSpanTests.cs @@ -560,6 +560,9 @@ public static void TensorSpanSystemArrayConstructorTests() // 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 TensorSpan spanInt = new TensorSpan(a);