-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
{ | ||
// 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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. | ||
|
@@ -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). | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,20 +14,20 @@ 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)>? _globalAliasInfo; | ||
private readonly HashSet<(string alias, string name, int arity, bool isGlobal)>? _aliasInfo; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. both the global and local alias info is useful to store per file. So we keep them both, with a flag saying which type it is. |
||
|
||
private SyntaxTreeIndex( | ||
Checksum? checksum, | ||
LiteralInfo literalInfo, | ||
IdentifierInfo identifierInfo, | ||
ContextInfo contextInfo, | ||
HashSet<(string alias, string name, int arity)>? globalAliasInfo) | ||
HashSet<(string alias, string name, int arity, bool isGlobal)>? aliasInfo) | ||
: base(checksum) | ||
{ | ||
_literalInfo = literalInfo; | ||
_identifierInfo = identifierInfo; | ||
_contextInfo = contextInfo; | ||
_globalAliasInfo = globalAliasInfo; | ||
_aliasInfo = aliasInfo; | ||
} | ||
|
||
public static ValueTask<SyntaxTreeIndex> GetRequiredIndexAsync(Document document, CancellationToken cancellationToken) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -47,7 +47,7 @@ private static SyntaxTreeIndex CreateIndex( | |
var stringLiterals = StringLiteralHashSetPool.Allocate(); | ||
var longLiterals = LongLiteralHashSetPool.Allocate(); | ||
|
||
HashSet<(string alias, string name, int arity)>? globalAliasInfo = null; | ||
HashSet<(string alias, string name, int arity, bool isGlobal)>? aliasInfo = null; | ||
|
||
try | ||
{ | ||
|
@@ -98,7 +98,7 @@ private static SyntaxTreeIndex CreateIndex( | |
containsConversion = containsConversion || syntaxFacts.IsConversionExpression(node); | ||
containsCollectionInitializer = containsCollectionInitializer || syntaxFacts.IsObjectCollectionInitializer(node); | ||
|
||
TryAddGlobalAliasInfo(syntaxFacts, ref globalAliasInfo, node); | ||
TryAddAliasInfo(syntaxFacts, ref aliasInfo, node); | ||
} | ||
else | ||
{ | ||
|
@@ -186,7 +186,7 @@ private static SyntaxTreeIndex CreateIndex( | |
containsConversion, | ||
containsGlobalKeyword, | ||
containsCollectionInitializer), | ||
globalAliasInfo); | ||
aliasInfo); | ||
} | ||
finally | ||
{ | ||
|
@@ -219,17 +219,15 @@ 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)>? globalAliasInfo, | ||
ref HashSet<(string alias, string name, int arity, bool isGlobal)>? aliasInfo, | ||
SyntaxNode node) | ||
{ | ||
if (!syntaxFacts.IsUsingAliasDirective(node)) | ||
return; | ||
|
||
syntaxFacts.GetPartsOfUsingAliasDirective(node, out var globalToken, out var alias, out var usingTarget); | ||
if (globalToken.IsMissing) | ||
return; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this was the bug. 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)) | ||
|
@@ -242,8 +240,9 @@ private static void TryAddGlobalAliasInfo( | |
if (syntaxFacts.IsSimpleName(usingTarget)) | ||
{ | ||
syntaxFacts.GetNameAndArityOfSimpleName(usingTarget, out var name, out var arity); | ||
globalAliasInfo ??= []; | ||
globalAliasInfo.Add((alias.ValueText, name, arity)); | ||
|
||
aliasInfo ??= []; | ||
aliasInfo.Add((alias.ValueText, name, arity, isGlobal: globalToken != default)); | ||
} | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,18 +25,15 @@ public override void WriteTo(ObjectWriter writer) | |
_identifierInfo.WriteTo(writer); | ||
_contextInfo.WriteTo(writer); | ||
|
||
if (_globalAliasInfo == null) | ||
writer.WriteInt32(_aliasInfo?.Count ?? 0); | ||
if (_aliasInfo != null) | ||
{ | ||
writer.WriteInt32(0); | ||
} | ||
else | ||
{ | ||
writer.WriteInt32(_globalAliasInfo.Count); | ||
foreach (var (alias, name, arity) in _globalAliasInfo) | ||
foreach (var (alias, name, arity, isGlobal) in _aliasInfo) | ||
{ | ||
writer.WriteString(alias); | ||
writer.WriteString(name); | ||
writer.WriteInt32(arity); | ||
writer.WriteBoolean(isGlobal); | ||
} | ||
} | ||
} | ||
|
@@ -51,19 +48,20 @@ 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; | ||
var aliasInfoCount = reader.ReadInt32(); | ||
HashSet<(string alias, string name, int arity, bool isGlobal)>? aliasInfo = null; | ||
|
||
if (globalAliasInfoCount > 0) | ||
if (aliasInfoCount > 0) | ||
{ | ||
globalAliasInfo = []; | ||
aliasInfo = []; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
||
for (var i = 0; i < globalAliasInfoCount; i++) | ||
for (var i = 0; i < aliasInfoCount; i++) | ||
{ | ||
var alias = reader.ReadRequiredString(); | ||
var name = reader.ReadRequiredString(); | ||
var arity = reader.ReadInt32(); | ||
globalAliasInfo.Add((alias, name, arity)); | ||
aliasInfo.Add(( | ||
reader.ReadRequiredString(), | ||
reader.ReadRequiredString(), | ||
reader.ReadInt32(), | ||
reader.ReadBoolean())); | ||
} | ||
} | ||
|
||
|
@@ -72,6 +70,6 @@ public override void WriteTo(ObjectWriter writer) | |
literalInfo.Value, | ||
identifierInfo.Value, | ||
contextInfo.Value, | ||
globalAliasInfo); | ||
aliasInfo); | ||
} | ||
} |
There was a problem hiding this comment.
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...