Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix find refs doing too much work lookign for types that had an alias to them in one file. #74015

Merged
merged 5 commits into from
Jun 16, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,7 @@ async ValueTask PerformSearchInDocumentAsync(

foreach (var symbol in symbols)
{
var state = new FindReferencesDocumentState(
cache, TryGet(symbolToGlobalAliases, symbol));

var state = new FindReferencesDocumentState(cache, TryGet(symbolToGlobalAliases, symbol));
await PerformSearchInDocumentWorkerAsync(symbol, state).ConfigureAwait(false);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,22 +98,17 @@ protected override void FindReferencesInDocument<TData>(
CancellationToken cancellationToken)
{
// First just look for this normal constructor references using the name of it's containing type.
var name = methodSymbol.ContainingType.Name;
var containingType = methodSymbol.ContainingType;
var containingTypeName = containingType.Name;
AddReferencesInDocumentWorker(
methodSymbol, name, state, processResult, processResultData, cancellationToken);
methodSymbol, containingTypeName, state, processResult, processResultData, cancellationToken);

// Next, look for constructor references through a global alias to our containing type.
foreach (var globalAlias in state.GlobalAliases)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we both want to search for references to global aliases anywhere that might be to our symbol, and...

{
// ignore the cases where the global alias might match the type name (i.e.
// global alias Console = System.Console). We'll already find those references
// above.
if (state.SyntaxFacts.StringComparer.Equals(name, globalAlias))
continue;
FindReferenceToAlias(methodSymbol, state, processResult, processResultData, containingTypeName, globalAlias, cancellationToken);

AddReferencesInDocumentWorker(
methodSymbol, globalAlias, state, processResult, processResultData, cancellationToken);
}
foreach (var localAlias in state.Cache.SyntaxTreeIndex.GetAliases(containingTypeName, containingType.Arity))
FindReferenceToAlias(methodSymbol, state, processResult, processResultData, containingTypeName, localAlias, cancellationToken);
Copy link
Member Author

@CyrusNajmabadi CyrusNajmabadi Jun 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

... we want to search for local aliases in this file alone to our symbol.


// Finally, look for constructor references to predefined types (like `new int()`),
// implicit object references, and inside global suppression attributes.
Expand All @@ -127,6 +122,19 @@ protected override void FindReferencesInDocument<TData>(
methodSymbol, state, processResult, processResultData, cancellationToken);
}

private static void FindReferenceToAlias<TData>(
IMethodSymbol methodSymbol, FindReferencesDocumentState state, Action<FinderLocation, TData> processResult, TData processResultData, string name, string alias, CancellationToken cancellationToken)
{
// ignore the cases where the global alias might match the type name (i.e.
// global alias Console = System.Console). We'll already find those references
// above.
if (state.SyntaxFacts.StringComparer.Equals(name, alias))
return;

AddReferencesInDocumentWorker(
methodSymbol, alias, state, processResult, processResultData, cancellationToken);
}

/// <summary>
/// Finds references to <paramref name="symbol"/> in this <paramref name="state"/>, but only if it referenced
/// though <paramref name="name"/> (which might be the actual name of the type, or a global alias to it).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,16 +147,23 @@ internal static void AddReferencesToTypeOrGlobalAliasToIt<TData>(
namedType, namedType.Name, state, processResult, processResultData, cancellationToken);

foreach (var globalAlias in state.GlobalAliases)
{
// ignore the cases where the global alias might match the type name (i.e.
// global alias Console = System.Console). We'll already find those references
// above.
if (state.SyntaxFacts.StringComparer.Equals(namedType.Name, globalAlias))
continue;

AddNonAliasReferences(
namedType, globalAlias, state, processResult, processResultData, cancellationToken);
}
FindReferenceToAlias(namedType, state, processResult, processResultData, globalAlias, cancellationToken);

foreach (var localAlias in state.Cache.SyntaxTreeIndex.GetAliases(namedType.Name, namedType.Arity))
FindReferenceToAlias(namedType, state, processResult, processResultData, localAlias, cancellationToken);
}

private static void FindReferenceToAlias<TData>(
INamedTypeSymbol namedType, FindReferencesDocumentState state, Action<FinderLocation, TData> processResult, TData processResultData, string alias, CancellationToken cancellationToken)
{
// ignore the cases where the global alias might match the type name (i.e.
// global alias Console = System.Console). We'll already find those references
// above.
if (state.SyntaxFacts.StringComparer.Equals(namedType.Name, alias))
return;

AddNonAliasReferences(
namedType, alias, state, processResult, processResultData, cancellationToken);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,16 +72,10 @@ protected override void FindReferencesInDocument<TData>(
symbol, namespaceName, state, StandardCallbacks<FinderLocation>.AddToArrayBuilder, initialReferences, cancellationToken);

foreach (var globalAlias in state.GlobalAliases)
{
// ignore the cases where the global alias might match the namespace name (i.e.
// global alias Collections = System.Collections). We'll already find those references
// above.
if (state.SyntaxFacts.StringComparer.Equals(namespaceName, globalAlias))
continue;

AddNamedReferences(
symbol, globalAlias, state, StandardCallbacks<FinderLocation>.AddToArrayBuilder, initialReferences, cancellationToken);
}
FindReferenceToAlias(symbol, state, initialReferences, namespaceName, globalAlias, cancellationToken);

foreach (var localAlias in state.Cache.SyntaxTreeIndex.GetAliases(symbol.Name, arity: 0))
FindReferenceToAlias(symbol, state, initialReferences, namespaceName, localAlias, cancellationToken);

// The items in initialReferences need to be both reported and used later to calculate additional results.
foreach (var location in initialReferences)
Expand All @@ -95,6 +89,19 @@ protected override void FindReferencesInDocument<TData>(
}
}

private static void FindReferenceToAlias(
INamespaceSymbol symbol, FindReferencesDocumentState state, ArrayBuilder<FinderLocation> initialReferences, string namespaceName, string alias, CancellationToken cancellationToken)
{
// ignore the cases where the global alias might match the namespace name (i.e.
// global alias Collections = System.Collections). We'll already find those references
// above.
if (state.SyntaxFacts.StringComparer.Equals(namespaceName, alias))
return;

AddNamedReferences(
symbol, alias, state, StandardCallbacks<FinderLocation>.AddToArrayBuilder, initialReferences, cancellationToken);
}

/// <summary>
/// Finds references to <paramref name="symbol"/> in this <paramref name="state"/>, but only if it referenced
/// though <paramref name="name"/> (which might be the actual name of the type, or a global alias to it).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,13 @@ namespace Microsoft.CodeAnalysis.FindSymbols;
internal partial class AbstractSyntaxIndex<TIndex>
{
private static readonly string s_persistenceName = typeof(TIndex).Name;
private static readonly Checksum s_serializationFormatChecksum = CodeAnalysis.Checksum.Create("39");

/// <summary>
/// Increment this whenever the data format of the <see cref="AbstractSyntaxIndex{TIndex}"/> changes. This ensures
/// that we will not try to read previously cached data from a prior version of roslyn with a different format and
/// will instead regenerate all the indices with the new format.
/// </summary>
private static readonly Checksum s_serializationFormatChecksum = CodeAnalysis.Checksum.Create("40");

/// <summary>
/// Cache of ParseOptions to a checksum for the <see cref="ParseOptions.PreprocessorSymbolNames"/> contained
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,22 @@ internal sealed partial class SyntaxTreeIndex : AbstractSyntaxIndex<SyntaxTreeIn
private readonly LiteralInfo _literalInfo;
private readonly IdentifierInfo _identifierInfo;
private readonly ContextInfo _contextInfo;
private readonly HashSet<(string alias, string name, int arity)>? _aliasInfo;
private readonly HashSet<(string alias, string name, int arity)>? _globalAliasInfo;

private SyntaxTreeIndex(
Checksum? checksum,
LiteralInfo literalInfo,
IdentifierInfo identifierInfo,
ContextInfo contextInfo,
HashSet<(string alias, string name, int arity)>? aliasInfo,
HashSet<(string alias, string name, int arity)>? globalAliasInfo)
: base(checksum)
{
_literalInfo = literalInfo;
_identifierInfo = identifierInfo;
_contextInfo = contextInfo;
_aliasInfo = aliasInfo;
_globalAliasInfo = globalAliasInfo;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ private static SyntaxTreeIndex CreateIndex(
var stringLiterals = StringLiteralHashSetPool.Allocate();
var longLiterals = LongLiteralHashSetPool.Allocate();

HashSet<(string alias, string name, int arity)>? aliasInfo = null;
HashSet<(string alias, string name, int arity)>? globalAliasInfo = null;

try
Expand Down Expand Up @@ -98,7 +99,7 @@ private static SyntaxTreeIndex CreateIndex(
containsConversion = containsConversion || syntaxFacts.IsConversionExpression(node);
containsCollectionInitializer = containsCollectionInitializer || syntaxFacts.IsObjectCollectionInitializer(node);

TryAddGlobalAliasInfo(syntaxFacts, ref globalAliasInfo, node);
TryAddAliasInfo(syntaxFacts, ref aliasInfo, ref globalAliasInfo, node);
}
else
{
Expand Down Expand Up @@ -186,6 +187,7 @@ private static SyntaxTreeIndex CreateIndex(
containsConversion,
containsGlobalKeyword,
containsCollectionInitializer),
aliasInfo,
globalAliasInfo);
}
finally
Expand Down Expand Up @@ -219,17 +221,16 @@ private static bool IsGlobalSuppressMessageAttribute(ISyntaxFactsService syntaxF
syntaxFacts.StringComparer.Equals(identifierName, nameof(SuppressMessageAttribute));
}

private static void TryAddGlobalAliasInfo(
private static void TryAddAliasInfo(
ISyntaxFactsService syntaxFacts,
ref HashSet<(string alias, string name, int arity)>? aliasInfo,
ref HashSet<(string alias, string name, int arity)>? globalAliasInfo,
SyntaxNode node)
{
if (!syntaxFacts.IsUsingAliasDirective(node))
return;

syntaxFacts.GetPartsOfUsingAliasDirective(node, out var globalToken, out var alias, out var usingTarget);
if (globalToken.IsMissing)
return;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this was the bug. IsMissing is for when the parser thinks a token must be there but doesn't see it. e.g. if x == y) will have a 'missing' open paren token. but if you just have using x = u; there is no missing 'global' token there as that's just a legal using statement with a default global token.

this bug was making it so that every alias in the project was being treated as a global alias. We'd still only look for that alias if the name it bound to was hte name of hte symbol we were looking for. but this was still unnecessary work.


// if we have `global using X = Y.Z` then walk down the rhs to pull out 'Z'.
if (syntaxFacts.IsQualifiedName(usingTarget))
Expand All @@ -242,8 +243,13 @@ private static void TryAddGlobalAliasInfo(
if (syntaxFacts.IsSimpleName(usingTarget))
{
syntaxFacts.GetNameAndArityOfSimpleName(usingTarget, out var name, out var arity);
globalAliasInfo ??= [];
globalAliasInfo.Add((alias.ValueText, name, arity));

ref var set = ref globalToken == default
? ref aliasInfo
: ref globalAliasInfo;

set ??= [];
set.Add((alias.ValueText, name, arity));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// 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 Microsoft.CodeAnalysis.LanguageService;
using Microsoft.CodeAnalysis.Shared.Collections;
Expand Down Expand Up @@ -42,13 +43,20 @@ internal sealed partial class SyntaxTreeIndex
/// <c>name="C"</c> and arity=1 will return <c>X</c>.
/// </summary>
public ImmutableArray<string> GetGlobalAliases(string name, int arity)
=> GetAliasesWorker(name, arity, _globalAliasInfo);

public ImmutableArray<string> GetAliases(string name, int arity)
=> GetAliasesWorker(name, arity, _aliasInfo);

private static ImmutableArray<string> GetAliasesWorker(
string name, int arity, HashSet<(string alias, string name, int arity)>? aliases)
{
if (_globalAliasInfo == null)
if (aliases == null)
return [];

using var result = TemporaryArray<string>.Empty;

foreach (var (alias, aliasName, aliasArity) in _globalAliasInfo)
foreach (var (alias, aliasName, aliasArity) in aliases)
{
if (aliasName == name && aliasArity == arity)
result.Add(alias);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,20 @@ public override void WriteTo(ObjectWriter writer)
_identifierInfo.WriteTo(writer);
_contextInfo.WriteTo(writer);

if (_globalAliasInfo == null)
{
writer.WriteInt32(0);
}
else
WriteTo(writer, _aliasInfo);
WriteTo(writer, _globalAliasInfo);

static void WriteTo(ObjectWriter writer, HashSet<(string alias, string name, int arity)>? aliasInfo)
{
writer.WriteInt32(_globalAliasInfo.Count);
foreach (var (alias, name, arity) in _globalAliasInfo)
writer.WriteInt32(aliasInfo?.Count ?? 0);
if (aliasInfo != null)
{
writer.WriteString(alias);
writer.WriteString(name);
writer.WriteInt32(arity);
foreach (var (alias, name, arity) in aliasInfo)
{
writer.WriteString(alias);
writer.WriteString(name);
writer.WriteInt32(arity);
}
}
}
}
Expand All @@ -51,27 +53,33 @@ public override void WriteTo(ObjectWriter writer)
if (literalInfo == null || identifierInfo == null || contextInfo == null)
return null;

var globalAliasInfoCount = reader.ReadInt32();
HashSet<(string alias, string name, int arity)>? globalAliasInfo = null;
return new SyntaxTreeIndex(
checksum,
literalInfo.Value,
identifierInfo.Value,
contextInfo.Value,
ReadAliasInfo(reader),
ReadAliasInfo(reader));

if (globalAliasInfoCount > 0)
static HashSet<(string alias, string name, int arity)>? ReadAliasInfo(ObjectReader reader)
{
globalAliasInfo = [];
var aliasInfoCount = reader.ReadInt32();
HashSet<(string alias, string name, int arity)>? aliasInfo = null;

for (var i = 0; i < globalAliasInfoCount; i++)
if (aliasInfoCount > 0)
{
var alias = reader.ReadRequiredString();
var name = reader.ReadRequiredString();
var arity = reader.ReadInt32();
globalAliasInfo.Add((alias, name, arity));
aliasInfo = [];

for (var i = 0; i < aliasInfoCount; i++)
{
var alias = reader.ReadRequiredString();
var name = reader.ReadRequiredString();
var arity = reader.ReadInt32();
aliasInfo.Add((alias, name, arity));
}
}
}

return new SyntaxTreeIndex(
checksum,
literalInfo.Value,
identifierInfo.Value,
contextInfo.Value,
globalAliasInfo);
return aliasInfo;
}
}
}
Loading