From d8389ad0f224d1ffc3711fd58eba40f889a89609 Mon Sep 17 00:00:00 2001 From: Oleg Rakhmatulin Date: Mon, 1 May 2023 13:19:46 +0200 Subject: [PATCH] Issue #172 - Never add method implementation if the declaration handle equals to body handle (default interface method case). --- src/AssemblyGenerator.Types.cs | 18 +++++++++++++++--- src/Lokad.ILPack.csproj | 4 ++-- test/TestSubject/IMyItf.cs | 4 ++++ test/TestSubject/TestSubject.csproj | 2 +- 4 files changed, 22 insertions(+), 6 deletions(-) diff --git a/src/AssemblyGenerator.Types.cs b/src/AssemblyGenerator.Types.cs index 798cf6c..714912e 100644 --- a/src/AssemblyGenerator.Types.cs +++ b/src/AssemblyGenerator.Types.cs @@ -209,16 +209,28 @@ HashSet interfaces var targetMethod = interfaceMapping.TargetMethods[i]; var ifcMethod = interfaceMapping.InterfaceMethods[i]; + if (targetMethod == null || ifcMethod == null) + { + continue; + } + + var bodyHandle =_metadata.GetMethodHandle(targetMethod); + var declarationHandle = _metadata.GetMethodHandle(ifcMethod); + + if (bodyHandle == declarationHandle) + { + continue; + } + // Declare a method override either when the interface implementation or // the interface method implementation is declared by the type. - if (targetMethod != null && (implementedByType || targetMethod.DeclaringType == type)) + if (implementedByType || targetMethod.DeclaringType == type) { // Mark the target method as implementing the interface method. // (This is the equivalent of .Override in msil) _metadata.Builder.AddMethodImplementation( (TypeDefinitionHandle)_metadata.GetTypeHandle(targetMethod.DeclaringType), - _metadata.GetMethodHandle(targetMethod), - _metadata.GetMethodHandle(ifcMethod)); + bodyHandle, declarationHandle); } } } diff --git a/src/Lokad.ILPack.csproj b/src/Lokad.ILPack.csproj index aa72a9a..b0aaed2 100644 --- a/src/Lokad.ILPack.csproj +++ b/src/Lokad.ILPack.csproj @@ -2,7 +2,7 @@ netstandard2.0 - true + true Library 8.0 @@ -13,7 +13,7 @@ Copyright © Lokad 2020 - 2022 Lokad.ILPack - + 0.2.0.0 0.2.0.0 0.2.0.0 diff --git a/test/TestSubject/IMyItf.cs b/test/TestSubject/IMyItf.cs index 9066de0..15da9c5 100644 --- a/test/TestSubject/IMyItf.cs +++ b/test/TestSubject/IMyItf.cs @@ -15,5 +15,9 @@ public interface IMyItf int InterfaceMethod2(); TResult InterfaceMethod3(Func f); + +#if NETCOREAPP3_0_OR_GREATER + void DefaultInterfaceMethod() { } +#endif } } diff --git a/test/TestSubject/TestSubject.csproj b/test/TestSubject/TestSubject.csproj index 20f1a23..cfedc9f 100644 --- a/test/TestSubject/TestSubject.csproj +++ b/test/TestSubject/TestSubject.csproj @@ -1,7 +1,7 @@  - netcoreapp3.1;netstandard2.0 + netstandard2.0;net6.0 true True ..\..\Lokad.ILPack.snk