From dc54240c282e1a215794694fc449f59bbb198382 Mon Sep 17 00:00:00 2001 From: Cyrus Najmabadi Date: Wed, 10 Apr 2024 22:33:36 -0700 Subject: [PATCH 1/2] Simplify ForAttributeWithMetadataName --- ...lueProvider.ImmutableArrayValueComparer.cs | 35 ------------------- ...alueProvider_ForAttributeWithSimpleName.cs | 1 - 2 files changed, 36 deletions(-) delete mode 100644 src/Compilers/Core/Portable/SourceGeneration/Nodes/SyntaxValueProvider.ImmutableArrayValueComparer.cs diff --git a/src/Compilers/Core/Portable/SourceGeneration/Nodes/SyntaxValueProvider.ImmutableArrayValueComparer.cs b/src/Compilers/Core/Portable/SourceGeneration/Nodes/SyntaxValueProvider.ImmutableArrayValueComparer.cs deleted file mode 100644 index d1cadedcf503d..0000000000000 --- a/src/Compilers/Core/Portable/SourceGeneration/Nodes/SyntaxValueProvider.ImmutableArrayValueComparer.cs +++ /dev/null @@ -1,35 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - -using System.Collections.Generic; -using System.Collections.Immutable; -using System.Diagnostics.CodeAnalysis; -using Roslyn.Utilities; - -namespace Microsoft.CodeAnalysis; - -public partial struct SyntaxValueProvider -{ - private class ImmutableArrayValueComparer : IEqualityComparer> - { - public static readonly IEqualityComparer> Instance = new ImmutableArrayValueComparer(); - - public bool Equals(ImmutableArray x, ImmutableArray y) - { - if (x == y) - return true; - - return x.SequenceEqual(y, 0, static (a, b, _) => EqualityComparer.Default.Equals(a, b)); - } - - public int GetHashCode(ImmutableArray obj) - { - var hashCode = 0; - foreach (var value in obj) - hashCode = Hash.Combine(hashCode, EqualityComparer.Default.GetHashCode(value!)); - - return hashCode; - } - } -} diff --git a/src/Compilers/Core/Portable/SourceGeneration/Nodes/SyntaxValueProvider_ForAttributeWithSimpleName.cs b/src/Compilers/Core/Portable/SourceGeneration/Nodes/SyntaxValueProvider_ForAttributeWithSimpleName.cs index f2fa22112ded2..68faac2336808 100644 --- a/src/Compilers/Core/Portable/SourceGeneration/Nodes/SyntaxValueProvider_ForAttributeWithSimpleName.cs +++ b/src/Compilers/Core/Portable/SourceGeneration/Nodes/SyntaxValueProvider_ForAttributeWithSimpleName.cs @@ -76,7 +76,6 @@ public partial struct SyntaxValueProvider // file changes its global aliases or a file is added / removed from the compilation var collectedGlobalAliasesProvider = individualFileGlobalAliasesProvider .Collect() - .WithComparer(ImmutableArrayValueComparer.Instance) .WithTrackingName("collectedGlobalAliases_ForAttribute"); var allUpGlobalAliasesProvider = collectedGlobalAliasesProvider From d584ef96bbb1d4071b6835aa8261f1ec571fa439 Mon Sep 17 00:00:00 2001 From: Cyrus Najmabadi Date: Wed, 10 Apr 2024 22:47:04 -0700 Subject: [PATCH 2/2] Make static --- .../SyntaxValueProvider_ForAttributeWithSimpleName.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Compilers/Core/Portable/SourceGeneration/Nodes/SyntaxValueProvider_ForAttributeWithSimpleName.cs b/src/Compilers/Core/Portable/SourceGeneration/Nodes/SyntaxValueProvider_ForAttributeWithSimpleName.cs index 68faac2336808..e51c2e58e7320 100644 --- a/src/Compilers/Core/Portable/SourceGeneration/Nodes/SyntaxValueProvider_ForAttributeWithSimpleName.cs +++ b/src/Compilers/Core/Portable/SourceGeneration/Nodes/SyntaxValueProvider_ForAttributeWithSimpleName.cs @@ -68,7 +68,7 @@ public partial struct SyntaxValueProvider // Create a provider that provides (and updates) the global aliases for any particular file when it is edited. var individualFileGlobalAliasesProvider = syntaxTreesProvider - .Where((info, _) => info.Info.HasFlag(SourceGeneratorSyntaxTreeInfo.ContainsGlobalAliases)) + .Where(static (info, _) => info.Info.HasFlag(SourceGeneratorSyntaxTreeInfo.ContainsGlobalAliases)) .Select((info, cancellationToken) => getGlobalAliasesInCompilationUnit(syntaxHelper, info.Tree.GetRoot(cancellationToken))) .WithTrackingName("individualFileGlobalAliases_ForAttribute"); @@ -94,19 +94,19 @@ public partial struct SyntaxValueProvider allUpGlobalAliasesProvider = allUpGlobalAliasesProvider .Combine(compilationGlobalAliases) - .Select((tuple, _) => GlobalAliases.Concat(tuple.Left, tuple.Right)) + .Select(static (tuple, _) => GlobalAliases.Concat(tuple.Left, tuple.Right)) .WithTrackingName("allUpIncludingCompilationGlobalAliases_ForAttribute"); // Combine the two providers so that we reanalyze every file if the global aliases change, or we reanalyze a // particular file when it's compilation unit changes. var syntaxTreeAndGlobalAliasesProvider = syntaxTreesProvider - .Where((info, _) => info.Info.HasFlag(SourceGeneratorSyntaxTreeInfo.ContainsAttributeList)) + .Where(static (info, _) => info.Info.HasFlag(SourceGeneratorSyntaxTreeInfo.ContainsAttributeList)) .Combine(allUpGlobalAliasesProvider) .WithTrackingName("compilationUnitAndGlobalAliases_ForAttribute"); return syntaxTreeAndGlobalAliasesProvider .Select((tuple, c) => (tuple.Left.Tree, GetMatchingNodes(syntaxHelper, tuple.Right, tuple.Left.Tree, simpleName, predicate, c))) - .Where(tuple => tuple.Item2.Length > 0) + .Where(static tuple => tuple.Item2.Length > 0) .WithTrackingName("result_ForAttributeInternal"); static GlobalAliases getGlobalAliasesInCompilationUnit(