From f59f7a5ad3079debd19a9911e88e35c3c7b1db64 Mon Sep 17 00:00:00 2001 From: Stuart Turner Date: Sun, 25 Jun 2023 09:32:22 -0500 Subject: [PATCH] Hide extensions that conflict with newer .NET versions This is a squashed merge of PR #945 that: - closes #565 - closes #738 Co-authored-by: Atif Aziz --- MoreLinq.Test/AppendTest.cs | 4 + MoreLinq.Test/DistinctByTest.cs | 4 + MoreLinq.Test/Enumerable.cs | 5 + MoreLinq.Test/PrependTest.cs | 4 + MoreLinq.Test/SkipLastTest.cs | 4 + MoreLinq.Test/TakeLastTest.cs | 4 + MoreLinq/Append.cs | 4 + MoreLinq/CompatibilitySuppressions.xml | 112 ++++++++++++++++++ MoreLinq/DistinctBy.cs | 12 +- MoreLinq/Prepend.cs | 4 + .../PublicAPI/net6.0/PublicAPI.Unshipped.txt | 16 +++ .../netstandard2.0/PublicAPI.Unshipped.txt | 4 + .../netstandard2.1/PublicAPI.Unshipped.txt | 12 ++ MoreLinq/SkipLast.cs | 4 + MoreLinq/TakeLast.cs | 4 + MoreLinq/ToHashSet.cs | 10 +- bld/ExtensionsGenerator/Program.cs | 11 +- 17 files changed, 215 insertions(+), 3 deletions(-) diff --git a/MoreLinq.Test/AppendTest.cs b/MoreLinq.Test/AppendTest.cs index 736ba6498..55ff31597 100644 --- a/MoreLinq.Test/AppendTest.cs +++ b/MoreLinq.Test/AppendTest.cs @@ -15,6 +15,8 @@ // limitations under the License. #endregion +#if !NET471_OR_GREATER && !NETSTANDARD1_6_OR_GREATER && !NETCOREAPP2_0_OR_GREATER + namespace MoreLinq.Test { using System.Collections.Generic; @@ -90,3 +92,5 @@ public void AppendWithSharedSource() } } } + +#endif diff --git a/MoreLinq.Test/DistinctByTest.cs b/MoreLinq.Test/DistinctByTest.cs index 7035578f2..377c9a318 100644 --- a/MoreLinq.Test/DistinctByTest.cs +++ b/MoreLinq.Test/DistinctByTest.cs @@ -15,6 +15,8 @@ // limitations under the License. #endregion +#if !NET6_0_OR_GREATER + namespace MoreLinq.Test { using System; @@ -61,3 +63,5 @@ public void DistinctByIsLazyWithComparer() } } } + +#endif diff --git a/MoreLinq.Test/Enumerable.cs b/MoreLinq.Test/Enumerable.cs index f7795af4e..ae22e31cd 100644 --- a/MoreLinq.Test/Enumerable.cs +++ b/MoreLinq.Test/Enumerable.cs @@ -45,6 +45,11 @@ public static bool Any(this IEnumerable source) => public static bool Any(this IEnumerable source, Func predicate) => LinqEnumerable.Any(source, predicate); +#if NET471_OR_GREATER || NET6_0_OR_GREATER + public static IEnumerable Append (this IEnumerable source, TSource element) => + LinqEnumerable.Append(source, element); +#endif + public static IEnumerable AsEnumerable(this IEnumerable source) => LinqEnumerable.AsEnumerable(source); diff --git a/MoreLinq.Test/PrependTest.cs b/MoreLinq.Test/PrependTest.cs index eaaad5c81..3a4f938d1 100644 --- a/MoreLinq.Test/PrependTest.cs +++ b/MoreLinq.Test/PrependTest.cs @@ -15,6 +15,8 @@ // limitations under the License. #endregion +#if !NET471_OR_GREATER && !NETSTANDARD1_6_OR_GREATER && !NETCOREAPP2_0_OR_GREATER + namespace MoreLinq.Test { using System.Collections.Generic; @@ -89,3 +91,5 @@ public void PrependWithSharedSource() } } } + +#endif diff --git a/MoreLinq.Test/SkipLastTest.cs b/MoreLinq.Test/SkipLastTest.cs index df7b19680..f6bbf7796 100644 --- a/MoreLinq.Test/SkipLastTest.cs +++ b/MoreLinq.Test/SkipLastTest.cs @@ -15,6 +15,8 @@ // limitations under the License. #endregion +#if !NETSTANDARD2_1 && !NETCOREAPP2_0_OR_GREATER + namespace MoreLinq.Test { using System.Collections.Generic; @@ -68,3 +70,5 @@ public void SkipLastUsesCollectionCountAtIterationTime() } } } + +#endif diff --git a/MoreLinq.Test/TakeLastTest.cs b/MoreLinq.Test/TakeLastTest.cs index 5779f4cfc..fc0347bf6 100644 --- a/MoreLinq.Test/TakeLastTest.cs +++ b/MoreLinq.Test/TakeLastTest.cs @@ -15,6 +15,8 @@ // limitations under the License. #endregion +#if !NETSTANDARD2_1 && !NETCOREAPP2_0_OR_GREATER + namespace MoreLinq.Test { using NUnit.Framework; @@ -89,3 +91,5 @@ static void AssertTakeLast(ICollection input, int count, ActionA sequence consisting of the head elements and the given tail element. /// This operator uses deferred execution and streams its results. +#if NET471_OR_GREATER || NETSTANDARD1_6_OR_GREATER || NETCOREAPP2_0_OR_GREATER + public static IEnumerable Append(IEnumerable head, T tail) +#else public static IEnumerable Append(this IEnumerable head, T tail) +#endif { if (head == null) throw new ArgumentNullException(nameof(head)); return head is PendNode node diff --git a/MoreLinq/CompatibilitySuppressions.xml b/MoreLinq/CompatibilitySuppressions.xml index da1602414..227399b89 100644 --- a/MoreLinq/CompatibilitySuppressions.xml +++ b/MoreLinq/CompatibilitySuppressions.xml @@ -1,6 +1,69 @@ + + CP0001 + T:MoreLinq.Extensions.AppendExtension + lib/net462/MoreLinq.dll + lib/netstandard2.0/MoreLinq.dll + true + + + CP0001 + T:MoreLinq.Extensions.PrependExtension + lib/net462/MoreLinq.dll + lib/netstandard2.0/MoreLinq.dll + true + + + CP0001 + T:MoreLinq.Extensions.AppendExtension + lib/net6.0/MoreLinq.dll + lib/net6.0/MoreLinq.dll + true + + + CP0001 + T:MoreLinq.Extensions.PrependExtension + lib/net6.0/MoreLinq.dll + lib/net6.0/MoreLinq.dll + true + + + CP0001 + T:MoreLinq.Extensions.AppendExtension + lib/netstandard2.0/MoreLinq.dll + lib/netstandard2.0/MoreLinq.dll + true + + + CP0001 + T:MoreLinq.Extensions.PrependExtension + lib/netstandard2.0/MoreLinq.dll + lib/netstandard2.0/MoreLinq.dll + true + + + CP0001 + T:MoreLinq.Extensions.AppendExtension + lib/netstandard2.1/MoreLinq.dll + lib/netstandard2.1/MoreLinq.dll + true + + + CP0001 + T:MoreLinq.Extensions.PrependExtension + lib/netstandard2.1/MoreLinq.dll + lib/netstandard2.1/MoreLinq.dll + true + + + CP0002 + M:MoreLinq.MoreEnumerable.Append``1(System.Collections.Generic.IEnumerable{``0},``0) + lib/net462/MoreLinq.dll + lib/netstandard2.0/MoreLinq.dll + true + CP0002 M:MoreLinq.MoreEnumerable.Concat``1(System.Collections.Generic.IEnumerable{``0},``0) @@ -8,6 +71,20 @@ lib/netstandard2.0/MoreLinq.dll true + + CP0002 + M:MoreLinq.MoreEnumerable.Prepend``1(System.Collections.Generic.IEnumerable{``0},``0) + lib/net462/MoreLinq.dll + lib/netstandard2.0/MoreLinq.dll + true + + + CP0002 + M:MoreLinq.MoreEnumerable.Append``1(System.Collections.Generic.IEnumerable{``0},``0) + lib/net6.0/MoreLinq.dll + lib/net6.0/MoreLinq.dll + true + CP0002 M:MoreLinq.MoreEnumerable.Concat``1(System.Collections.Generic.IEnumerable{``0},``0) @@ -15,6 +92,20 @@ lib/net6.0/MoreLinq.dll true + + CP0002 + M:MoreLinq.MoreEnumerable.Prepend``1(System.Collections.Generic.IEnumerable{``0},``0) + lib/net6.0/MoreLinq.dll + lib/net6.0/MoreLinq.dll + true + + + CP0002 + M:MoreLinq.MoreEnumerable.Append``1(System.Collections.Generic.IEnumerable{``0},``0) + lib/netstandard2.0/MoreLinq.dll + lib/netstandard2.0/MoreLinq.dll + true + CP0002 M:MoreLinq.MoreEnumerable.Concat``1(System.Collections.Generic.IEnumerable{``0},``0) @@ -22,6 +113,20 @@ lib/netstandard2.0/MoreLinq.dll true + + CP0002 + M:MoreLinq.MoreEnumerable.Prepend``1(System.Collections.Generic.IEnumerable{``0},``0) + lib/netstandard2.0/MoreLinq.dll + lib/netstandard2.0/MoreLinq.dll + true + + + CP0002 + M:MoreLinq.MoreEnumerable.Append``1(System.Collections.Generic.IEnumerable{``0},``0) + lib/netstandard2.1/MoreLinq.dll + lib/netstandard2.1/MoreLinq.dll + true + CP0002 M:MoreLinq.MoreEnumerable.Concat``1(System.Collections.Generic.IEnumerable{``0},``0) @@ -29,6 +134,13 @@ lib/netstandard2.1/MoreLinq.dll true + + CP0002 + M:MoreLinq.MoreEnumerable.Prepend``1(System.Collections.Generic.IEnumerable{``0},``0) + lib/netstandard2.1/MoreLinq.dll + lib/netstandard2.1/MoreLinq.dll + true + PKV006 .NETStandard,Version=v1.0 diff --git a/MoreLinq/DistinctBy.cs b/MoreLinq/DistinctBy.cs index a05faab82..1079ff814 100644 --- a/MoreLinq/DistinctBy.cs +++ b/MoreLinq/DistinctBy.cs @@ -38,10 +38,15 @@ static partial class MoreEnumerable /// A sequence consisting of distinct elements from the source sequence, /// comparing them by the specified key projection. +#if NET6_0_OR_GREATER + public static IEnumerable DistinctBy(IEnumerable source, + Func keySelector) +#else public static IEnumerable DistinctBy(this IEnumerable source, Func keySelector) +#endif { - return source.DistinctBy(keySelector, null); + return DistinctBy(source, keySelector, null); } /// @@ -62,8 +67,13 @@ public static IEnumerable DistinctBy(this IEnumerableA sequence consisting of distinct elements from the source sequence, /// comparing them by the specified key projection. +#if NET6_0_OR_GREATER + public static IEnumerable DistinctBy(IEnumerable source, + Func keySelector, IEqualityComparer? comparer) +#else public static IEnumerable DistinctBy(this IEnumerable source, Func keySelector, IEqualityComparer? comparer) +#endif { if (source == null) throw new ArgumentNullException(nameof(source)); if (keySelector == null) throw new ArgumentNullException(nameof(keySelector)); diff --git a/MoreLinq/Prepend.cs b/MoreLinq/Prepend.cs index 06f5a0f27..fb82e8538 100644 --- a/MoreLinq/Prepend.cs +++ b/MoreLinq/Prepend.cs @@ -41,7 +41,11 @@ static partial class MoreEnumerable /// The result variable, when iterated over, will yield /// 0, 1, 2 and 3, in turn. +#if NET471_OR_GREATER || NETSTANDARD1_6_OR_GREATER || NETCOREAPP2_0_OR_GREATER + public static IEnumerable Prepend(IEnumerable source, TSource value) +#else public static IEnumerable Prepend(this IEnumerable source, TSource value) +#endif { if (source == null) throw new ArgumentNullException(nameof(source)); return source is PendNode node diff --git a/MoreLinq/PublicAPI/net6.0/PublicAPI.Unshipped.txt b/MoreLinq/PublicAPI/net6.0/PublicAPI.Unshipped.txt index b4ddb014c..8959c7eec 100644 --- a/MoreLinq/PublicAPI/net6.0/PublicAPI.Unshipped.txt +++ b/MoreLinq/PublicAPI/net6.0/PublicAPI.Unshipped.txt @@ -1,2 +1,18 @@ #nullable enable +*REMOVED*static MoreLinq.MoreEnumerable.Append(this System.Collections.Generic.IEnumerable! head, T tail) -> System.Collections.Generic.IEnumerable! *REMOVED*static MoreLinq.MoreEnumerable.Concat(this System.Collections.Generic.IEnumerable! head, T tail) -> System.Collections.Generic.IEnumerable! +*REMOVED*static MoreLinq.MoreEnumerable.Prepend(this System.Collections.Generic.IEnumerable! source, TSource value) -> System.Collections.Generic.IEnumerable! +*REMOVED*static MoreLinq.MoreEnumerable.DistinctBy(this System.Collections.Generic.IEnumerable! source, System.Func! keySelector) -> System.Collections.Generic.IEnumerable! +*REMOVED*static MoreLinq.MoreEnumerable.DistinctBy(this System.Collections.Generic.IEnumerable! source, System.Func! keySelector, System.Collections.Generic.IEqualityComparer? comparer) -> System.Collections.Generic.IEnumerable! +*REMOVED*static MoreLinq.MoreEnumerable.SkipLast(this System.Collections.Generic.IEnumerable! source, int count) -> System.Collections.Generic.IEnumerable! +*REMOVED*static MoreLinq.MoreEnumerable.TakeLast(this System.Collections.Generic.IEnumerable! source, int count) -> System.Collections.Generic.IEnumerable! +*REMOVED*static MoreLinq.MoreEnumerable.ToHashSet(this System.Collections.Generic.IEnumerable! source) -> System.Collections.Generic.HashSet! +*REMOVED*static MoreLinq.MoreEnumerable.ToHashSet(this System.Collections.Generic.IEnumerable! source, System.Collections.Generic.IEqualityComparer? comparer) -> System.Collections.Generic.HashSet! +static MoreLinq.MoreEnumerable.Append(System.Collections.Generic.IEnumerable! head, T tail) -> System.Collections.Generic.IEnumerable! +static MoreLinq.MoreEnumerable.DistinctBy(System.Collections.Generic.IEnumerable! source, System.Func! keySelector) -> System.Collections.Generic.IEnumerable! +static MoreLinq.MoreEnumerable.DistinctBy(System.Collections.Generic.IEnumerable! source, System.Func! keySelector, System.Collections.Generic.IEqualityComparer? comparer) -> System.Collections.Generic.IEnumerable! +static MoreLinq.MoreEnumerable.Prepend(System.Collections.Generic.IEnumerable! source, TSource value) -> System.Collections.Generic.IEnumerable! +static MoreLinq.MoreEnumerable.SkipLast(System.Collections.Generic.IEnumerable! source, int count) -> System.Collections.Generic.IEnumerable! +static MoreLinq.MoreEnumerable.TakeLast(System.Collections.Generic.IEnumerable! source, int count) -> System.Collections.Generic.IEnumerable! +static MoreLinq.MoreEnumerable.ToHashSet(System.Collections.Generic.IEnumerable! source) -> System.Collections.Generic.HashSet! +static MoreLinq.MoreEnumerable.ToHashSet(System.Collections.Generic.IEnumerable! source, System.Collections.Generic.IEqualityComparer? comparer) -> System.Collections.Generic.HashSet! diff --git a/MoreLinq/PublicAPI/netstandard2.0/PublicAPI.Unshipped.txt b/MoreLinq/PublicAPI/netstandard2.0/PublicAPI.Unshipped.txt index b4ddb014c..056e1801b 100644 --- a/MoreLinq/PublicAPI/netstandard2.0/PublicAPI.Unshipped.txt +++ b/MoreLinq/PublicAPI/netstandard2.0/PublicAPI.Unshipped.txt @@ -1,2 +1,6 @@ #nullable enable +*REMOVED*static MoreLinq.MoreEnumerable.Append(this System.Collections.Generic.IEnumerable! head, T tail) -> System.Collections.Generic.IEnumerable! *REMOVED*static MoreLinq.MoreEnumerable.Concat(this System.Collections.Generic.IEnumerable! head, T tail) -> System.Collections.Generic.IEnumerable! +*REMOVED*static MoreLinq.MoreEnumerable.Prepend(this System.Collections.Generic.IEnumerable! source, TSource value) -> System.Collections.Generic.IEnumerable! +static MoreLinq.MoreEnumerable.Append(System.Collections.Generic.IEnumerable! head, T tail) -> System.Collections.Generic.IEnumerable! +static MoreLinq.MoreEnumerable.Prepend(System.Collections.Generic.IEnumerable! source, TSource value) -> System.Collections.Generic.IEnumerable! diff --git a/MoreLinq/PublicAPI/netstandard2.1/PublicAPI.Unshipped.txt b/MoreLinq/PublicAPI/netstandard2.1/PublicAPI.Unshipped.txt index b4ddb014c..b025e7aed 100644 --- a/MoreLinq/PublicAPI/netstandard2.1/PublicAPI.Unshipped.txt +++ b/MoreLinq/PublicAPI/netstandard2.1/PublicAPI.Unshipped.txt @@ -1,2 +1,14 @@ #nullable enable +*REMOVED*static MoreLinq.MoreEnumerable.Append(this System.Collections.Generic.IEnumerable! head, T tail) -> System.Collections.Generic.IEnumerable! *REMOVED*static MoreLinq.MoreEnumerable.Concat(this System.Collections.Generic.IEnumerable! head, T tail) -> System.Collections.Generic.IEnumerable! +*REMOVED*static MoreLinq.MoreEnumerable.Prepend(this System.Collections.Generic.IEnumerable! source, TSource value) -> System.Collections.Generic.IEnumerable! +*REMOVED*static MoreLinq.MoreEnumerable.SkipLast(this System.Collections.Generic.IEnumerable! source, int count) -> System.Collections.Generic.IEnumerable! +*REMOVED*static MoreLinq.MoreEnumerable.TakeLast(this System.Collections.Generic.IEnumerable! source, int count) -> System.Collections.Generic.IEnumerable! +*REMOVED*static MoreLinq.MoreEnumerable.ToHashSet(this System.Collections.Generic.IEnumerable! source) -> System.Collections.Generic.HashSet! +*REMOVED*static MoreLinq.MoreEnumerable.ToHashSet(this System.Collections.Generic.IEnumerable! source, System.Collections.Generic.IEqualityComparer? comparer) -> System.Collections.Generic.HashSet! +static MoreLinq.MoreEnumerable.Append(System.Collections.Generic.IEnumerable! head, T tail) -> System.Collections.Generic.IEnumerable! +static MoreLinq.MoreEnumerable.Prepend(System.Collections.Generic.IEnumerable! source, TSource value) -> System.Collections.Generic.IEnumerable! +static MoreLinq.MoreEnumerable.SkipLast(System.Collections.Generic.IEnumerable! source, int count) -> System.Collections.Generic.IEnumerable! +static MoreLinq.MoreEnumerable.TakeLast(System.Collections.Generic.IEnumerable! source, int count) -> System.Collections.Generic.IEnumerable! +static MoreLinq.MoreEnumerable.ToHashSet(System.Collections.Generic.IEnumerable! source) -> System.Collections.Generic.HashSet! +static MoreLinq.MoreEnumerable.ToHashSet(System.Collections.Generic.IEnumerable! source, System.Collections.Generic.IEqualityComparer? comparer) -> System.Collections.Generic.HashSet! diff --git a/MoreLinq/SkipLast.cs b/MoreLinq/SkipLast.cs index 0483f3e74..c88cec5e4 100644 --- a/MoreLinq/SkipLast.cs +++ b/MoreLinq/SkipLast.cs @@ -33,7 +33,11 @@ static partial class MoreEnumerable /// An containing the source sequence elements except for the bypassed ones at the end. /// +#if NETSTANDARD2_1 || NETCOREAPP2_0_OR_GREATER + public static IEnumerable SkipLast(IEnumerable source, int count) +#else public static IEnumerable SkipLast(this IEnumerable source, int count) +#endif { if (source == null) throw new ArgumentNullException(nameof(source)); diff --git a/MoreLinq/TakeLast.cs b/MoreLinq/TakeLast.cs index 6619ff196..88eac7a8e 100644 --- a/MoreLinq/TakeLast.cs +++ b/MoreLinq/TakeLast.cs @@ -46,7 +46,11 @@ static partial class MoreEnumerable /// 56 and 78 in turn. /// +#if NETSTANDARD2_1 || NETCOREAPP2_0_OR_GREATER + public static IEnumerable TakeLast(IEnumerable source, int count) +#else public static IEnumerable TakeLast(this IEnumerable source, int count) +#endif { if (source == null) throw new ArgumentNullException(nameof(source)); diff --git a/MoreLinq/ToHashSet.cs b/MoreLinq/ToHashSet.cs index 7301ed357..1f6dc8bd1 100644 --- a/MoreLinq/ToHashSet.cs +++ b/MoreLinq/ToHashSet.cs @@ -35,9 +35,13 @@ static partial class MoreEnumerable /// This evaluates the input sequence completely. /// +#if NETSTANDARD2_1 || NET472_OR_GREATER || NETCOREAPP2_0_OR_GREATER + public static HashSet ToHashSet(IEnumerable source) +#else public static HashSet ToHashSet(this IEnumerable source) +#endif { - return source.ToHashSet(null); + return ToHashSet(source, null); } /// @@ -53,7 +57,11 @@ public static HashSet ToHashSet(this IEnumerable sour /// This evaluates the input sequence completely. /// +#if NETSTANDARD2_1 || NET472_OR_GREATER || NETCOREAPP2_0_OR_GREATER + public static HashSet ToHashSet(IEnumerable source, IEqualityComparer? comparer) +#else public static HashSet ToHashSet(this IEnumerable source, IEqualityComparer? comparer) +#endif { if (source == null) throw new ArgumentNullException(nameof(source)); return new HashSet(source, comparer); diff --git a/bld/ExtensionsGenerator/Program.cs b/bld/ExtensionsGenerator/Program.cs index f9cc083c2..87dba483d 100644 --- a/bld/ExtensionsGenerator/Program.cs +++ b/bld/ExtensionsGenerator/Program.cs @@ -235,7 +235,16 @@ from md in g select MethodDeclaration(md.ReturnType, md.Identifier) .WithAttributeLists(md.AttributeLists) - .WithModifiers(md.Modifiers) + .WithModifiers( + TokenList(md.Modifiers[0] // assume at least one modifier, like public + .WithLeadingTrivia( + from lt in md.Modifiers[0].LeadingTrivia + where lt.Kind() is not (SyntaxKind.DisabledTextTrivia + or SyntaxKind.IfDirectiveTrivia + or SyntaxKind.ElseDirectiveTrivia + or SyntaxKind.EndIfDirectiveTrivia) + select lt)) + .AddRange(md.Modifiers.Skip(1))) .WithTypeParameterList(md.TypeParameterList) .WithConstraintClauses(md.ConstraintClauses) .WithParameterList(md.ParameterList)