Skip to content

Commit

Permalink
Fix marking of nested type forwarders (#2385)
Browse files Browse the repository at this point in the history
When create a type reference for the target of a type forwarder, if the type forwarder is for a nested type, we have to build a whole tree of type references for all of the declaring types and not just the final nested type.

Enabled tests which were already added for this case (and fixed a bug in them)
  • Loading branch information
vitek-karas authored Nov 22, 2021
1 parent 0f21f2d commit 5eee321
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 17 deletions.
14 changes: 13 additions & 1 deletion src/linker/Linker.Steps/MarkStep.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1443,7 +1443,7 @@ protected override void ProcessTypeReference (TypeReference type)
protected override void ProcessExportedType (ExportedType exportedType)
{
markingHelpers.MarkExportedType (exportedType, assembly.MainModule, new DependencyInfo (DependencyKind.ExportedType, assembly));
markingHelpers.MarkForwardedScope (new TypeReference (exportedType.Namespace, exportedType.Name, assembly.MainModule, exportedType.Scope));
markingHelpers.MarkForwardedScope (CreateTypeReferenceForExportedTypeTarget (exportedType));
}

protected override void ProcessExtra ()
Expand All @@ -1456,6 +1456,18 @@ protected override void ProcessExtra ()
markingHelpers.MarkForwardedScope (typeReference);
}
}

TypeReference CreateTypeReferenceForExportedTypeTarget (ExportedType exportedType)
{
TypeReference? declaringTypeReference = null;
if (exportedType.DeclaringType != null) {
declaringTypeReference = CreateTypeReferenceForExportedTypeTarget (exportedType.DeclaringType);
}

return new TypeReference (exportedType.Namespace, exportedType.Name, assembly.MainModule, exportedType.Scope) {
DeclaringType = declaringTypeReference
};
}
}

void ProcessModuleType (AssemblyDefinition assembly)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
}
.class extern ForwardedNestedType
{
.class extern 'Mono.Linker.Tests.Cases.TypeForwarding.Dependencies.ImplementationLibrary'
.class extern 'Mono.Linker.Tests.Cases.TypeForwarding.Dependencies.AnotherImplementationClass'
}

.module 'NestedForwarderLibrary.dll'
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
}
.class extern ForwardedNestedType
{
.class extern 'Mono.Linker.Tests.Cases.TypeForwarding.Dependencies.ImplementationLibrary'
.class extern 'Mono.Linker.Tests.Cases.TypeForwarding.Dependencies.AnotherImplementationClass'
}

.module 'NestedForwarderLibrary_2.dll'
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,10 @@ namespace Mono.Linker.Tests.Cases.TypeForwarding
[SetupLinkerAction ("copyused", "NestedForwarderLibrary")]
[SetupLinkerAction ("copyused", "Implementation")]

// https://github.com/dotnet/linker/issues/2359
// One of the type forwarders in NestedForwarderLibrary will not be kept.
// Which one depends on order.
//[KeptTypeInAssembly ("NestedForwarderLibrary.dll", typeof (ImplementationLibrary.ForwardedNestedType))]
//[KeptTypeInAssembly ("NestedForwarderLibrary.dll", typeof (AnotherImplementationClass.ForwardedNestedType))]
//[KeptTypeInAssembly ("NestedForwarderLibrary_2.dll", typeof (ImplementationLibrary.ForwardedNestedType))]
//[KeptTypeInAssembly ("NestedForwarderLibrary_2.dll", typeof (AnotherImplementationClass.ForwardedNestedType))]
[KeptTypeInAssembly ("NestedForwarderLibrary.dll", typeof (ImplementationLibrary.ForwardedNestedType))]
[KeptTypeInAssembly ("NestedForwarderLibrary.dll", typeof (AnotherImplementationClass.ForwardedNestedType))]
[KeptTypeInAssembly ("NestedForwarderLibrary_2.dll", typeof (ImplementationLibrary.ForwardedNestedType))]
[KeptTypeInAssembly ("NestedForwarderLibrary_2.dll", typeof (AnotherImplementationClass.ForwardedNestedType))]
[KeptTypeInAssembly ("Implementation.dll", typeof (ImplementationLibrary.ForwardedNestedType))]
[KeptTypeInAssembly ("Implementation.dll", typeof (AnotherImplementationClass.ForwardedNestedType))]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,10 @@ namespace Mono.Linker.Tests.Cases.TypeForwarding
[SetupLinkerAction ("link", "NestedForwarderLibrary")]
[SetupLinkerAction ("link", "Implementation")]

// https://github.com/dotnet/linker/issues/2359
// One of the type forwarders in NestedForwarderLibrary will not be kept.
// Which one depends on order.
//[KeptTypeInAssembly ("NestedForwarderLibrary.dll", typeof (ImplementationLibrary.ForwardedNestedType))]
//[KeptTypeInAssembly ("NestedForwarderLibrary.dll", typeof (AnotherImplementationClass.ForwardedNestedType))]
//[KeptTypeInAssembly ("NestedForwarderLibrary_2.dll", typeof (ImplementationLibrary.ForwardedNestedType))]
//[KeptTypeInAssembly ("NestedForwarderLibrary_2.dll", typeof (AnotherImplementationClass.ForwardedNestedType))]
[KeptTypeInAssembly ("NestedForwarderLibrary.dll", typeof (ImplementationLibrary.ForwardedNestedType))]
[KeptTypeInAssembly ("NestedForwarderLibrary.dll", typeof (AnotherImplementationClass.ForwardedNestedType))]
[KeptTypeInAssembly ("NestedForwarderLibrary_2.dll", typeof (ImplementationLibrary.ForwardedNestedType))]
[KeptTypeInAssembly ("NestedForwarderLibrary_2.dll", typeof (AnotherImplementationClass.ForwardedNestedType))]
[KeptTypeInAssembly ("Implementation.dll", typeof (ImplementationLibrary.ForwardedNestedType))]
[KeptTypeInAssembly ("Implementation.dll", typeof (AnotherImplementationClass.ForwardedNestedType))]

Expand Down

0 comments on commit 5eee321

Please sign in to comment.