diff --git a/src/Workspaces/Core/Portable/FindSymbols/FindReferences/FindReferencesSearchEngine.cs b/src/Workspaces/Core/Portable/FindSymbols/FindReferences/FindReferencesSearchEngine.cs index 82c8473e56ab16b162dd2702e0efab02e321ad00..52eeef85e253443e2240300e08c942457830698a 100644 --- a/src/Workspaces/Core/Portable/FindSymbols/FindReferences/FindReferencesSearchEngine.cs +++ b/src/Workspaces/Core/Portable/FindSymbols/FindReferences/FindReferencesSearchEngine.cs @@ -2,6 +2,8 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +#nullable enable + using System; using System.Collections.Generic; using System.Collections.Immutable; @@ -22,7 +24,7 @@ namespace Microsoft.CodeAnalysis.FindSymbols internal partial class FindReferencesSearchEngine { private readonly Solution _solution; - private readonly IImmutableSet _documents; + private readonly IImmutableSet? _documents; private readonly ImmutableArray _finders; private readonly IStreamingProgressTracker _progressTracker; private readonly IStreamingFindReferencesProgress _progress; @@ -31,7 +33,7 @@ internal partial class FindReferencesSearchEngine public FindReferencesSearchEngine( Solution solution, - IImmutableSet documents, + IImmutableSet? documents, ImmutableArray finders, IStreamingFindReferencesProgress progress, FindReferencesSearchOptions options, diff --git a/src/Workspaces/Core/Portable/FindSymbols/FindReferences/FindReferencesSearchEngine_MapCreation.cs b/src/Workspaces/Core/Portable/FindSymbols/FindReferences/FindReferencesSearchEngine_MapCreation.cs index a581056b0ffa144ea8f0b9358d2c5e3bff6e7494..d0b2b24f50faf15fca8c3716558049afd70a50c1 100644 --- a/src/Workspaces/Core/Portable/FindSymbols/FindReferences/FindReferencesSearchEngine_MapCreation.cs +++ b/src/Workspaces/Core/Portable/FindSymbols/FindReferences/FindReferencesSearchEngine_MapCreation.cs @@ -126,7 +126,8 @@ private async Task CreateProjectMapAsync(ConcurrentSet symb searchSymbol = sourceSymbol; } - if (searchSymbol != null && result.Add(searchSymbol)) + Contract.ThrowIfNull(searchSymbol); + if (result.Add(searchSymbol)) { await _progress.OnDefinitionFoundAsync(searchSymbol).ConfigureAwait(false); @@ -135,12 +136,12 @@ private async Task CreateProjectMapAsync(ConcurrentSet symb _cancellationToken.ThrowIfCancellationRequested(); - var finderTasks = new List(); + using var _ = ArrayBuilder.GetInstance(out var finderTasks); foreach (var f in _finders) { finderTasks.Add(Task.Run(async () => { - var symbolTasks = new List(); + using var _ = ArrayBuilder.GetInstance(out var symbolTasks); var symbols = await f.DetermineCascadedSymbolsAsync( searchSymbol, _solution, projects, _options, _cancellationToken).ConfigureAwait(false); @@ -169,12 +170,13 @@ private async Task CreateProjectMapAsync(ConcurrentSet symb private void AddSymbolTasks( ConcurrentSet result, ImmutableArray symbols, - List symbolTasks) + ArrayBuilder symbolTasks) { if (!symbols.IsDefault) { foreach (var child in symbols) { + Contract.ThrowIfNull(child); _cancellationToken.ThrowIfCancellationRequested(); symbolTasks.Add(Task.Run(() => DetermineAllSymbolsCoreAsync(child, result), _cancellationToken)); } @@ -227,6 +229,7 @@ private static ISymbol MapToAppropriateSymbol(ISymbol symbol) searchSymbol = symbol.ContainingType; } + Contract.ThrowIfNull(searchSymbol); return searchSymbol; } } diff --git a/src/Workspaces/Core/Portable/FindSymbols/FindReferences/Finders/AbstractMemberScopedReferenceFinder.cs b/src/Workspaces/Core/Portable/FindSymbols/FindReferences/Finders/AbstractMemberScopedReferenceFinder.cs index 4307fd2a5c91398d3413c967138f9c877cb9eefe..7aebade206ab57b279982ba06dd249d008462dd0 100644 --- a/src/Workspaces/Core/Portable/FindSymbols/FindReferences/Finders/AbstractMemberScopedReferenceFinder.cs +++ b/src/Workspaces/Core/Portable/FindSymbols/FindReferences/Finders/AbstractMemberScopedReferenceFinder.cs @@ -2,6 +2,8 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +#nullable enable + using System; using System.Collections.Generic; using System.Collections.Immutable; @@ -62,7 +64,7 @@ protected sealed override bool CanFind(TSymbol symbol) if (symbol.ContainingType != null && symbol.ContainingType.IsScriptClass) { - var syntaxTree = await document.GetSyntaxTreeAsync(cancellationToken).ConfigureAwait(false); + var syntaxTree = await document.GetRequiredSyntaxTreeAsync(cancellationToken).ConfigureAwait(false); var syntaxFactsService = document.GetLanguageService(); var root = await syntaxTree.GetRootAsync(cancellationToken).ConfigureAwait(false); var tokens = root.DescendantTokens(); @@ -74,7 +76,7 @@ protected sealed override bool CanFind(TSymbol symbol) return ImmutableArray.Empty; } - private static ISymbol GetContainer(ISymbol symbol) + private static ISymbol? GetContainer(ISymbol symbol) { for (var current = symbol; current != null; current = current.ContainingSymbol) { @@ -126,11 +128,11 @@ private static ISymbol GetContainer(ISymbol symbol) Document document, SemanticModel semanticModel, IEnumerable tokens, - Func findParentNode, + Func? findParentNode, CancellationToken cancellationToken) { var name = symbol.Name; - var syntaxFacts = document.GetLanguageService(); + var syntaxFacts = document.GetRequiredLanguageService(); var symbolsMatch = GetStandardSymbolsMatchFunction(symbol, findParentNode, document.Project.Solution, cancellationToken); return FindReferencesInTokensAsync( @@ -159,15 +161,15 @@ private static ISymbol GetContainer(ISymbol symbol) ISymbol container, Document document, SemanticModel semanticModel, - Func findParentNode, + Func? findParentNode, CancellationToken cancellationToken) { - var service = document.GetLanguageService(); + var service = document.GetRequiredLanguageService(); var declarations = service.GetDeclarations(container); var tokens = declarations.SelectMany(r => r.GetSyntax(cancellationToken).DescendantTokens()); var name = symbol.Name; - var syntaxFacts = document.GetLanguageService(); + var syntaxFacts = document.GetRequiredLanguageService(); var symbolsMatch = GetStandardSymbolsMatchFunction(symbol, findParentNode, document.Project.Solution, cancellationToken); var tokensMatch = GetTokensMatchFunction(syntaxFacts, name); diff --git a/src/Workspaces/Core/Portable/FindSymbols/FindReferences/Finders/AbstractMethodOrPropertyOrEventSymbolReferenceFinder.cs b/src/Workspaces/Core/Portable/FindSymbols/FindReferences/Finders/AbstractMethodOrPropertyOrEventSymbolReferenceFinder.cs index 727fef48429bffe175983b82ba354d0192b251cf..a5ce028b3e105eceb479029b425afcdf4b548a58 100644 --- a/src/Workspaces/Core/Portable/FindSymbols/FindReferences/Finders/AbstractMethodOrPropertyOrEventSymbolReferenceFinder.cs +++ b/src/Workspaces/Core/Portable/FindSymbols/FindReferences/Finders/AbstractMethodOrPropertyOrEventSymbolReferenceFinder.cs @@ -2,10 +2,13 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +#nullable enable + using System.Collections.Immutable; using System.Threading; using System.Threading.Tasks; using Microsoft.CodeAnalysis.LanguageServices; +using Microsoft.CodeAnalysis.PooledObjects; using Microsoft.CodeAnalysis.Shared.Extensions; namespace Microsoft.CodeAnalysis.FindSymbols.Finders @@ -69,16 +72,22 @@ protected AbstractMethodOrPropertyOrEventSymbolReferenceFinder() // the only accessor method referenced in a foreach-statement is the .Current's // get-accessor - return ImmutableArray.Create(symbols.CurrentProperty.GetMethod); + return symbols.CurrentProperty.GetMethod == null + ? ImmutableArray.Empty + : ImmutableArray.Create(symbols.CurrentProperty.GetMethod); } if (semanticFacts.IsWrittenTo(model, node, cancellationToken)) { // if it was only written to, then only the setter was referenced. // if it was written *and* read, then both accessors were referenced. - return semanticFacts.IsOnlyWrittenTo(model, node, cancellationToken) - ? ImmutableArray.Create(property.SetMethod) - : ImmutableArray.Create(property.GetMethod, property.SetMethod); + using var _ = ArrayBuilder.GetInstance(out var result); + result.AddIfNotNull(property.SetMethod); + + if (!semanticFacts.IsOnlyWrittenTo(model, node, cancellationToken)) + result.AddIfNotNull(property.GetMethod); + + return result.ToImmutable(); } else { @@ -93,7 +102,7 @@ protected AbstractMethodOrPropertyOrEventSymbolReferenceFinder() var inNameOf = semanticFacts.IsInsideNameOfExpression(model, node, cancellationToken); var inStructuredTrivia = node.IsPartOfStructuredTrivia(); - return inNameOf || inStructuredTrivia + return inNameOf || inStructuredTrivia || property.GetMethod == null ? ImmutableArray.Empty : ImmutableArray.Create(property.GetMethod); } diff --git a/src/Workspaces/Core/Portable/FindSymbols/FindReferences/Finders/AbstractReferenceFinder_GlobalSuppressions.cs b/src/Workspaces/Core/Portable/FindSymbols/FindReferences/Finders/AbstractReferenceFinder_GlobalSuppressions.cs index 974e163b76d05e99caa5575d96bf80bf2bed0cdb..7ce0c53b0a4f5c13be27d69e70bce321e9759352 100644 --- a/src/Workspaces/Core/Portable/FindSymbols/FindReferences/Finders/AbstractReferenceFinder_GlobalSuppressions.cs +++ b/src/Workspaces/Core/Portable/FindSymbols/FindReferences/Finders/AbstractReferenceFinder_GlobalSuppressions.cs @@ -105,7 +105,7 @@ static bool SupportsGlobalSuppression(ISymbol symbol) if (IsCandidate(token, expectedDocCommentId, semanticModel, syntaxFacts, suppressMessageAttribute, cancellationToken, out var offsetOfReferenceInToken)) { var referenceLocation = CreateReferenceLocation(offsetOfReferenceInToken, token, root, document, syntaxFacts); - locations.Add(new FinderLocation(token.Parent, referenceLocation)); + locations.Add(new FinderLocation(token.GetRequiredParent(), referenceLocation)); } } diff --git a/src/Workspaces/Core/Portable/FindSymbols/FindReferences/Finders/ConstructorInitializerSymbolReferenceFinder.cs b/src/Workspaces/Core/Portable/FindSymbols/FindReferences/Finders/ConstructorInitializerSymbolReferenceFinder.cs index ce4e245b99530b0b1399944e863537145d890150..2f1848f4e24ce60266a81e0f117f8c718b0bc7bb 100644 --- a/src/Workspaces/Core/Portable/FindSymbols/FindReferences/Finders/ConstructorInitializerSymbolReferenceFinder.cs +++ b/src/Workspaces/Core/Portable/FindSymbols/FindReferences/Finders/ConstructorInitializerSymbolReferenceFinder.cs @@ -2,6 +2,8 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +#nullable enable + using System.Collections.Immutable; using System.Linq; using System.Threading; @@ -56,7 +58,7 @@ protected override bool CanFind(IMethodSymbol symbol) FindReferencesSearchOptions options, CancellationToken cancellationToken) { - var syntaxFactsService = document.GetLanguageService(); + var syntaxFactsService = document.GetRequiredLanguageService(); var typeName = methodSymbol.ContainingType.Name; var tokens = await document.GetConstructorInitializerTokensAsync(semanticModel, cancellationToken).ConfigureAwait(false); diff --git a/src/Workspaces/Core/Portable/FindSymbols/FindReferences/Finders/DestructorSymbolReferenceFinder.cs b/src/Workspaces/Core/Portable/FindSymbols/FindReferences/Finders/DestructorSymbolReferenceFinder.cs index 139048d7ad1f99c92e60547dbf527f327fdeb235..cdaf332d299214e74ff3e7c0a02952593922c5a6 100644 --- a/src/Workspaces/Core/Portable/FindSymbols/FindReferences/Finders/DestructorSymbolReferenceFinder.cs +++ b/src/Workspaces/Core/Portable/FindSymbols/FindReferences/Finders/DestructorSymbolReferenceFinder.cs @@ -2,6 +2,8 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +#nullable enable + using System.Collections.Immutable; using System.Threading; using System.Threading.Tasks; diff --git a/src/Workspaces/Core/Portable/FindSymbols/FindReferences/Finders/EventSymbolReferenceFinder.cs b/src/Workspaces/Core/Portable/FindSymbols/FindReferences/Finders/EventSymbolReferenceFinder.cs index d4657403470a54358a2092a6f8682d42c7b833c7..5f8f42437eef422e033d253d02c60620bda278ae 100644 --- a/src/Workspaces/Core/Portable/FindSymbols/FindReferences/Finders/EventSymbolReferenceFinder.cs +++ b/src/Workspaces/Core/Portable/FindSymbols/FindReferences/Finders/EventSymbolReferenceFinder.cs @@ -2,6 +2,8 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +#nullable enable + using System.Collections.Immutable; using System.Linq; using System.Threading; diff --git a/src/Workspaces/Core/Portable/FindSymbols/FindReferences/Finders/ExplicitInterfaceMethodReferenceFinder.cs b/src/Workspaces/Core/Portable/FindSymbols/FindReferences/Finders/ExplicitInterfaceMethodReferenceFinder.cs index 4fd2ec03545f285f792c7b84de22e1d79436375f..a9c1ec7fc77e9ea1eabb7057cbd793dcb356d6f3 100644 --- a/src/Workspaces/Core/Portable/FindSymbols/FindReferences/Finders/ExplicitInterfaceMethodReferenceFinder.cs +++ b/src/Workspaces/Core/Portable/FindSymbols/FindReferences/Finders/ExplicitInterfaceMethodReferenceFinder.cs @@ -2,6 +2,8 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +#nullable enable + using System.Collections.Immutable; using System.Threading; using System.Threading.Tasks; diff --git a/src/Workspaces/Core/Portable/FindSymbols/FindReferences/Finders/FieldSymbolReferenceFinder.cs b/src/Workspaces/Core/Portable/FindSymbols/FindReferences/Finders/FieldSymbolReferenceFinder.cs index edd4df5003a27956c5d1ee0e04cd07929c373f5f..282e705063fa2333e9fe9fa2dfe681cb790780f3 100644 --- a/src/Workspaces/Core/Portable/FindSymbols/FindReferences/Finders/FieldSymbolReferenceFinder.cs +++ b/src/Workspaces/Core/Portable/FindSymbols/FindReferences/Finders/FieldSymbolReferenceFinder.cs @@ -2,6 +2,8 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +#nullable enable + using System.Collections.Immutable; using System.Threading; using System.Threading.Tasks; diff --git a/src/Workspaces/Core/Portable/FindSymbols/FindReferences/Finders/FinderLocation.cs b/src/Workspaces/Core/Portable/FindSymbols/FindReferences/Finders/FinderLocation.cs index 7533d1f6590e33b5d00cd05461c768b0b434a881..6c9df71109d6e8a737177d498cffe8133a0460cc 100644 --- a/src/Workspaces/Core/Portable/FindSymbols/FindReferences/Finders/FinderLocation.cs +++ b/src/Workspaces/Core/Portable/FindSymbols/FindReferences/Finders/FinderLocation.cs @@ -2,6 +2,8 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +#nullable enable + namespace Microsoft.CodeAnalysis.FindSymbols.Finders { internal readonly struct FinderLocation diff --git a/src/Workspaces/Core/Portable/FindSymbols/FindReferences/Finders/ILanguageServiceReferenceFinder.cs b/src/Workspaces/Core/Portable/FindSymbols/FindReferences/Finders/ILanguageServiceReferenceFinder.cs index 737e35400968db9f186a2bd0b24c3f2aa12ba06a..f82aa5c1ecadf7fb8ea03c5eb937a2bf158dfc83 100644 --- a/src/Workspaces/Core/Portable/FindSymbols/FindReferences/Finders/ILanguageServiceReferenceFinder.cs +++ b/src/Workspaces/Core/Portable/FindSymbols/FindReferences/Finders/ILanguageServiceReferenceFinder.cs @@ -2,6 +2,8 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +#nullable enable + using System.Collections.Immutable; using System.Threading; using System.Threading.Tasks; diff --git a/src/Workspaces/Core/Portable/FindSymbols/FindReferences/Finders/IReferenceFinder.cs b/src/Workspaces/Core/Portable/FindSymbols/FindReferences/Finders/IReferenceFinder.cs index 5f41df416d4f1a9dca8dba7b3b5787c8192ea542..0ff8490f03c3c26d15bcb4df6745f1d90bea22c2 100644 --- a/src/Workspaces/Core/Portable/FindSymbols/FindReferences/Finders/IReferenceFinder.cs +++ b/src/Workspaces/Core/Portable/FindSymbols/FindReferences/Finders/IReferenceFinder.cs @@ -2,6 +2,8 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +#nullable enable + using System.Collections.Immutable; using System.Threading; using System.Threading.Tasks; diff --git a/src/Workspaces/Core/Portable/FindSymbols/FindReferences/Finders/LabelSymbolReferenceFinder.cs b/src/Workspaces/Core/Portable/FindSymbols/FindReferences/Finders/LabelSymbolReferenceFinder.cs index 711c28315217bb54ce6714fe04fbc29bbf28cee7..ccd46cfa7d43b33ed90931c3d879fe35d5cd938c 100644 --- a/src/Workspaces/Core/Portable/FindSymbols/FindReferences/Finders/LabelSymbolReferenceFinder.cs +++ b/src/Workspaces/Core/Portable/FindSymbols/FindReferences/Finders/LabelSymbolReferenceFinder.cs @@ -2,6 +2,8 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +#nullable enable + using System; using Microsoft.CodeAnalysis.LanguageServices; diff --git a/src/Workspaces/Core/Portable/FindSymbols/FindReferences/Finders/LinkedFileReferenceFinder.cs b/src/Workspaces/Core/Portable/FindSymbols/FindReferences/Finders/LinkedFileReferenceFinder.cs index bf1980ceebb15acab8ce7fea39708e9d6ff4e7d6..0887606e92ca5ef9f521406068f5a6a8f569a581 100644 --- a/src/Workspaces/Core/Portable/FindSymbols/FindReferences/Finders/LinkedFileReferenceFinder.cs +++ b/src/Workspaces/Core/Portable/FindSymbols/FindReferences/Finders/LinkedFileReferenceFinder.cs @@ -2,6 +2,8 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +#nullable enable + using System.Collections.Immutable; using System.Threading; using System.Threading.Tasks; @@ -25,7 +27,7 @@ internal class LinkedFileReferenceFinder : IReferenceFinder return SpecializedTasks.EmptyImmutableArray(); } - public Task> DetermineProjectsToSearchAsync(ISymbol symbol, Solution solution, IImmutableSet projects = null, CancellationToken cancellationToken = default) + public Task> DetermineProjectsToSearchAsync(ISymbol symbol, Solution solution, IImmutableSet? projects = null, CancellationToken cancellationToken = default) => SpecializedTasks.EmptyImmutableArray(); public Task> FindReferencesInDocumentAsync( diff --git a/src/Workspaces/Core/Portable/FindSymbols/FindReferences/Finders/LocalSymbolReferenceFinder.cs b/src/Workspaces/Core/Portable/FindSymbols/FindReferences/Finders/LocalSymbolReferenceFinder.cs index fd607c45b64c521e1b7aaf776a6d88fd854c907f..36073c0e43692285b5727b9548f05984a8b6dbaa 100644 --- a/src/Workspaces/Core/Portable/FindSymbols/FindReferences/Finders/LocalSymbolReferenceFinder.cs +++ b/src/Workspaces/Core/Portable/FindSymbols/FindReferences/Finders/LocalSymbolReferenceFinder.cs @@ -2,6 +2,8 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +#nullable enable + using System; using Microsoft.CodeAnalysis.LanguageServices; diff --git a/src/Workspaces/Core/Portable/FindSymbols/FindReferences/Finders/MethodTypeParameterSymbolReferenceFinder.cs b/src/Workspaces/Core/Portable/FindSymbols/FindReferences/Finders/MethodTypeParameterSymbolReferenceFinder.cs index 7c20fb117927152572c6702bef6757aea78e24e8..d3e5e537341fe326df4ea7a7c09fda07d8c7026c 100644 --- a/src/Workspaces/Core/Portable/FindSymbols/FindReferences/Finders/MethodTypeParameterSymbolReferenceFinder.cs +++ b/src/Workspaces/Core/Portable/FindSymbols/FindReferences/Finders/MethodTypeParameterSymbolReferenceFinder.cs @@ -2,6 +2,8 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +#nullable enable + using System.Collections.Immutable; using System.Threading; using System.Threading.Tasks; @@ -59,6 +61,7 @@ protected override bool CanFind(ITypeParameterSymbol symbol) // // Also, we only look for files that have the name of the owning type. This helps filter // down the set considerably. + Contract.ThrowIfNull(symbol.DeclaringMethod); return FindDocumentsAsync(project, documents, findInGlobalSuppressions: false, cancellationToken, symbol.Name, GetMemberNameWithoutInterfaceName(symbol.DeclaringMethod.Name), symbol.DeclaringMethod.ContainingType.Name); diff --git a/src/Workspaces/Core/Portable/FindSymbols/FindReferences/Finders/NamedTypeSymbolReferenceFinder.cs b/src/Workspaces/Core/Portable/FindSymbols/FindReferences/Finders/NamedTypeSymbolReferenceFinder.cs index b4c1fad7002405c6759217a74c3931b127d5fe87..6135e02087b0c00d0c5183f09de071e2be9ed9cf 100644 --- a/src/Workspaces/Core/Portable/FindSymbols/FindReferences/Finders/NamedTypeSymbolReferenceFinder.cs +++ b/src/Workspaces/Core/Portable/FindSymbols/FindReferences/Finders/NamedTypeSymbolReferenceFinder.cs @@ -2,6 +2,8 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +#nullable enable + using System.Collections.Immutable; using System.Linq; using System.Threading; @@ -53,9 +55,11 @@ protected override bool CanFind(INamedTypeSymbol symbol) FindReferencesSearchOptions options, CancellationToken cancellationToken) { + var syntaxFacts = project.LanguageServices.GetRequiredService(); + var documentsWithName = await FindDocumentsAsync(project, documents, findInGlobalSuppressions: true, cancellationToken, symbol.Name).ConfigureAwait(false); var documentsWithType = await FindDocumentsAsync(project, documents, symbol.SpecialType.ToPredefinedType(), cancellationToken).ConfigureAwait(false); - var documentsWithAttribute = TryGetNameWithoutAttributeSuffix(symbol.Name, project.LanguageServices.GetService(), out var simpleName) + var documentsWithAttribute = TryGetNameWithoutAttributeSuffix(symbol.Name, syntaxFacts, out var simpleName) ? await FindDocumentsAsync(project, documents, findInGlobalSuppressions: false, cancellationToken, simpleName).ConfigureAwait(false) : ImmutableArray.Empty; @@ -127,7 +131,7 @@ protected override bool CanFind(INamedTypeSymbol symbol) return SpecializedTasks.EmptyImmutableArray(); } - var syntaxFacts = document.GetLanguageService(); + var syntaxFacts = document.GetRequiredLanguageService(); return FindReferencesInDocumentAsync(document, semanticModel, t => IsPotentialReference(predefinedType, syntaxFacts, t), (t, m) => new ValueTask<(bool matched, CandidateReason reason)>((matched: true, reason: CandidateReason.None)), @@ -143,7 +147,7 @@ protected override bool CanFind(INamedTypeSymbol symbol) CancellationToken cancellationToken) { var symbolsMatch = GetStandardSymbolsMatchFunction(namedType, null, document.Project.Solution, cancellationToken); - var syntaxFacts = document.GetLanguageService(); + var syntaxFacts = document.GetRequiredLanguageService(); return TryGetNameWithoutAttributeSuffix(namedType.Name, syntaxFacts, out var simpleName) ? FindReferencesInDocumentUsingIdentifierAsync(simpleName, document, semanticModel, symbolsMatch, docCommentId: null, findInGlobalSuppressions: false, cancellationToken) diff --git a/src/Workspaces/Core/Portable/FindSymbols/FindReferences/Finders/NamespaceSymbolReferenceFinder.cs b/src/Workspaces/Core/Portable/FindSymbols/FindReferences/Finders/NamespaceSymbolReferenceFinder.cs index 4da2ebea6321f3f33f8f611d621cf5ba662b54ce..70851afd9d18137cdab1daee0fcf0cee7cf2ed7c 100644 --- a/src/Workspaces/Core/Portable/FindSymbols/FindReferences/Finders/NamespaceSymbolReferenceFinder.cs +++ b/src/Workspaces/Core/Portable/FindSymbols/FindReferences/Finders/NamespaceSymbolReferenceFinder.cs @@ -2,6 +2,8 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +#nullable enable + using System.Collections.Immutable; using System.Threading; using System.Threading.Tasks; @@ -42,7 +44,7 @@ private static string GetNamespaceIdentifierName(INamespaceSymbol symbol) CancellationToken cancellationToken) { var identifierName = GetNamespaceIdentifierName(symbol); - var syntaxFactsService = document.GetLanguageService(); + var syntaxFacts = document.GetRequiredLanguageService(); var tokens = await GetIdentifierOrGlobalNamespaceTokensWithTextAsync( document, semanticModel, identifierName, cancellationToken).ConfigureAwait(false); @@ -50,14 +52,14 @@ private static string GetNamespaceIdentifierName(INamespaceSymbol symbol) document, semanticModel, tokens, - t => syntaxFactsService.TextMatch(t.ValueText, identifierName), + t => syntaxFacts.TextMatch(t.ValueText, identifierName), cancellationToken).ConfigureAwait(false); var aliasReferences = await FindAliasReferencesAsync(nonAliasReferences, symbol, document, semanticModel, cancellationToken).ConfigureAwait(false); var suppressionReferences = ShouldFindReferencesInGlobalSuppressions(symbol, out var docCommentId) ? await FindReferencesInDocumentInsideGlobalSuppressionsAsync(document, semanticModel, - syntaxFactsService, docCommentId, cancellationToken).ConfigureAwait(false) + syntaxFacts, docCommentId, cancellationToken).ConfigureAwait(false) : ImmutableArray.Empty; return nonAliasReferences.Concat(aliasReferences).Concat(suppressionReferences); diff --git a/src/Workspaces/Core/Portable/FindSymbols/FindReferences/Finders/OperatorSymbolReferenceFinder.cs b/src/Workspaces/Core/Portable/FindSymbols/FindReferences/Finders/OperatorSymbolReferenceFinder.cs index cdc658e7f2519d87889277939c75a7f5966841eb..067e0cc6368b23263a75b31da990271a0a94f17b 100644 --- a/src/Workspaces/Core/Portable/FindSymbols/FindReferences/Finders/OperatorSymbolReferenceFinder.cs +++ b/src/Workspaces/Core/Portable/FindSymbols/FindReferences/Finders/OperatorSymbolReferenceFinder.cs @@ -2,6 +2,8 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +#nullable enable + using System.Collections.Immutable; using System.Threading; using System.Threading.Tasks; @@ -33,7 +35,7 @@ protected override bool CanFind(IMethodSymbol symbol) FindReferencesSearchOptions options, CancellationToken cancellationToken) { - var syntaxFacts = document.GetLanguageService(); + var syntaxFacts = document.GetRequiredLanguageService(); var op = symbol.GetPredefinedOperator(); return FindReferencesInDocumentAsync(symbol, document, semanticModel, t => diff --git a/src/Workspaces/Core/Portable/FindSymbols/FindReferences/Finders/OrdinaryMethodReferenceFinder.cs b/src/Workspaces/Core/Portable/FindSymbols/FindReferences/Finders/OrdinaryMethodReferenceFinder.cs index 353852a18aefeb46589a47808481ac99993183aa..d58d0e250aa2b759697a9711a3bf7ec6b8fb1bd6 100644 --- a/src/Workspaces/Core/Portable/FindSymbols/FindReferences/Finders/OrdinaryMethodReferenceFinder.cs +++ b/src/Workspaces/Core/Portable/FindSymbols/FindReferences/Finders/OrdinaryMethodReferenceFinder.cs @@ -2,6 +2,8 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +#nullable enable + using System.Collections.Immutable; using System.Threading; using System.Threading.Tasks; diff --git a/src/Workspaces/Core/Portable/FindSymbols/FindReferences/Finders/ParameterSymbolReferenceFinder.cs b/src/Workspaces/Core/Portable/FindSymbols/FindReferences/Finders/ParameterSymbolReferenceFinder.cs index 671e0a11c9300394146c5052b88161283a27ce7d..d3c490459c70d06340b87b055c2d6208b6166960 100644 --- a/src/Workspaces/Core/Portable/FindSymbols/FindReferences/Finders/ParameterSymbolReferenceFinder.cs +++ b/src/Workspaces/Core/Portable/FindSymbols/FindReferences/Finders/ParameterSymbolReferenceFinder.cs @@ -2,6 +2,8 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +#nullable enable + using System; using System.Collections.Immutable; using System.Linq; @@ -131,18 +133,18 @@ protected override bool CanFind(IParameterSymbol symbol) var document = solution.GetDocument(parameterNode.SyntaxTree); if (document != null) { - var semanticFacts = document.GetLanguageService(); + var semanticFacts = document.GetRequiredLanguageService(); if (semanticFacts.ExposesAnonymousFunctionParameterNames) { - var semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false); + var semanticModel = await document.GetRequiredSemanticModelAsync(cancellationToken).ConfigureAwait(false); var lambdaNode = parameter.ContainingSymbol.DeclaringSyntaxReferences.Select(r => r.GetSyntax(cancellationToken)).FirstOrDefault(); var convertedType = semanticModel.GetTypeInfo(lambdaNode, cancellationToken).ConvertedType; if (convertedType != null) { - var syntaxFactsService = document.GetLanguageService(); - var container = GetContainer(semanticModel, parameterNode, syntaxFactsService); + var syntaxFacts = document.GetRequiredLanguageService(); + var container = GetContainer(semanticModel, parameterNode, syntaxFacts); if (container != null) { CascadeBetweenAnonymousFunctionParameters( @@ -165,12 +167,12 @@ protected override bool CanFind(IParameterSymbol symbol) ArrayBuilder results, CancellationToken cancellationToken) { - var syntaxFacts = document.GetLanguageService(); + var syntaxFacts = document.GetRequiredLanguageService(); foreach (var token in container.DescendantTokens()) { if (IdentifiersMatch(syntaxFacts, parameter.Name, token)) { - var symbol = semanticModel.GetDeclaredSymbol(token.Parent, cancellationToken); + var symbol = semanticModel.GetDeclaredSymbol(token.GetRequiredParent(), cancellationToken); if (symbol is IParameterSymbol && symbol.ContainingSymbol.IsAnonymousFunction() && SignatureComparer.Instance.HaveSameSignatureAndConstraintsAndReturnTypeAndAccessors(parameter.ContainingSymbol, symbol.ContainingSymbol, syntaxFacts.IsCaseSensitive) && diff --git a/src/Workspaces/Core/Portable/FindSymbols/FindReferences/Finders/PropertyAccessorSymbolReferenceFinder.cs b/src/Workspaces/Core/Portable/FindSymbols/FindReferences/Finders/PropertyAccessorSymbolReferenceFinder.cs index 6c8ec8f31faf0a3bc327ce84ff94ddddc8224428..af65deb89b8ae29ea2248fc535640c6525fba988 100644 --- a/src/Workspaces/Core/Portable/FindSymbols/FindReferences/Finders/PropertyAccessorSymbolReferenceFinder.cs +++ b/src/Workspaces/Core/Portable/FindSymbols/FindReferences/Finders/PropertyAccessorSymbolReferenceFinder.cs @@ -2,6 +2,8 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +#nullable enable + using System.Collections.Immutable; using System.Threading; using System.Threading.Tasks; @@ -80,8 +82,8 @@ protected override bool CanFind(IMethodSymbol symbol) options.WithAssociatePropertyReferencesWithSpecificAccessor(false), cancellationToken).ConfigureAwait(false); - var syntaxFacts = document.GetLanguageService(); - var semanticFacts = document.GetLanguageService(); + var syntaxFacts = document.GetRequiredLanguageService(); + var semanticFacts = document.GetRequiredLanguageService(); var accessorReferences = propertyReferences.WhereAsArray( loc => diff --git a/src/Workspaces/Core/Portable/FindSymbols/FindReferences/Finders/PropertySymbolReferenceFinder.cs b/src/Workspaces/Core/Portable/FindSymbols/FindReferences/Finders/PropertySymbolReferenceFinder.cs index ba40abfe2f95b5fee6d72963ba41c50ba43f5af2..f9707985c67e0d6e499a4fc068af568695180cd1 100644 --- a/src/Workspaces/Core/Portable/FindSymbols/FindReferences/Finders/PropertySymbolReferenceFinder.cs +++ b/src/Workspaces/Core/Portable/FindSymbols/FindReferences/Finders/PropertySymbolReferenceFinder.cs @@ -95,8 +95,8 @@ private static bool IsForEachProperty(IPropertySymbol symbol) // We want to associate property references to a specific accessor (if an accessor // is being referenced). Check if this reference would match an accessor. If so, do // not add it. It will be added by PropertyAccessorSymbolReferenceFinder. - var syntaxFacts = document.GetLanguageService(); - var semanticFacts = document.GetLanguageService(); + var syntaxFacts = document.GetRequiredLanguageService(); + var semanticFacts = document.GetRequiredLanguageService(); nameReferences = nameReferences.WhereAsArray(loc => { diff --git a/src/Workspaces/Core/Portable/FindSymbols/FindReferences/Finders/RangeVariableSymbolReferenceFinder.cs b/src/Workspaces/Core/Portable/FindSymbols/FindReferences/Finders/RangeVariableSymbolReferenceFinder.cs index 0cdd77c27b40f921e524c2f941eb349cd598b573..af091b3951d59179e9b3a2c61626daf2cc83e8cf 100644 --- a/src/Workspaces/Core/Portable/FindSymbols/FindReferences/Finders/RangeVariableSymbolReferenceFinder.cs +++ b/src/Workspaces/Core/Portable/FindSymbols/FindReferences/Finders/RangeVariableSymbolReferenceFinder.cs @@ -2,6 +2,8 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +#nullable enable + using System; using Microsoft.CodeAnalysis.LanguageServices; diff --git a/src/Workspaces/Core/Portable/FindSymbols/FindReferences/Finders/ReferenceFinders.cs b/src/Workspaces/Core/Portable/FindSymbols/FindReferences/Finders/ReferenceFinders.cs index 0fc7695f182d2027aa4e9a793d01c08a68dc7951..11d4f178a6aec2acf36cdea80b68775807dd735d 100644 --- a/src/Workspaces/Core/Portable/FindSymbols/FindReferences/Finders/ReferenceFinders.cs +++ b/src/Workspaces/Core/Portable/FindSymbols/FindReferences/Finders/ReferenceFinders.cs @@ -2,6 +2,8 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +#nullable enable + using System.Collections.Immutable; namespace Microsoft.CodeAnalysis.FindSymbols.Finders diff --git a/src/Workspaces/Core/Portable/FindSymbols/FindReferences/Finders/TypeParameterSymbolReferenceFinder.cs b/src/Workspaces/Core/Portable/FindSymbols/FindReferences/Finders/TypeParameterSymbolReferenceFinder.cs index d12c260174c0a6b60971e0fad2fa2e229fd383e4..ec7aa4907f726a5d64294691549e40d770e1b373 100644 --- a/src/Workspaces/Core/Portable/FindSymbols/FindReferences/Finders/TypeParameterSymbolReferenceFinder.cs +++ b/src/Workspaces/Core/Portable/FindSymbols/FindReferences/Finders/TypeParameterSymbolReferenceFinder.cs @@ -2,6 +2,8 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +#nullable enable + using System.Collections.Immutable; using System.Threading; using System.Threading.Tasks; diff --git a/src/Workspaces/Core/Portable/FindSymbols/SymbolFinder_FindReferences_Current.cs b/src/Workspaces/Core/Portable/FindSymbols/SymbolFinder_FindReferences_Current.cs index 8fcb8ca396f7f59356b5e5c7ef819789fe693f2e..e649cfcff0a02e587acf8f7693e1263157dc65d1 100644 --- a/src/Workspaces/Core/Portable/FindSymbols/SymbolFinder_FindReferences_Current.cs +++ b/src/Workspaces/Core/Portable/FindSymbols/SymbolFinder_FindReferences_Current.cs @@ -58,7 +58,7 @@ public static partial class SymbolFinder } internal static Task FindReferencesInCurrentProcessAsync( - ISymbol symbolAndProjectId, + ISymbol symbol, Solution solution, IStreamingFindReferencesProgress progress, IImmutableSet documents, @@ -69,7 +69,7 @@ public static partial class SymbolFinder progress ??= NoOpStreamingFindReferencesProgress.Instance; var engine = new FindReferencesSearchEngine( solution, documents, finders, progress, options, cancellationToken); - return engine.FindReferencesAsync(symbolAndProjectId); + return engine.FindReferencesAsync(symbol); } } } diff --git a/src/Workspaces/Core/Portable/FindSymbols/SymbolFinder_FindRenamableReferences.cs b/src/Workspaces/Core/Portable/FindSymbols/SymbolFinder_FindRenamableReferences.cs index 98cea5ce586196a04a1f93ffc44b0b4a303fdc07..649a3a1a2aa3863e402e70aeae1dd0e3a9690738 100644 --- a/src/Workspaces/Core/Portable/FindSymbols/SymbolFinder_FindRenamableReferences.cs +++ b/src/Workspaces/Core/Portable/FindSymbols/SymbolFinder_FindRenamableReferences.cs @@ -2,6 +2,8 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +#nullable enable + using System.Collections.Immutable; using System.Threading; using System.Threading.Tasks; @@ -21,10 +23,9 @@ public static partial class SymbolFinder { var streamingProgress = new StreamingProgressCollector(); - IImmutableSet documents = null; var engine = new FindReferencesSearchEngine( solution, - documents, + documents: null, ReferenceFinders.DefaultRenameReferenceFinders, streamingProgress, FindReferencesSearchOptions.Default, diff --git a/src/Workspaces/SharedUtilitiesAndExtensions/Compiler/Core/Extensions/SyntaxTokenExtensions.cs b/src/Workspaces/SharedUtilitiesAndExtensions/Compiler/Core/Extensions/SyntaxTokenExtensions.cs index cd707fcc65d41a3872fb83e7181ea3111c94cd99..74708e058c07c7690f4e73b0f7a2389a8243b691 100644 --- a/src/Workspaces/SharedUtilitiesAndExtensions/Compiler/Core/Extensions/SyntaxTokenExtensions.cs +++ b/src/Workspaces/SharedUtilitiesAndExtensions/Compiler/Core/Extensions/SyntaxTokenExtensions.cs @@ -159,5 +159,8 @@ public static SyntaxToken With(this SyntaxToken token, SyntaxTriviaList leading, public static SyntaxTrivia[] GetTrivia(this IEnumerable tokens) => tokens.SelectMany(token => SyntaxNodeOrTokenExtensions.GetTrivia(token)).ToArray(); + + public static SyntaxNode GetRequiredParent(this SyntaxToken token) + => token.Parent ?? throw new InvalidOperationException("Token's parent was null"); } }