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

Allow TypePreserve info to be applied multiple times #103140

Merged
merged 4 commits into from
Jun 12, 2024
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
3 changes: 1 addition & 2 deletions src/tools/illink/src/linker/Linker.Steps/MarkStep.cs
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,6 @@ bool ProcessMarkedPending ()

foreach (var type in Annotations.GetPendingPreserve ()) {
marked = true;
Debug.Assert (Annotations.IsProcessed (type));
ApplyPreserveInfo (type);
}

Expand Down Expand Up @@ -2785,7 +2784,7 @@ void ApplyPreserveInfo (TypeDefinition type)

if (Annotations.TryGetPreserve (type, out TypePreserve preserve)) {
if (!Annotations.SetAppliedPreserve (type, preserve))
throw new InternalErrorException ($"Type {type} already has applied {preserve}.");
return;

var di = new DependencyInfo (DependencyKind.TypePreserve, type);

Expand Down
14 changes: 0 additions & 14 deletions src/tools/illink/src/linker/Linker/Annotations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Reflection.Metadata.Ecma335;
using ILLink.Shared.TrimAnalysis;
using Mono.Cecil;
using Mono.Cecil.Cil;
Expand Down Expand Up @@ -303,25 +302,12 @@ public bool SetAppliedPreserve (TypeDefinition type, TypePreserve preserve)
return true;
}

public bool HasAppliedPreserve (TypeDefinition type, TypePreserve preserve)
{
if (!preserved_types.TryGetValue (type, out (TypePreserve preserve, bool applied) existing))
throw new InternalErrorException ($"Type {type} must have a TypePreserve before it can be applied.");

if (preserve != existing.preserve)
throw new InternalErrorException ($"Type {type} does not have {preserve}. The TypePreserve may have changed before the call to {nameof (HasAppliedPreserve)}.");

return existing.applied;
}

public void SetPreserve (TypeDefinition type, TypePreserve preserve)
{
Debug.Assert (preserve != TypePreserve.Nothing);
if (!preserved_types.TryGetValue (type, out (TypePreserve preserve, bool applied) existing)) {
preserved_types.Add (type, (preserve, false));
if (IsProcessed (type)) {
// Required to track preserve for marked types where the existing preserve
// was Nothing (since these aren't explicitly tracked.)
var addedPending = pending_preserve.Add (type);
Debug.Assert (addedPending);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System;
using Mono.Linker.Tests.Cases.Expectations.Assertions;
using Mono.Linker.Tests.Cases.Expectations.Metadata;

namespace Mono.Linker.Tests.Cases.CommandLine
{
[SetupCompileBefore ("CustomApplyPreserveStep.dll", new[] { "Dependencies/CustomApplyPreserveStep.cs" }, new[] { "illink.dll", "Mono.Cecil.dll" }, addAsReference: false)]
[SetupLinkerArgument ("--custom-step", "-MarkStep:CustomStep.CustomApplyPreserveStep,CustomApplyPreserveStep.dll")]
[SetupLinkerArgument ("--verbose")]
public class CustomStepApplyPreserve
{
[Kept]
public class HasPreserveApplied
{
[Kept]
public int Field;

[Kept]
public HasPreserveApplied () { }

[Kept]
public void Method ()
{
}

public class Nested { }
}

[Kept]
public static void Main ()
{
Console.WriteLine (typeof (HasPreserveApplied).FullName);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace CustomStep
{
public class CustomApplyPreserveStep : Mono.Linker.Steps.IStep
{
public void Process(Mono.Linker.LinkContext context)
{
var myType = context.GetType("Mono.Linker.Tests.Cases.CommandLine.CustomStepApplyPreserve/HasPreserveApplied");
context.Annotations.SetPreserve(myType, Mono.Linker.TypePreserve.Methods);
// Make sure we can set preserve multiple times
context.Annotations.SetPreserve(myType, Mono.Linker.TypePreserve.All);
jtschuster marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
<Compile Remove="TypeForwarding\Dependencies\TypeForwardersRewriteForwarders.cs" />
<Compile Remove="CommandLine\Dependencies\CustomStepDummy.cs" />
<Compile Remove="CommandLine\Dependencies\CustomStepUser.cs" />
<Compile Remove="CommandLine\Dependencies\CustomApplyPreserveStep.cs" />
<Compile Remove="Logging\Dependencies\LogStep.cs" />
<Compile Remove="Extensibility\Dependencies\*.cs" />
<Compile Remove="DynamicDependencies\Dependencies\FacadeAssembly.cs" />
Expand Down
Loading