Skip to content
New issue

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

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

Already on GitHub? # to your account

Do a better job matching properties to implement against existing properties #76144

Merged
merged 1 commit into from
Dec 2, 2024
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 @@ -12129,4 +12129,35 @@ class Class : IEnumerator<int>
}
""");
}

[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/19721")]
public async Task TestMatchPropertyAgainstPropertyWithMoreAccessors1()
{
await TestWithAllCodeStyleOptionsOnAsync(
"""
interface ImmutableView
{
int Prop1 { get; }
}
interface MutableView : ImmutableView
{
new int Prop1 { get; set; }
}
class Implementation : {|CS0535:{|CS0535:MutableView|}|} { }
""",
"""
interface ImmutableView
{
int Prop1 { get; }
}
interface MutableView : ImmutableView
{
new int Prop1 { get; set; }
}
class Implementation : MutableView
{
public int Prop1 { get => throw new System.NotImplementedException(); set => throw new System.NotImplementedException(); }
}
""");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -405,22 +405,35 @@ private bool HasMatchingMember(ArrayBuilder<ISymbol> implementedVisibleMembers,
return implementedVisibleMembers.Any(m => MembersMatch(m, member));
}

private bool MembersMatch(ISymbol member1, ISymbol member2)
private bool MembersMatch(ISymbol existingMember, ISymbol memberToAdd)
{
if (member1.Kind != member2.Kind)
if (existingMember.Kind != memberToAdd.Kind)
return false;

if (member1.DeclaredAccessibility != member2.DeclaredAccessibility ||
member1.IsStatic != member2.IsStatic)
if (existingMember.DeclaredAccessibility != memberToAdd.DeclaredAccessibility ||
existingMember.IsStatic != memberToAdd.IsStatic)
{
return false;
}

if (member1.ExplicitInterfaceImplementations().Any() || member2.ExplicitInterfaceImplementations().Any())
if (existingMember.ExplicitInterfaceImplementations().Any() || memberToAdd.ExplicitInterfaceImplementations().Any())
return false;

return SignatureComparer.Instance.HaveSameSignatureAndConstraintsAndReturnTypeAndAccessors(
member1, member2, IsCaseSensitive);
if (!SignatureComparer.Instance.HaveSameSignatureAndConstraintsAndReturnType(existingMember, memberToAdd, IsCaseSensitive))
return false;

if (existingMember is IPropertySymbol existingProperty && memberToAdd is IPropertySymbol propertyToAdd)
Copy link
Contributor

Choose a reason for hiding this comment

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

if (existingMember is IPropertySymbol existingProperty && memberToAdd is IPropertySymbol propertyToAdd)

Coffee still isn't hitting, doesn't SignatureComparer.HaveSameSignatureAndConstraintsAndReturnType now cover this case?

Copy link
Contributor

Choose a reason for hiding this comment

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

Discussed offline

{
// Have to make sure the accessors of the properties are complimentary. Note: it's ok for the new
// property to have a subset of the accessors of the existing property.
if (propertyToAdd.GetMethod != null && SignatureComparer.BadPropertyAccessor(propertyToAdd.GetMethod, existingProperty.GetMethod))
return false;

if (propertyToAdd.SetMethod != null && SignatureComparer.BadPropertyAccessor(propertyToAdd.SetMethod, existingProperty.SetMethod))
return false;
}

return true;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public bool HaveSameSignature(IPropertySymbol property1, IPropertySymbol propert
this.ParameterEquivalenceComparer);
}

private static bool BadPropertyAccessor(IMethodSymbol method1, IMethodSymbol method2)
public static bool BadPropertyAccessor(IMethodSymbol method1, IMethodSymbol method2)
{
return method1 != null &&
(method2 == null || method2.DeclaredAccessibility != Accessibility.Public);
Expand Down Expand Up @@ -137,6 +137,21 @@ public bool HaveSameSignature(
}

public bool HaveSameSignatureAndConstraintsAndReturnTypeAndAccessors(ISymbol symbol1, ISymbol symbol2, bool caseSensitive)
{
// NOTE - we're deliberately using reference equality here for speed.
if (symbol1 == symbol2)
return true;

if (!HaveSameSignatureAndConstraintsAndReturnType(symbol1, symbol2, caseSensitive))
return false;

if (symbol1 is IPropertySymbol property1 && symbol2 is IPropertySymbol property2)
return HaveSameAccessors(property1, property2);

return true;
}

public bool HaveSameSignatureAndConstraintsAndReturnType(ISymbol symbol1, ISymbol symbol2, bool caseSensitive)
{
// NOTE - we're deliberately using reference equality here for speed.
if (symbol1 == symbol2)
Expand All @@ -158,8 +173,7 @@ public bool HaveSameSignatureAndConstraintsAndReturnTypeAndAccessors(ISymbol sym

return property1.ReturnsByRef == property2.ReturnsByRef &&
property1.ReturnsByRefReadonly == property2.ReturnsByRefReadonly &&
this.SignatureTypeEquivalenceComparer.Equals(property1.Type, property2.Type) &&
HaveSameAccessors(property1, property2);
this.SignatureTypeEquivalenceComparer.Equals(property1.Type, property2.Type);
case SymbolKind.Event:
var ev1 = (IEventSymbol)symbol1;
var ev2 = (IEventSymbol)symbol2;
Expand Down
Loading