Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
22 changes: 20 additions & 2 deletions src/Meziantou.Analyzer/Internals/OverloadFinder.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
using System.Collections.Concurrent;
using Microsoft.CodeAnalysis.CSharp.Syntax;

namespace Meziantou.Analyzer.Internals;

internal sealed class OverloadFinder(Compilation compilation)
Expand All @@ -13,6 +16,7 @@ internal sealed class OverloadFinder(Compilation compilation)
private readonly INamedTypeSymbol? _ienumerableOfTSymbol = compilation.GetTypeByMetadataName("System.Collections.Generic.IEnumerable`1");
private readonly INamedTypeSymbol? _halfSymbol = compilation.GetTypeByMetadataName("System.Half");
private readonly Lazy<Dictionary<string, List<IMethodSymbol>>> _extensionMethodsByName = new(() => CreateExtensionMethodsByName(compilation));
private readonly ConcurrentDictionary<(SyntaxTree Tree, int ScopeStart, ITypeSymbol Container, string Name, bool IncludeReducedExtensionMethods), ImmutableArray<ISymbol>> _lookupCache = new();
Comment thread
meziantou marked this conversation as resolved.
Resolved

private static ReadOnlySpan<OverloadParameterType> Wrap(ReadOnlySpan<ITypeSymbol?> types)
{
Expand Down Expand Up @@ -671,10 +675,10 @@ static void AddSymbols(IEnumerable<ISymbol> symbols, List<ISymbol> results, Hash
var semanticModel = compilation.GetSemanticModel(options.SyntaxNode.SyntaxTree);
var position = options.SyntaxNode.GetLocation().SourceSpan.End;

AddSymbols(semanticModel.LookupSymbols(position, methodSymbol.ContainingType, methodName, includeReducedExtensionMethods: true), results, knownSymbols);
AddSymbols(LookupSymbols(semanticModel, options.SyntaxNode, position, methodSymbol.ContainingType, methodName, includeReducedExtensionMethods: true), results, knownSymbols);
if (reducedReceiverType is not null)
{
AddSymbols(semanticModel.LookupSymbols(position, reducedReceiverType, methodName, includeReducedExtensionMethods: false), results, knownSymbols);
AddSymbols(LookupSymbols(semanticModel, options.SyntaxNode, position, reducedReceiverType, methodName, includeReducedExtensionMethods: false), results, knownSymbols);
AddSymbols(reducedReceiverType.GetMembers(methodName), results, knownSymbols);
}

Expand All @@ -695,6 +699,20 @@ static void AddSymbols(IEnumerable<ISymbol> symbols, List<ISymbol> results, Hash
return results;
}

// A member lookup only depends on the enclosing type declaration (accessibility, imported namespaces), the container and the name
private ImmutableArray<ISymbol> LookupSymbols(SemanticModel semanticModel, SyntaxNode node, int position, ITypeSymbol container, string name, bool includeReducedExtensionMethods)
{
var scopeStart = node.FirstAncestorOrSelf<BaseTypeDeclarationSyntax>()?.SpanStart ?? -1;
var key = (node.SyntaxTree, scopeStart, container, name, includeReducedExtensionMethods);
if (!_lookupCache.TryGetValue(key, out var symbols))
{
symbols = semanticModel.LookupSymbols(position, container, name, includeReducedExtensionMethods);
_lookupCache.TryAdd(key, symbols);
}

return symbols;
}

/// <summary>
/// Adds the extension methods that apply to the receiver of <paramref name="methodSymbol"/>, are accessible at
/// <paramref name="position"/>, and are not in scope because their namespace is not imported. They are added after the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1953,4 +1953,188 @@ public class Sample

return test.RunAsync();
}

[Fact]
public Task ExtensionMethod_ImportedInOneNamespaceBlockOfTheFile()
{
// Each call is made twice so that the second one is answered from the lookup cache of OverloadFinder
var test = CreateTest();
test.TestCode = """
namespace A
{
using Ext;

class TestA
{
public void M(Sample sample, System.Threading.CancellationToken cancellationToken)
{
{|MA0040:sample.Run()|};
{|MA0040:sample.Run()|};
}
}
}

namespace B
{
class TestB
{
public void M(Sample sample, System.Threading.CancellationToken cancellationToken)
{
sample.Run();
sample.Run();
}
}
}

public class Sample
{
public void Run() => throw null;
}

namespace Ext
{
public static class SampleExtensions
{
public static void Run(this Sample sample, System.Threading.CancellationToken cancellationToken) => throw null;
}
}
""";

return test.RunAsync();
}

[Fact]
public Task ExtensionMethod_ImportedInOneFileOfTheCompilation()
{
// Each call is made twice so that the second one is answered from the lookup cache of OverloadFinder
var test = CreateTest();
test.TestState.Sources.Add(("File1.cs", """
using Ext;

class TestA
{
public void M(Sample sample, System.Threading.CancellationToken cancellationToken)
{
{|MA0040:sample.Run()|};
{|MA0040:sample.Run()|};
}
}
"""));
test.TestState.Sources.Add(("File2.cs", """
using Bar;

class TestB
{
public void M(Sample sample, System.Threading.CancellationToken cancellationToken)
{
sample.Run();
sample.Run();
}
}

public class Sample
{
public void Run() => throw null;
}

namespace Ext
{
public static class SampleExtensions
{
public static void Run(this Sample sample, System.Threading.CancellationToken cancellationToken) => throw null;
}
}

namespace Bar
{
public static class Unrelated
{
}
}
"""));

return test.RunAsync();
}

[Fact]
public Task PrivateOverload_OnlyReportedWhereAccessible()
{
// Each call is made twice so that the second one is answered from the lookup cache of OverloadFinder
var test = CreateTest();
test.TestCode = """
public class Sample
{
public void Run() => throw null;
private void Run(System.Threading.CancellationToken cancellationToken) => throw null;

public void M(Sample other, System.Threading.CancellationToken cancellationToken)
{
{|MA0040:other.Run()|};
{|MA0040:other.Run()|};
}

private sealed class Nested
{
public void M(Sample sample, System.Threading.CancellationToken cancellationToken)
{
{|MA0040:sample.Run()|};
{|MA0040:sample.Run()|};
}
}
}

class Test
{
public void M(Sample sample, System.Threading.CancellationToken cancellationToken)
{
sample.Run();
sample.Run();
}
}
""";

return test.RunAsync();
}

[Fact]
public Task ExtensionMethod_ImportedInNamespaceBlockAfterTopLevelStatements()
{
// Each call is made twice so that the second one is answered from the lookup cache of OverloadFinder
var test = CreateTest();
test.TestState.OutputKind = OutputKind.ConsoleApplication;
test.TestCode = """
var cancellationToken = System.Threading.CancellationToken.None;
new Sample().Run();
new Sample().Run();

namespace A
{
using Ext;

class TestA
{
public void M(Sample sample, System.Threading.CancellationToken cancellationToken)
{
{|MA0040:sample.Run()|};
{|MA0040:sample.Run()|};
}
}
}

public class Sample
{
public void Run() => throw null;
}

namespace Ext
{
public static class SampleExtensions
{
public static void Run(this Sample sample, System.Threading.CancellationToken cancellationToken) => throw null;
}
}
""";

return test.RunAsync();
}
}