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

improve goto-def on an invalid override #75901

Merged
merged 3 commits into from
Nov 14, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -855,6 +855,95 @@ class C
Await TestAsync(workspace)
End Function

<WpfFact, WorkItem("https://github.com/dotnet/roslyn/issues/57110")>
Public Async Function TestCSharpGoToOverriddenDefinition_FromOverride_LooseMatch() As Task
Dim workspace =
<Workspace>
<Project Language="C#" CommonReferences="true">
<Document>
class C
{
public virtual void [|F|](bool x)
{
}
}

class D : C
{
public $$override void F(int x)
{
}
}
</Document>
</Project>
</Workspace>

Await TestAsync(workspace)
End Function

<WpfFact, WorkItem("https://github.com/dotnet/roslyn/issues/57110")>
Public Async Function TestCSharpGoToOverriddenDefinition_FromOverride_LooseMatch2() As Task
Dim workspace =
<Workspace>
<Project Language="C#" CommonReferences="true">
<Document>
class C
{
public virtual void F()
{
}

public virtual void [|F|](bool x)
{
}
}

class D : C
{
public $$override void F(int x)
{
}
}
</Document>
</Project>
</Workspace>

Await TestAsync(workspace)
End Function

<WpfFact, WorkItem("https://github.com/dotnet/roslyn/issues/57110")>
Public Async Function TestCSharpGoToOverriddenDefinition_FromOverride_LooseMatch3() As Task
Dim workspace =
<Workspace>
<Project Language="C#" CommonReferences="true">
<Document>
class B
{
public virtual void F(bool x)
{
}
}

class C
{
public virtual void [|F|]()
{
}
}

class D : C
{
public $$override void F(int x)
{
}
}
</Document>
</Project>
</Workspace>

Await TestAsync(workspace)
End Function

<WpfFact>
Public Async Function TestCSharpGoToUnmanaged_Keyword() As Task
Dim workspace =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public static TokenSemanticInfo GetSemanticInfo(
{
// on an "override" token, we'll find the overridden symbol
var overriddingSymbol = semanticFacts.GetDeclaredSymbol(semanticModel, overriddingIdentifier.Value, cancellationToken);
var overriddenSymbol = overriddingSymbol.GetOverriddenMember();
var overriddenSymbol = overriddingSymbol.GetOverriddenMember(allowLooseMatch: true);

allSymbols = overriddenSymbol is null ? [] : [overriddenSymbol];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,75 @@ public static SymbolVisibility GetResultantVisibility(this ISymbol symbol)
return visibility;
}

public static ISymbol? GetOverriddenMember(this ISymbol? symbol)
=> symbol switch
public static ISymbol? GetOverriddenMember(this ISymbol? symbol, bool allowLooseMatch = false)
{
if (symbol is null)
return null;

ISymbol? exactMatch = symbol switch
{
IMethodSymbol method => method.OverriddenMethod,
IPropertySymbol property => property.OverriddenProperty,
IEventSymbol @event => @event.OverriddenEvent,
_ => null,
};

if (exactMatch != null)
return exactMatch;

if (allowLooseMatch)
{
foreach (var baseType in symbol.ContainingType.GetBaseTypes())
{
if (TryFindLooseMatch(symbol, baseType, out var looseMatch))
return looseMatch;
}
}

return null;

bool TryFindLooseMatch(ISymbol symbol, INamedTypeSymbol baseType, [NotNullWhen(true)] out ISymbol? looseMatch)
{
using var _ = ArrayBuilder<IMethodSymbol>.GetInstance(out var candidateMethods);

foreach (var member in baseType.GetMembers(symbol.Name))
{
if (member.Kind != symbol.Kind)
continue;

if (member.IsSealed)
continue;

if (!member.IsVirtual && !member.IsOverride && !member.IsAbstract)
continue;

if (symbol.Kind is SymbolKind.Event or SymbolKind.Property)
{
// We've found a matching event/property in the base type (perhaps differing by return type). This
// is a good enough match to return as a loose match for the starting symbol.
looseMatch = member;
return true;
}
else if (member is IMethodSymbol method)
{
// We found a method. Keep track of this so we can try to find the best possible loose match.
candidateMethods.Add(method);
CyrusNajmabadi marked this conversation as resolved.
Show resolved Hide resolved
}
}

var parameterCount = symbol.GetParameters().Length;
candidateMethods.Sort((m1, m2) =>
{
var parameterDiff1 = Math.Abs(m1.Parameters.Length - parameterCount);
var parameterDiff2 = Math.Abs(m2.Parameters.Length - parameterCount);
return parameterDiff1 - parameterDiff2;
});

looseMatch = candidateMethods.FirstOrDefault();
return looseMatch != null;
}
}

public static ImmutableArray<ISymbol> ExplicitInterfaceImplementations(this ISymbol symbol)
=> symbol switch
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,13 +121,16 @@ public static IEnumerable<ITypeSymbol> GetBaseTypesAndThis(this ITypeSymbol? typ
}
}

public static IEnumerable<INamedTypeSymbol> GetBaseTypes(this ITypeSymbol type)
public static IEnumerable<INamedTypeSymbol> GetBaseTypes(this ITypeSymbol? type)
{
var current = type.BaseType;
while (current != null)
if (type is not null)
{
yield return current;
current = current.BaseType;
var current = type.BaseType;
Copy link
Member

Choose a reason for hiding this comment

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

would type?.BaseType work here instead of the if?

Copy link
Member Author

Choose a reason for hiding this comment

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

yes. done.

while (current != null)
{
yield return current;
current = current.BaseType;
}
}
}

Expand Down
Loading