Skip to content
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
12 changes: 11 additions & 1 deletion src/linker/Linker.Steps/SweepStep.cs
Original file line number Diff line number Diff line change
Expand Up @@ -472,12 +472,22 @@ protected virtual void SweepMethods (Collection<MethodDefinition> methods)
void SweepOverrides (MethodDefinition method)
{
for (int i = 0; i < method.Overrides.Count;) {
if (Context.Resolve (method.Overrides[i]) is MethodDefinition ov && ShouldRemove (ov))
if (Context.Resolve (method.Overrides[i]) is MethodDefinition ov && ShouldRemove (ov) && !IgnoreScope (ov.DeclaringType.Scope))
method.Overrides.RemoveAt (i);
else
i++;
}
}

/// <summary>
/// Returns true if the assembly of the <paramref name="scope"></paramref> is not set to link (i.e. action=copy is set for that assembly)
/// </summary>
private bool IgnoreScope (IMetadataScope scope)
{
AssemblyDefinition? assembly = Context.Resolve (scope);
return assembly != null && Annotations.GetAction (assembly) != AssemblyAction.Link;
}

void SweepDebugInfo (Collection<MethodDefinition> methods)
{
List<ScopeDebugInformation>? sweptScopes = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ public interface ICopyLibraryStaticInterface
{
static abstract void CopyLibraryStaticInterfaceMethod ();
static abstract void CopyLibraryExplicitImplementationStaticInterfaceMethod ();
public static abstract void PublicStaticInterfaceMethod ();
}
}
10 changes: 10 additions & 0 deletions test/Mono.Linker.Tests.Cases/Libraries/Dependencies/SkipLibrary.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

namespace Mono.Linker.Tests.Cases.Libraries.Dependencies
{
public interface ISkipLibraryStaticInterface
{
static abstract void StaticInterfaceMethod ();
}
}
12 changes: 11 additions & 1 deletion test/Mono.Linker.Tests.Cases/Libraries/RootLibrary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
namespace Mono.Linker.Tests.Cases.Libraries
{
[SetupCompileBefore ("copylibrary.dll", new[] { "Dependencies/CopyLibrary.cs" })]
[SetupCompileBefore ("skiplibrary.dll", new[] { "Dependencies/SkipLibrary.cs" })]
[SetupLinkerAction ("copy", "copylibrary")]
[SetupLinkerAction ("skip", "skiplibrary")]
[SetupLinkerArgument ("-a", "test.exe", "library")]
[SetupLinkerArgument ("--enable-opt", "ipconstprop")]
[VerifyMetadataNames]
Expand Down Expand Up @@ -188,14 +190,16 @@ public interface I
[KeptInterface (typeof (IInternalStaticInterface))]
[KeptInterface (typeof (ICopyLibraryInterface))]
[KeptInterface (typeof (ICopyLibraryStaticInterface))]
[KeptInterface (typeof (ISkipLibraryStaticInterface))]
public class UninstantiatedPublicClassWithInterface :
IPublicInterface,
IPublicStaticInterface,
IInternalInterface,
IInternalStaticInterface,
IEnumerator,
ICopyLibraryInterface,
ICopyLibraryStaticInterface
ICopyLibraryStaticInterface,
ISkipLibraryStaticInterface
{
internal UninstantiatedPublicClassWithInterface () { }

Expand Down Expand Up @@ -241,7 +245,12 @@ void ICopyLibraryInterface.CopyLibraryExplicitImplementationInterfaceMethod () {
public static void CopyLibraryStaticInterfaceMethod () { }

[Kept]
[KeptOverride (typeof (ICopyLibraryStaticInterface))]
static void ICopyLibraryStaticInterface.CopyLibraryExplicitImplementationStaticInterfaceMethod () { }

[Kept]
[KeptOverride (typeof (ISkipLibraryStaticInterface))]
static void ISkipLibraryStaticInterface.StaticInterfaceMethod () { }
}

[Kept]
Expand Down Expand Up @@ -325,6 +334,7 @@ void ICopyLibraryInterface.CopyLibraryExplicitImplementationInterfaceMethod () {
public static void CopyLibraryStaticInterfaceMethod () { }

[Kept]
[KeptOverride (typeof (ICopyLibraryStaticInterface))]
static void ICopyLibraryStaticInterface.CopyLibraryExplicitImplementationStaticInterfaceMethod () { }
}

Expand Down
4 changes: 2 additions & 2 deletions test/Mono.Linker.Tests/TestCasesRunner/AssemblyChecker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ void VerifyOverrides (MethodDefinition original, MethodDefinition linked)
return;
var expectedBaseTypesOverridden = new HashSet<string> (original.CustomAttributes
.Where (ca => ca.AttributeType.Name == nameof (KeptOverrideAttribute))
.Select (ca => (ca.ConstructorArguments[0].Value as TypeDefinition).FullName));
.Select (ca => (ca.ConstructorArguments[0].Value as TypeReference).FullName));
var originalBaseTypesOverridden = new HashSet<string> (original.Overrides.Select (ov => ov.DeclaringType.FullName));
var linkedBaseTypesOverridden = new HashSet<string> (linked.Overrides.Select (ov => ov.DeclaringType.FullName));
foreach (var expectedBaseType in expectedBaseTypesOverridden) {
Expand All @@ -232,7 +232,7 @@ void VerifyOverrides (MethodDefinition original, MethodDefinition linked)

var expectedBaseTypesNotOverridden = new HashSet<string> (original.CustomAttributes
.Where (ca => ca.AttributeType.Name == nameof (RemovedOverrideAttribute))
.Select (ca => (ca.ConstructorArguments[0].Value as TypeDefinition).FullName));
.Select (ca => (ca.ConstructorArguments[0].Value as TypeReference).FullName));
foreach (var expectedRemovedBaseType in expectedBaseTypesNotOverridden) {
Assert.IsTrue (originalBaseTypesOverridden.Contains (expectedRemovedBaseType),
$"Method {linked.FullName} was expected to remove override {expectedRemovedBaseType}::{linked.Name}, " +
Expand Down