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

Consider method accessibility in interface resolution #81409

Merged
merged 2 commits into from
Feb 2, 2023
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 @@ -117,6 +117,14 @@ public override bool IsFinal
}
}

public override bool IsPublic
{
get
{
return _methodDef.IsPublic;
}
}

public override bool HasCustomAttribute(string attributeNamespace, string attributeName)
{
return _methodDef.HasCustomAttribute(attributeNamespace, attributeName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ private static MethodDesc FindMatchingVirtualMethodOnTypeByNameAndSigWithSlotChe
return FindMatchingVirtualMethodOnTypeByNameAndSig(method, currentType, reverseMethodSearch, nameSigMatchMethodIsValidCandidate: s_VerifyMethodsHaveTheSameVirtualSlot);
}

private static Func<MethodDesc, MethodDesc, bool> s_VerifyMethodsHaveTheSameVirtualSlot = VerifyMethodsHaveTheSameVirtualSlot;
private static readonly Func<MethodDesc, MethodDesc, bool> s_VerifyMethodsHaveTheSameVirtualSlot = VerifyMethodsHaveTheSameVirtualSlot;

// Return true if the slot that defines methodToVerify matches slotDefiningMethod
private static bool VerifyMethodsHaveTheSameVirtualSlot(MethodDesc slotDefiningMethod, MethodDesc methodToVerify)
Expand All @@ -424,6 +424,14 @@ private static bool VerifyMethodsHaveTheSameVirtualSlot(MethodDesc slotDefiningM
return slotDefiningMethodOfMethodToVerify == slotDefiningMethod;
}

private static readonly Func<MethodDesc, MethodDesc, bool> s_VerifyMethodIsPublic = VerifyMethodIsPublic;

// Return true if the method to verify is public
private static bool VerifyMethodIsPublic(MethodDesc slotDefiningMethod, MethodDesc methodToVerify)
{
return methodToVerify.IsPublic;
}

private static void FindBaseUnificationGroup(MetadataType currentType, UnificationGroup unificationGroup)
{
MethodDesc originalDefiningMethod = unificationGroup.DefiningMethod;
Expand Down Expand Up @@ -615,7 +623,7 @@ private static MethodDesc ResolveInterfaceMethodToVirtualMethodOnType(MethodDesc
{
MethodDesc foundOnCurrentType = FindMatchingVirtualMethodOnTypeByNameAndSig(interfaceMethod, currentType,
reverseMethodSearch: false, /* When searching for name/sig overrides on a type that explicitly defines an interface, search through the type in the forward direction*/
nameSigMatchMethodIsValidCandidate :null);
nameSigMatchMethodIsValidCandidate: s_VerifyMethodIsPublic);
foundOnCurrentType = FindSlotDefiningMethodForVirtualMethod(foundOnCurrentType);

if (baseType == null)
Expand Down Expand Up @@ -653,7 +661,7 @@ private static MethodDesc ResolveInterfaceMethodToVirtualMethodOnType(MethodDesc
{
MethodDesc foundOnCurrentType = FindMatchingVirtualMethodOnTypeByNameAndSig(interfaceMethod, currentType,
reverseMethodSearch: false, /* When searching for name/sig overrides on a type that is the first type in the hierarchy to require the interface, search through the type in the forward direction*/
nameSigMatchMethodIsValidCandidate: null);
nameSigMatchMethodIsValidCandidate: s_VerifyMethodIsPublic);

foundOnCurrentType = FindSlotDefiningMethodForVirtualMethod(foundOnCurrentType);

Expand Down Expand Up @@ -729,7 +737,7 @@ private static MethodDesc FindNameSigOverrideForInterfaceMethodRecursive(MethodD

MethodDesc nameSigOverride = FindMatchingVirtualMethodOnTypeByNameAndSig(interfaceMethod, currentType,
reverseMethodSearch: true, /* When searching for a name sig match for an interface on parent types search in reverse order of declaration */
nameSigMatchMethodIsValidCandidate:null);
nameSigMatchMethodIsValidCandidate: s_VerifyMethodIsPublic);

if (nameSigOverride != null)
{
Expand Down
8 changes: 8 additions & 0 deletions src/coreclr/tools/Common/TypeSystem/Common/MethodDesc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -584,6 +584,14 @@ public virtual bool IsFinal
}
}

public virtual bool IsPublic
{
get
{
return false;
}
}

public abstract bool HasCustomAttribute(string attributeNamespace, string attributeName);

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,14 @@ public override bool IsFinal
}
}

public override bool IsPublic
{
get
{
return _typicalMethodDef.IsPublic;
}
}

public override bool HasCustomAttribute(string attributeNamespace, string attributeName)
{
return _typicalMethodDef.HasCustomAttribute(attributeNamespace, attributeName);
Expand Down
8 changes: 8 additions & 0 deletions src/coreclr/tools/Common/TypeSystem/Ecma/EcmaMethod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,14 @@ public override bool IsDefaultConstructor
}
}

public override bool IsPublic
{
get
{
return Attributes.IsPublic();
jkotas marked this conversation as resolved.
Show resolved Hide resolved
}
}

public override bool IsStaticConstructor
{
get
Expand Down
41 changes: 41 additions & 0 deletions src/tests/nativeaot/SmokeTests/UnitTests/Interfaces.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public static int Run()
if (TestIterfaceCallOptimization() == Fail)
return Fail;

TestPublicAndNonpublicDifference.Run();
TestDefaultInterfaceMethods.Run();
TestDefaultInterfaceVariance.Run();
TestVariantInterfaceOptimizations.Run();
Expand Down Expand Up @@ -477,6 +478,46 @@ private static int TestIterfaceCallOptimization()

#endregion

class TestPublicAndNonpublicDifference
{
interface IFrobber
{
string Frob();
}
class ProtectedBase : IFrobber
{
string IFrobber.Frob() => "IFrobber.Frob";
protected virtual string Frob() => "Base.Frob";
}

class ProtectedDerived : ProtectedBase, IFrobber
{
protected override string Frob() => "Derived.Frob";
}

class PublicBase : IFrobber
{
string IFrobber.Frob() => "IFrobber.Frob";
public virtual string Frob() => "Base.Frob";
}

class PublicDerived : PublicBase, IFrobber
{
public override string Frob() => "Derived.Frob";
}

public static void Run()
{
IFrobber f1 = new PublicDerived();
if (f1.Frob() != "Derived.Frob")
throw new Exception();

IFrobber f2 = new ProtectedDerived();
if (f2.Frob() != "IFrobber.Frob")
throw new Exception();
}
}

class TestDefaultInterfaceMethods
{
interface IFoo
Expand Down