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

Address a few locations also need to care about partial properties #73792

Merged
Show file tree
Hide file tree
Changes from all 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 @@ -164,6 +164,34 @@ public partial void M() { }
Assert.Equal(implementation, ResolveSymbol(implementation, comp, SymbolKeyComparison.None));
}

[Fact]
public void ExtendedPartialPropertyDefinitionAndImplementationResolveCorrectly()
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 is just a test verified the same behavior as partial methods above.

{
var src = """
using System;
namespace NS
{
public partial class C1
{
private int x;
public partial int Prop { get; set; }
public partial int Prop { get => x; set => x = value; }
}
}
""";

var comp = (Compilation)CreateCompilation(src, assemblyName: "Test");

var ns = comp.SourceModule.GlobalNamespace.GetMembers("NS").Single() as INamespaceSymbol;
var type = ns.GetTypeMembers("C1").FirstOrDefault();
var definition = type.GetMembers("Prop").First() as IPropertySymbol;
var implementation = definition.PartialImplementationPart;

// Assert that both the definition and implementation resolve back to themselves
Assert.Equal(definition, ResolveSymbol(definition, comp, SymbolKeyComparison.None));
Assert.Equal(implementation, ResolveSymbol(implementation, comp, SymbolKeyComparison.None));
}

[Fact, WorkItem("http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/916341")]
public void ExplicitIndexerImplementationResolvesCorrectly()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,18 +73,27 @@ private static ImmutableArray<RoslynNavigationBarItem> GetMembersInTypes(
continue;
}

var method = member as IMethodSymbol;
if (method != null && method.PartialImplementationPart != null)
if (member is IMethodSymbol { PartialImplementationPart: { } } methodSymbol)
Copy link
Member Author

Choose a reason for hiding this comment

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

Without this change, navigation bar still works.
Before:
image

After:
image

After behavior matches the existing behavior of partial method.

{
memberItems.AddIfNotNull(CreateItemForMember(solution, method, tree, cancellationToken));
memberItems.AddIfNotNull(CreateItemForMember(solution, method.PartialImplementationPart, tree, cancellationToken));
memberItems.AddIfNotNull(CreateItemForMember(solution, methodSymbol, tree, cancellationToken));
memberItems.AddIfNotNull(CreateItemForMember(solution, methodSymbol.PartialImplementationPart, tree, cancellationToken));
}
else
else if (member is IPropertySymbol { PartialImplementationPart: { } } propertySymbol)
{
memberItems.AddIfNotNull(CreateItemForMember(solution, propertySymbol, tree, cancellationToken));
memberItems.AddIfNotNull(CreateItemForMember(solution, propertySymbol.PartialImplementationPart, tree, cancellationToken));
}
else if (member is IMethodSymbol or IPropertySymbol)
{
Debug.Assert(method == null || method.PartialDefinitionPart == null, "NavBar expected GetMembers to return partial method definition parts but the implementation part was returned.");
Debug.Assert(member is IMethodSymbol { PartialDefinitionPart: null } or IPropertySymbol { PartialDefinitionPart: null },
$"NavBar expected GetMembers to return partial method/property definition parts but the implementation part was returned.");

memberItems.AddIfNotNull(CreateItemForMember(solution, member, tree, cancellationToken));
}
else
{
memberItems.AddIfNotNull(CreateItemForMember(solution, member, tree, cancellationToken));
}
}

memberItems.Sort((x, y) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ private IEnumerable<ISymbol> FindMembers(IEnumerable<INamespaceOrTypeSymbol> con
private IEnumerable<ISymbol> FindMembers(IEnumerable<INamedTypeSymbol> types, NameAndArity nameAndArity)
{
// Get the matching members from all types (including constructors and explicit interface
// implementations). If there is a partial method, prefer returning the implementation over
// implementations). If there is a partial method/property, prefer returning the implementation over
// the definition (since the definition will not be a candidate for setting a breakpoint).
var members = types.SelectMany(t => GetMembers(t, nameAndArity.Name))
.Select(s => GetPartialImplementationPartOrNull(s) ?? s);
Expand All @@ -227,8 +227,12 @@ private async Task<IEnumerable<INamedTypeSymbol>> GetAllTypesAsync(CancellationT
return namespaces.GetAllTypes(cancellationToken);
}

private static IMethodSymbol GetPartialImplementationPartOrNull(ISymbol symbol)
=> (symbol.Kind == SymbolKind.Method) ? ((IMethodSymbol)symbol).PartialImplementationPart : null;
private static ISymbol GetPartialImplementationPartOrNull(ISymbol symbol) => symbol.Kind switch
Copy link
Member Author

Choose a reason for hiding this comment

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

cc @tmat to take a look. Not familiar with debugger, but I feel this change brings no harm.

{
SymbolKind.Method => ((IMethodSymbol)symbol).PartialImplementationPart,
SymbolKind.Property => ((IPropertySymbol)symbol).PartialImplementationPart,
_ => null
};

/// <summary>
/// Is this method or property a valid place to set a breakpoint and does it match the expected parameter count?
Expand Down
Loading