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

Detect static default interface methods in illink #96821

Merged
merged 2 commits into from
Jan 11, 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
32 changes: 16 additions & 16 deletions src/tools/illink/src/linker/Linker/TypeMapInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -149,26 +149,26 @@ void MapInterfaceMethodsInTypeHierarchy (TypeDefinition type)
// we shouldn't need to run the below logic. This results in ILLink potentially
// keeping more methods than needed.

// Static methods on interfaces must be implemented only via explicit method-impl record
// not by a signature match. So there's no point in running the logic below for static methods.

if (!resolvedInterfaceMethod.IsVirtual
|| resolvedInterfaceMethod.IsFinal
|| resolvedInterfaceMethod.IsStatic)
|| resolvedInterfaceMethod.IsFinal)
continue;

// Try to find an implementation with a name/sig match on the current type
MethodDefinition? exactMatchOnType = TryMatchMethod (type, interfaceMethod);
if (exactMatchOnType != null) {
AnnotateMethods (resolvedInterfaceMethod, exactMatchOnType);
continue;
}
// Static methods on interfaces must be implemented only via explicit method-impl record
// not by a signature match. So there's no point in running this logic for static methods.
if (!resolvedInterfaceMethod.IsStatic) {
// Try to find an implementation with a name/sig match on the current type
MethodDefinition? exactMatchOnType = TryMatchMethod (type, interfaceMethod);
if (exactMatchOnType != null) {
AnnotateMethods (resolvedInterfaceMethod, exactMatchOnType);
continue;
}

// Next try to find an implementation with a name/sig match in the base hierarchy
var @base = GetBaseMethodInTypeHierarchy (type, interfaceMethod);
if (@base != null) {
AnnotateMethods (resolvedInterfaceMethod, @base, interfaceImpl.OriginalImpl);
continue;
// Next try to find an implementation with a name/sig match in the base hierarchy
var @base = GetBaseMethodInTypeHierarchy (type, interfaceMethod);
if (@base != null) {
AnnotateMethods (resolvedInterfaceMethod, @base, interfaceImpl.OriginalImpl);
continue;
}
}

// Look for a default implementation last.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using Mono.Linker.Tests.Cases.Expectations.Assertions;

namespace Mono.Linker.Tests.Cases.Inheritance.Interfaces.DefaultInterfaceMethods
{
[TestCaseRequirements (TestRunCharacteristics.SupportsDefaultInterfaceMethods, "Requires support for default interface methods")]
class StaticDefaultInterfaceMethodOnStruct
{
public static void Main ()
{
#if SUPPORTS_DEFAULT_INTERFACE_METHODS
Foo<Derived> ();
#endif
}

#if SUPPORTS_DEFAULT_INTERFACE_METHODS
[Kept]
static void Foo<T> () where T : IBase
sbomer marked this conversation as resolved.
Show resolved Hide resolved
{
T.Foo ();
sbomer marked this conversation as resolved.
Show resolved Hide resolved
}

[Kept]
interface IBase
{
[Kept]
static abstract void Foo ();
}

[Kept]
[KeptInterface (typeof (IBase))]
interface IDerived : IBase
{
[Kept]
static void IBase.Foo () { }
}

[Kept]
[KeptInterface (typeof (IDerived))]
[KeptInterface (typeof (IBase))]
struct Derived : IDerived
{
}
#endif
}
}
Loading