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

Don't mark override of abstract base if the override's declaring type is not marked #3098

Merged
merged 4 commits into from
Nov 2, 2022
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
2 changes: 2 additions & 0 deletions src/linker/Linker.Steps/MarkStep.cs
Original file line number Diff line number Diff line change
Expand Up @@ -723,6 +723,8 @@ void ProcessVirtualMethod (MethodDefinition method)
// TODO: Move interface method marking logic here https://github.com/dotnet/linker/issues/3090
bool ShouldMarkOverrideForBase (OverrideInformation overrideInformation)
{
if (!Annotations.IsMarked (overrideInformation.Override.DeclaringType))
return false;
if (overrideInformation.IsOverrideOfInterfaceMember) {
_interfaceOverrides.Add ((overrideInformation, ScopeStack.CurrentScope));
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ public Task NeverInstantiatedTypeWithBaseInCopiedAssembly ()
return RunTest (allowMissingWarnings: true);
}

[Fact]
public Task OverrideInUnmarkedClassIsRemoved ()
{
return RunTest (allowMissingWarnings: true);
}

[Fact]
public Task UnusedTypeWithOverrideOfVirtualMethodIsRemoved ()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
// 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.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Mono.Linker.Tests.Cases.Expectations.Assertions;
using Mono.Linker.Tests.Cases.Expectations.Metadata;

namespace Mono.Linker.Tests.Cases.Inheritance.VirtualMethods
{
public class OverrideOfAbstractInUnmarkedClassIsRemoved
{
[Kept]
public static void Main ()
{
MarkedBase x = new MarkedDerived ();
x.Method ();

UsedSecondLevelTypeWithAbstractBase y = new ();
y.Method ();

UsedSecondLevelType z = new ();
z.Method ();
}

[Kept]
[KeptMember (".ctor()")]
abstract class MarkedBase
{
[Kept]
public abstract int Method ();
}

[Kept]
[KeptMember (".ctor()")]
[KeptBaseType (typeof (MarkedBase))]
class MarkedDerived : MarkedBase
{
[Kept]
public override int Method () => 1;
}

class UnmarkedDerived : MarkedBase
{
public override int Method () => 1;
}

[Kept]
[KeptMember (".ctor()")]
[KeptBaseType (typeof (MarkedBase))]
class UnusedIntermediateType : MarkedBase
{
[Kept]
public override int Method () => 1;
}

[Kept]
[KeptMember (".ctor()")]
[KeptBaseType (typeof (UnusedIntermediateType))]
class UsedSecondLevelType : UnusedIntermediateType
{
[Kept]
public override int Method () => 1;
}

[Kept]
[KeptMember (".ctor()")]
[KeptBaseType (typeof (MarkedBase))]
abstract class UnusedIntermediateTypeWithAbstractOverride : MarkedBase
{
[Kept]
public abstract override int Method ();
}

[Kept]
[KeptMember (".ctor()")]
[KeptBaseType (typeof (UnusedIntermediateTypeWithAbstractOverride))]
class UsedSecondLevelTypeWithAbstractBase : UnusedIntermediateTypeWithAbstractOverride
{
[Kept]
public override int Method () => 1;
}

class UnusedSecondLevelTypeWithAbstractBase : UnusedIntermediateTypeWithAbstractOverride
{
public override int Method () => 1;
}

class UnusedSecondLevelType : UnusedIntermediateType
{
public override int Method () => 1;
}
}
}