Skip to content
26 changes: 23 additions & 3 deletions src/coreclr/tools/Common/Compiler/ProcessLinkerXmlBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,8 @@ protected static string GetAttribute(XPathNavigator nav, string attribute)
public static string GetMethodSignature(MethodDesc meth, bool includeGenericParameters)
{
StringBuilder sb = new StringBuilder();
CecilTypeNameFormatter.Instance.AppendName(sb, meth.Signature.ReturnType);
var formatter = new CecilTypeNameFormatter(meth);
formatter.AppendName(sb, meth.Signature.ReturnType);
sb.Append(' ');
sb.Append(meth.GetName());
if (includeGenericParameters && meth.HasInstantiation)
Expand All @@ -545,7 +546,7 @@ public static string GetMethodSignature(MethodDesc meth, bool includeGenericPara
if (i > 0)
sb.Append(',');

CecilTypeNameFormatter.Instance.AppendName(sb, meth.Signature[i]);
formatter.AppendName(sb, meth.Signature[i]);
}

sb.Append(')');
Expand All @@ -572,7 +573,14 @@ protected void LogWarning(XPathNavigator position, DiagnosticId id, params strin

private sealed class CecilTypeNameFormatter : TypeNameFormatter
{
public static readonly CecilTypeNameFormatter Instance = new CecilTypeNameFormatter();
public static readonly CecilTypeNameFormatter Instance = new CecilTypeNameFormatter(null);

private readonly MethodDesc? _method;

public CecilTypeNameFormatter(MethodDesc? method)
{
_method = method;
}

public override void AppendName(StringBuilder sb, ArrayType type)
{
Expand Down Expand Up @@ -620,9 +628,21 @@ public override void AppendName(StringBuilder sb, GenericParameterDesc type)
}
public override void AppendName(StringBuilder sb, SignatureMethodVariable type)
{
if (_method is not null &&
type.Index < _method.Instantiation.Length &&
_method.Instantiation[type.Index] is GenericParameterDesc genericParameter)
{
sb.Append(genericParameter.Name);
}
}
public override void AppendName(StringBuilder sb, SignatureTypeVariable type)
{
if (_method is not null &&
type.Index < _method.OwningType.Instantiation.Length &&
_method.OwningType.Instantiation[type.Index] is GenericParameterDesc genericParameter)
{
sb.Append(genericParameter.Name);
}
}
protected override void AppendNameForInstantiatedType(StringBuilder sb, DefType type)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
using Internal.TypeSystem;
using Internal.TypeSystem.Ecma;

using Mono.Linker;

namespace ILCompiler.DependencyAnalysis
{
/// <summary>
Expand Down Expand Up @@ -171,7 +173,7 @@ protected override EntityHandle WriteInternal(ModuleWritingContext writeContext)

EcmaType ecmaType = (EcmaType)_module.GetObject(methodDef.GetDeclaringType());
MethodBodyNode bodyNode = writeContext.Factory.MethodBody(_module, Handle);
int bodyOffset = bodyNode.Marked
int bodyOffset = bodyNode.Marked || !writeContext.Factory.Settings.Optimizations.IsEnabled(CodeOptimizations.UnreachableBodies, _module.Assembly.GetName().Name)
? bodyNode.Write(writeContext)
: writeContext.WriteUnreachableMethodBody(Handle, _module);

Expand Down
1 change: 0 additions & 1 deletion src/coreclr/tools/ILTrim.Tests/ILTrimExpectedFailures.txt
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,6 @@ LinkXml.EmbeddedLinkXmlPreservesAdditionalAssemblyWithOverriddenMethod
LinkXml.LinkXmlErrorCases
LinkXml.UnusedFieldPreservedByLinkXmlIsKept
LinkXml.UnusedInterfaceTypeOnTypeWithPreserveAllIsKept
LinkXml.UnusedMethodPreservedByLinkXmlIsKept
LinkXml.UnusedNonRequiredTypeIsRemoved
LinkXml.UnusedTypeWithPreserveFieldsHasMethodsRemoved
LinkXml.UnusedTypeWithPreserveNothingAndPreserveMembers
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,11 @@ public virtual TestCaseLinkerOptions GetLinkerOptions(NPath inputPath)
tclo.AdditionalArguments.Add(new KeyValuePair<string, string[]>("-a", new[] { assemblyName, "all" }));
}

#if ILTRIM
if (_testCase.DisplayName == "LinkXml.UnusedMethodPreservedByLinkXmlIsKept")
tclo.AdditionalArguments.Add(new KeyValuePair<string, string[]>("--disable-opt", new[] { "unreachablebodies", "test" }));
#endif
Comment thread
MichalStrehovsky marked this conversation as resolved.
Outdated

return tclo;
}

Expand Down
Loading