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
9 changes: 6 additions & 3 deletions src/coreclr/tools/Common/Compiler/ProcessLinkerXmlBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -217,9 +217,10 @@ protected virtual void ProcessTypes(ModuleDesc assembly, XPathNavigator nav, boo
continue;
}

// TODO: Process exported types
MetadataType? type = CecilCompatibleTypeParser.GetType(assembly, fullname);

TypeDesc? type = CecilCompatibleTypeParser.GetType(assembly, fullname);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was already returning a resolved type, so exported types worked, but we "forgot" we got to the type through a forwarder and didn't preserve it. The newly added type.Module != assembly is basically "was this a forwarder?".

if (type != null && type.Module != assembly)
type = ProcessExportedType(type, assembly, typeNav);

if (type == null)
{
Expand All @@ -234,6 +235,8 @@ protected virtual void ProcessTypes(ModuleDesc assembly, XPathNavigator nav, boo
}
}

protected virtual MetadataType? ProcessExportedType(MetadataType exported, ModuleDesc assembly, XPathNavigator nav) => exported;

private void MatchType(TypeDesc type, Regex regex, XPathNavigator nav)
{
StringBuilder sb = new StringBuilder();
Expand Down Expand Up @@ -759,7 +762,7 @@ public bool TryConvertValue(string value, TypeDesc type, out object? result)

public class CecilCompatibleTypeParser
{
public static TypeDesc? GetType(ModuleDesc assembly, string fullName)
public static MetadataType? GetType(ModuleDesc assembly, string fullName)
{
Debug.Assert(!string.IsNullOrEmpty(fullName));
var position = fullName.IndexOf('/');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ protected override void ProcessAssembly(ModuleDesc assembly, XPathNavigator nav,
ProcessTypes(assembly, nav, warnOnUnresolvedTypes);
}

// protected override TypeDesc? ProcessExportedType(ExportedType exported, ModuleDesc assembly, XPathNavigator nav) => null;
protected override MetadataType ProcessExportedType(MetadataType exported, ModuleDesc assembly, XPathNavigator nav) => null;

protected override bool ProcessTypePattern(string fullname, ModuleDesc assembly, XPathNavigator nav) => false;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,14 @@ private void MarkAndPreserve(TypeDesc type, XPathNavigator nav, TypePreserve pre
}
#endif

protected override MetadataType? ProcessExportedType(MetadataType exported, ModuleDesc assembly, XPathNavigator nav)
{
// Rooting module metadata roots type forwarder metadata for all types in the module that are reflection visible.
// (We don't track individual type forwarders right now.)
_dependencies.Add(_factory.ModuleMetadata(assembly), "Type used through forwarder");
return base.ProcessExportedType(exported, assembly, nav);
}

protected override void ProcessType(TypeDesc type, XPathNavigator nav)
{
Debug.Assert(ShouldProcessElement(nav));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public static int Run()
ThrowIfMemberNotPresent(typeof(ILLinkDescriptor), nameof(PropertyKeptViaDescriptor));
ThrowIfMemberNotPresent(typeof(ILLinkDescriptor), nameof(EventKeptViaDescriptor));
ThrowIfTypeNotPresent(typeof(ILLinkDescriptor), nameof(NestedTypeKeptViaDescriptor));
ThrowIfTypeNotPresent("LibraryClass, ShimLibrary");
ThrowIfTypePresent(typeof(ILLinkDescriptor), nameof(NestedTypeNonKept));
return 100;
}
Expand Down Expand Up @@ -51,6 +52,10 @@ class NestedTypeNonKept
Justification = "That's the point")]
private static bool IsTypePresent(Type testType, string typeName) => testType.GetNestedType(typeName, BindingFlags.NonPublic | BindingFlags.Public) != null;

[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2057:UnrecognizedReflectionPattern",
Justification = "That's the point")]
private static bool IsTypePresent(string typeName) => Type.GetType(typeName) != null;

[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2070:UnrecognizedReflectionPattern",
Justification = "That's the point")]
private static bool IsMemberPresent(Type testType, string memberName) {
Expand All @@ -70,6 +75,14 @@ private static void ThrowIfTypeNotPresent(Type testType, string typeName)
}
}

private static void ThrowIfTypeNotPresent(string typeName)
{
if (!IsTypePresent(typeName))
{
throw new Exception(typeName);
}
}

private static void ThrowIfTypePresent(Type testType, string typeName)
{
if (IsTypePresent(testType, typeName))
Expand Down
4 changes: 4 additions & 0 deletions src/tests/nativeaot/SmokeTests/TrimmingBehaviors/Library.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

public class LibraryClass { }
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Library</OutputType>
</PropertyGroup>
<ItemGroup>
<Compile Include="Library.cs" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,8 @@
<method name="methodKeptViaStandaloneDescriptor"/>
</type>
</assembly>

<assembly fullname="ShimLibrary">
<type fullname="LibraryClass" />
</assembly>
</linker>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Runtime.CompilerServices;

[assembly: TypeForwardedTo(typeof(LibraryClass))]
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Library</OutputType>
</PropertyGroup>
<ItemGroup>
<Compile Include="ShimLibrary.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="Library.csproj" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@
<!-- We don't run the scanner in optimized builds -->
<CLRTestTargetUnsupported Condition="'$(IlcMultiModule)' == 'true'">true</CLRTestTargetUnsupported>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="ShimLibrary.csproj" />
</ItemGroup>

<ItemGroup>
<Compile Include="Dataflow.cs" />
<Compile Include="DeadCodeElimination.cs" />
Expand Down