From 0502653d9d0ee92ed593d408c540caf916f6e6e7 Mon Sep 17 00:00:00 2001 From: Tarek Mahmoud Sayed Date: Fri, 28 Jun 2024 10:42:37 -0700 Subject: [PATCH 1/3] Polyfill Index and Range types --- .../CompilerServices/RuntimeHelpers.cs | 45 +++++++++++++++++++ .../ref/Microsoft.Bcl.Memory.Forwards.ns21.cs | 5 +++ .../ref/Microsoft.Bcl.Memory.csproj | 14 +++++- .../ref/Microsoft.Bcl.Memory.ns20.cs | 41 +++++++++++++++++ .../src/Microsoft.Bcl.Memory.csproj | 18 +++++++- .../tests/Microsoft.Bcl.Memory.Tests.csproj | 13 +++++- .../src/System/Index.cs | 2 +- .../src/System/Range.cs | 2 +- 8 files changed, 133 insertions(+), 7 deletions(-) create mode 100644 src/libraries/Common/tests/System/Runtime/CompilerServices/RuntimeHelpers.cs create mode 100644 src/libraries/Microsoft.Bcl.Memory/ref/Microsoft.Bcl.Memory.Forwards.ns21.cs create mode 100644 src/libraries/Microsoft.Bcl.Memory/ref/Microsoft.Bcl.Memory.ns20.cs diff --git a/src/libraries/Common/tests/System/Runtime/CompilerServices/RuntimeHelpers.cs b/src/libraries/Common/tests/System/Runtime/CompilerServices/RuntimeHelpers.cs new file mode 100644 index 0000000000000..97927c754a314 --- /dev/null +++ b/src/libraries/Common/tests/System/Runtime/CompilerServices/RuntimeHelpers.cs @@ -0,0 +1,45 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +#if (NETSTANDARD2_0 || NETFRAMEWORK) +namespace System.Runtime.CompilerServices +{ + internal static class RuntimeHelpers + { + /// + /// Slices the specified array using the specified range. + /// + internal static T[] GetSubArray(T[] array, Range range) + { + if (array == null) + { + throw new ArgumentNullException(nameof(array)); + } + + (int offset, int length) = range.GetOffsetAndLength(array.Length); + + if (default(T) != null || typeof(T[]) == array.GetType()) + { + // We know the type of the array to be exactly T[]. + + if (length == 0) + { + return Array.Empty(); + } + + var dest = new T[length]; + Array.Copy(array, offset, dest, 0, length); + return dest; + } + else + { + // The array is actually a U[] where U:T. + var dest = (T[])Array.CreateInstance(array.GetType().GetElementType(), length); + Array.Copy(array, offset, dest, 0, length); + return dest; + } + } + } +} +#endif + diff --git a/src/libraries/Microsoft.Bcl.Memory/ref/Microsoft.Bcl.Memory.Forwards.ns21.cs b/src/libraries/Microsoft.Bcl.Memory/ref/Microsoft.Bcl.Memory.Forwards.ns21.cs new file mode 100644 index 0000000000000..4bfc68544aa76 --- /dev/null +++ b/src/libraries/Microsoft.Bcl.Memory/ref/Microsoft.Bcl.Memory.Forwards.ns21.cs @@ -0,0 +1,5 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +[assembly: System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Index))] +[assembly: System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Range))] diff --git a/src/libraries/Microsoft.Bcl.Memory/ref/Microsoft.Bcl.Memory.csproj b/src/libraries/Microsoft.Bcl.Memory/ref/Microsoft.Bcl.Memory.csproj index a122f2cfeebb5..1a9c6a6890964 100644 --- a/src/libraries/Microsoft.Bcl.Memory/ref/Microsoft.Bcl.Memory.csproj +++ b/src/libraries/Microsoft.Bcl.Memory/ref/Microsoft.Bcl.Memory.csproj @@ -1,7 +1,7 @@ - netstandard2.0;$(NetFrameworkMinimum);$(NetCoreAppCurrent) + netstandard2.0;netstandard2.1;$(NetFrameworkMinimum);$(NetCoreAppCurrent) @@ -12,6 +12,18 @@ + + + + + + + + + + + + diff --git a/src/libraries/Microsoft.Bcl.Memory/ref/Microsoft.Bcl.Memory.ns20.cs b/src/libraries/Microsoft.Bcl.Memory/ref/Microsoft.Bcl.Memory.ns20.cs new file mode 100644 index 0000000000000..3044055cc4834 --- /dev/null +++ b/src/libraries/Microsoft.Bcl.Memory/ref/Microsoft.Bcl.Memory.ns20.cs @@ -0,0 +1,41 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// ------------------------------------------------------------------------------ +// Changes to this file must follow the https://aka.ms/api-review process. +// ------------------------------------------------------------------------------ + +namespace System +{ + public readonly partial struct Index : System.IEquatable + { + private readonly int _dummyPrimitive; + public Index(int value, bool fromEnd = false) { throw null; } + public static System.Index End { get { throw null; } } + public bool IsFromEnd { get { throw null; } } + public static System.Index Start { get { throw null; } } + public int Value { get { throw null; } } + public bool Equals(System.Index other) { throw null; } + public override bool Equals([System.Diagnostics.CodeAnalysis.NotNullWhenAttribute(true)] object? value) { throw null; } + public static System.Index FromEnd(int value) { throw null; } + public static System.Index FromStart(int value) { throw null; } + public override int GetHashCode() { throw null; } + public int GetOffset(int length) { throw null; } + public static implicit operator System.Index (int value) { throw null; } + public override string ToString() { throw null; } + } + public readonly partial struct Range : System.IEquatable + { + private readonly int _dummyPrimitive; + public Range(System.Index start, System.Index end) { throw null; } + public static System.Range All { get { throw null; } } + public System.Index End { get { throw null; } } + public System.Index Start { get { throw null; } } + public static System.Range EndAt(System.Index end) { throw null; } + public override bool Equals([System.Diagnostics.CodeAnalysis.NotNullWhenAttribute(true)] object? value) { throw null; } + public bool Equals(System.Range other) { throw null; } + public override int GetHashCode() { throw null; } + public (int Offset, int Length) GetOffsetAndLength(int length) { throw null; } + public static System.Range StartAt(System.Index start) { throw null; } + public override string ToString() { throw null; } + } +} \ No newline at end of file diff --git a/src/libraries/Microsoft.Bcl.Memory/src/Microsoft.Bcl.Memory.csproj b/src/libraries/Microsoft.Bcl.Memory/src/Microsoft.Bcl.Memory.csproj index 8c8600b3c459d..42218d6c5b863 100644 --- a/src/libraries/Microsoft.Bcl.Memory/src/Microsoft.Bcl.Memory.csproj +++ b/src/libraries/Microsoft.Bcl.Memory/src/Microsoft.Bcl.Memory.csproj @@ -1,7 +1,7 @@ - netstandard2.0;$(NetFrameworkMinimum);$(NetCoreAppCurrent) + netstandard2.0;netstandard2.1;$(NetFrameworkMinimum);$(NetCoreAppCurrent) true true @@ -25,6 +26,10 @@ + + + + System\Buffers\Text\Base64Helper\Base64Helper.cs @@ -49,8 +54,17 @@ + + + + + + - + + + + diff --git a/src/libraries/Microsoft.Bcl.Memory/tests/Microsoft.Bcl.Memory.Tests.csproj b/src/libraries/Microsoft.Bcl.Memory/tests/Microsoft.Bcl.Memory.Tests.csproj index f3f7262755c19..722d1592b749d 100644 --- a/src/libraries/Microsoft.Bcl.Memory/tests/Microsoft.Bcl.Memory.Tests.csproj +++ b/src/libraries/Microsoft.Bcl.Memory/tests/Microsoft.Bcl.Memory.Tests.csproj @@ -24,10 +24,19 @@ System\Memory\Base64Url\Base64UrlValidationUnitTests.cs + + System\RangeTests.cs + + + System\IndexTests.cs + + + System\Runtime\CompilerServices\RuntimeHelpers.cs + - + - + diff --git a/src/libraries/System.Private.CoreLib/src/System/Index.cs b/src/libraries/System.Private.CoreLib/src/System/Index.cs index 23c3765e8bcc6..8858102be1e79 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Index.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Index.cs @@ -15,7 +15,7 @@ namespace System /// int lastElement = someArray[^1]; // lastElement = 5 /// /// -#if SYSTEM_PRIVATE_CORELIB +#if SYSTEM_PRIVATE_CORELIB || MICROSOFT_BCL_MEMORY public #else internal diff --git a/src/libraries/System.Private.CoreLib/src/System/Range.cs b/src/libraries/System.Private.CoreLib/src/System/Range.cs index 76308162f110c..f29f366ef486f 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Range.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Range.cs @@ -20,7 +20,7 @@ namespace System /// int[] subArray2 = someArray[1..^0]; // { 2, 3, 4, 5 } /// /// -#if SYSTEM_PRIVATE_CORELIB +#if SYSTEM_PRIVATE_CORELIB || MICROSOFT_BCL_MEMORY public #else internal From 4b525280ebf620ca3430239f5f528f2d3451ef88 Mon Sep 17 00:00:00 2001 From: Tarek Mahmoud Sayed Date: Fri, 28 Jun 2024 18:35:06 -0700 Subject: [PATCH 2/3] Address the feedback --- .../Runtime/CompilerServices/RuntimeHelpers.cs | 2 -- ...> Microsoft.Bcl.Memory.Forwards.netstandard21.cs} | 0 .../ref/Microsoft.Bcl.Memory.csproj | 10 ++++++---- ...ns20.cs => Microsoft.Bcl.Memory.netstandard20.cs} | 0 .../src/Microsoft.Bcl.Memory.csproj | 12 ++++++++---- .../tests/Microsoft.Bcl.Memory.Tests.csproj | 4 ++++ .../System.Private.CoreLib/src/System/Index.cs | 7 +------ .../System.Private.CoreLib/src/System/Range.cs | 7 +------ .../ComInterfaceGenerator.csproj | 2 +- 9 files changed, 21 insertions(+), 23 deletions(-) rename src/libraries/Microsoft.Bcl.Memory/ref/{Microsoft.Bcl.Memory.Forwards.ns21.cs => Microsoft.Bcl.Memory.Forwards.netstandard21.cs} (100%) rename src/libraries/Microsoft.Bcl.Memory/ref/{Microsoft.Bcl.Memory.ns20.cs => Microsoft.Bcl.Memory.netstandard20.cs} (100%) diff --git a/src/libraries/Common/tests/System/Runtime/CompilerServices/RuntimeHelpers.cs b/src/libraries/Common/tests/System/Runtime/CompilerServices/RuntimeHelpers.cs index 97927c754a314..1b7fe928a7fee 100644 --- a/src/libraries/Common/tests/System/Runtime/CompilerServices/RuntimeHelpers.cs +++ b/src/libraries/Common/tests/System/Runtime/CompilerServices/RuntimeHelpers.cs @@ -1,7 +1,6 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. -#if (NETSTANDARD2_0 || NETFRAMEWORK) namespace System.Runtime.CompilerServices { internal static class RuntimeHelpers @@ -41,5 +40,4 @@ internal static T[] GetSubArray(T[] array, Range range) } } } -#endif diff --git a/src/libraries/Microsoft.Bcl.Memory/ref/Microsoft.Bcl.Memory.Forwards.ns21.cs b/src/libraries/Microsoft.Bcl.Memory/ref/Microsoft.Bcl.Memory.Forwards.netstandard21.cs similarity index 100% rename from src/libraries/Microsoft.Bcl.Memory/ref/Microsoft.Bcl.Memory.Forwards.ns21.cs rename to src/libraries/Microsoft.Bcl.Memory/ref/Microsoft.Bcl.Memory.Forwards.netstandard21.cs diff --git a/src/libraries/Microsoft.Bcl.Memory/ref/Microsoft.Bcl.Memory.csproj b/src/libraries/Microsoft.Bcl.Memory/ref/Microsoft.Bcl.Memory.csproj index 1a9c6a6890964..ae94b2d611097 100644 --- a/src/libraries/Microsoft.Bcl.Memory/ref/Microsoft.Bcl.Memory.csproj +++ b/src/libraries/Microsoft.Bcl.Memory/ref/Microsoft.Bcl.Memory.csproj @@ -12,12 +12,14 @@ - - + + + - - + + + diff --git a/src/libraries/Microsoft.Bcl.Memory/ref/Microsoft.Bcl.Memory.ns20.cs b/src/libraries/Microsoft.Bcl.Memory/ref/Microsoft.Bcl.Memory.netstandard20.cs similarity index 100% rename from src/libraries/Microsoft.Bcl.Memory/ref/Microsoft.Bcl.Memory.ns20.cs rename to src/libraries/Microsoft.Bcl.Memory/ref/Microsoft.Bcl.Memory.netstandard20.cs diff --git a/src/libraries/Microsoft.Bcl.Memory/src/Microsoft.Bcl.Memory.csproj b/src/libraries/Microsoft.Bcl.Memory/src/Microsoft.Bcl.Memory.csproj index 42218d6c5b863..1de91b1f9df22 100644 --- a/src/libraries/Microsoft.Bcl.Memory/src/Microsoft.Bcl.Memory.csproj +++ b/src/libraries/Microsoft.Bcl.Memory/src/Microsoft.Bcl.Memory.csproj @@ -10,11 +10,13 @@ true Provides Base64Url encoding, decoding and validation APIs support for .NET Framework and .NET Standard. + Provides Index and Range types support for .NET Framework and .NET Standard 2.0. Commonly Used Types: System.Buffers.Text.Base64Url + System.Index + System.Range - $(DefineConstants);MICROSOFT_BCL_MEMORY @@ -26,8 +28,9 @@ - - + + + @@ -54,7 +57,8 @@ - + + diff --git a/src/libraries/Microsoft.Bcl.Memory/tests/Microsoft.Bcl.Memory.Tests.csproj b/src/libraries/Microsoft.Bcl.Memory/tests/Microsoft.Bcl.Memory.Tests.csproj index 722d1592b749d..e805a830958db 100644 --- a/src/libraries/Microsoft.Bcl.Memory/tests/Microsoft.Bcl.Memory.Tests.csproj +++ b/src/libraries/Microsoft.Bcl.Memory/tests/Microsoft.Bcl.Memory.Tests.csproj @@ -30,6 +30,10 @@ System\IndexTests.cs + + + + System\Runtime\CompilerServices\RuntimeHelpers.cs diff --git a/src/libraries/System.Private.CoreLib/src/System/Index.cs b/src/libraries/System.Private.CoreLib/src/System/Index.cs index 8858102be1e79..d848e75caf7d1 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Index.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Index.cs @@ -15,12 +15,7 @@ namespace System /// int lastElement = someArray[^1]; // lastElement = 5 /// /// -#if SYSTEM_PRIVATE_CORELIB || MICROSOFT_BCL_MEMORY - public -#else - internal -#endif - readonly struct Index : IEquatable + public readonly struct Index : IEquatable { private readonly int _value; diff --git a/src/libraries/System.Private.CoreLib/src/System/Range.cs b/src/libraries/System.Private.CoreLib/src/System/Range.cs index f29f366ef486f..8ef8e5fac1f9e 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Range.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Range.cs @@ -20,12 +20,7 @@ namespace System /// int[] subArray2 = someArray[1..^0]; // { 2, 3, 4, 5 } /// /// -#if SYSTEM_PRIVATE_CORELIB || MICROSOFT_BCL_MEMORY - public -#else - internal -#endif - readonly struct Range : IEquatable + public readonly struct Range : IEquatable { /// Represent the inclusive start index of the Range. public Index Start { get; } diff --git a/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/ComInterfaceGenerator.csproj b/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/ComInterfaceGenerator.csproj index 8d187cfa8b38a..7284f2b9500d1 100644 --- a/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/ComInterfaceGenerator.csproj +++ b/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/ComInterfaceGenerator.csproj @@ -26,11 +26,11 @@ - + From 2fe9f5d83cabe80703b345a715ff1995c24abf4e Mon Sep 17 00:00:00 2001 From: Tarek Mahmoud Sayed Date: Tue, 2 Jul 2024 09:44:22 -0700 Subject: [PATCH 3/3] Revert ComInterface source gen changes --- .../Microsoft.Bcl.Memory/src/Microsoft.Bcl.Memory.csproj | 1 + src/libraries/System.Private.CoreLib/src/System/Index.cs | 7 ++++++- src/libraries/System.Private.CoreLib/src/System/Range.cs | 7 ++++++- .../gen/ComInterfaceGenerator/ComInterfaceGenerator.csproj | 2 +- 4 files changed, 14 insertions(+), 3 deletions(-) diff --git a/src/libraries/Microsoft.Bcl.Memory/src/Microsoft.Bcl.Memory.csproj b/src/libraries/Microsoft.Bcl.Memory/src/Microsoft.Bcl.Memory.csproj index 1de91b1f9df22..0b191a869540f 100644 --- a/src/libraries/Microsoft.Bcl.Memory/src/Microsoft.Bcl.Memory.csproj +++ b/src/libraries/Microsoft.Bcl.Memory/src/Microsoft.Bcl.Memory.csproj @@ -4,6 +4,7 @@ netstandard2.0;netstandard2.1;$(NetFrameworkMinimum);$(NetCoreAppCurrent) true true + MICROSOFT_BCL_MEMORY;$(DefineConstants) diff --git a/src/libraries/System.Private.CoreLib/src/System/Index.cs b/src/libraries/System.Private.CoreLib/src/System/Index.cs index d848e75caf7d1..8858102be1e79 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Index.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Index.cs @@ -15,7 +15,12 @@ namespace System /// int lastElement = someArray[^1]; // lastElement = 5 /// /// - public readonly struct Index : IEquatable +#if SYSTEM_PRIVATE_CORELIB || MICROSOFT_BCL_MEMORY + public +#else + internal +#endif + readonly struct Index : IEquatable { private readonly int _value; diff --git a/src/libraries/System.Private.CoreLib/src/System/Range.cs b/src/libraries/System.Private.CoreLib/src/System/Range.cs index 8ef8e5fac1f9e..f29f366ef486f 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Range.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Range.cs @@ -20,7 +20,12 @@ namespace System /// int[] subArray2 = someArray[1..^0]; // { 2, 3, 4, 5 } /// /// - public readonly struct Range : IEquatable +#if SYSTEM_PRIVATE_CORELIB || MICROSOFT_BCL_MEMORY + public +#else + internal +#endif + readonly struct Range : IEquatable { /// Represent the inclusive start index of the Range. public Index Start { get; } diff --git a/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/ComInterfaceGenerator.csproj b/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/ComInterfaceGenerator.csproj index 7284f2b9500d1..8d187cfa8b38a 100644 --- a/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/ComInterfaceGenerator.csproj +++ b/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/ComInterfaceGenerator.csproj @@ -26,11 +26,11 @@ + -