From d8184b7fe53886aacf736a5c4d36b8c9d7f5051e Mon Sep 17 00:00:00 2001 From: David Barbet Date: Fri, 24 May 2019 10:58:34 -0700 Subject: [PATCH] Update documentation/naming for retrieving collidable symbols. --- .../LanguageServices/CSharpSemanticFactsService.cs | 4 ++-- .../AbstractSemanticFactsService.cs | 9 +++++++-- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/Workspaces/CSharp/Portable/LanguageServices/CSharpSemanticFactsService.cs b/src/Workspaces/CSharp/Portable/LanguageServices/CSharpSemanticFactsService.cs index 74293eb2c8f..9296c06e0a2 100644 --- a/src/Workspaces/CSharp/Portable/LanguageServices/CSharpSemanticFactsService.cs +++ b/src/Workspaces/CSharp/Portable/LanguageServices/CSharpSemanticFactsService.cs @@ -26,7 +26,7 @@ private CSharpSemanticFactsService() { } - protected override IEnumerable GetUsedSymbols(SemanticModel semanticModel, SyntaxNode location, SyntaxNode container, CancellationToken cancellationToken) + protected override IEnumerable GetCollidableSymbols(SemanticModel semanticModel, SyntaxNode location, SyntaxNode container, CancellationToken cancellationToken) { // Get all the symbols visible to the current location. var visibleSymbols = semanticModel.LookupSymbols(location.SpanStart); @@ -36,7 +36,7 @@ protected override IEnumerable GetUsedSymbols(SemanticModel semanticMod // Walk through the enclosing block to find them, but avoid exploring local functions because // a) Visible local function symbols would be returned from LookupSymbols // (e.g. location is inside a local function or the local function method name). - // b) Local function symbols are not affected by outer variables and do not contribute to the outer scope. + // b) Symbols declared inside a local function do not cause collisions with symbols declared outside the local function, so avoid considering them. var symbolsInBlock = semanticModel.GetExistingSymbols(container, cancellationToken, descendInto: n => !n.IsKind(SyntaxKind.LocalFunctionStatement)); diff --git a/src/Workspaces/Core/Portable/LanguageServices/SemanticsFactsService/AbstractSemanticFactsService.cs b/src/Workspaces/Core/Portable/LanguageServices/SemanticsFactsService/AbstractSemanticFactsService.cs index 823bbeae7b8..f5c40d7b753 100644 --- a/src/Workspaces/Core/Portable/LanguageServices/SemanticsFactsService/AbstractSemanticFactsService.cs +++ b/src/Workspaces/Core/Portable/LanguageServices/SemanticsFactsService/AbstractSemanticFactsService.cs @@ -60,13 +60,18 @@ internal abstract class AbstractSemanticFactsService var container = containerOpt ?? location.AncestorsAndSelf().FirstOrDefault( a => syntaxFacts.IsExecutableBlock(a) || syntaxFacts.IsMethodBody(a)); - var candidates = GetUsedSymbols(semanticModel, location, container, cancellationToken); + var candidates = GetCollidableSymbols(semanticModel, location, container, cancellationToken); var filteredCandidates = filter != null ? candidates.Where(filter) : candidates; return GenerateUniqueName(baseName, filteredCandidates.Select(s => s.Name).Concat(usedNames)); } - protected virtual IEnumerable GetUsedSymbols(SemanticModel semanticModel, SyntaxNode location, SyntaxNode container, CancellationToken cancellationToken) + /// + /// Retrieves all symbols that could collide with a symbol at the specified location. + /// A symbol can possibly collide with the location if it is available to that location and/or + /// could cause a compiler error if its name is re-used at that location. + /// + protected virtual IEnumerable GetCollidableSymbols(SemanticModel semanticModel, SyntaxNode location, SyntaxNode container, CancellationToken cancellationToken) => semanticModel.LookupSymbols(location.SpanStart).Concat(semanticModel.GetExistingSymbols(container, cancellationToken)); private SyntaxToken GenerateUniqueName(string baseName, IEnumerable usedNames) -- GitLab