Skip to content

Commit 5eee321

Browse files
authored
Fix marking of nested type forwarders (#2385)
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)
1 parent 0f21f2d commit 5eee321

File tree

5 files changed

+23
-17
lines changed

5 files changed

+23
-17
lines changed

src/linker/Linker.Steps/MarkStep.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1443,7 +1443,7 @@ protected override void ProcessTypeReference (TypeReference type)
14431443
protected override void ProcessExportedType (ExportedType exportedType)
14441444
{
14451445
markingHelpers.MarkExportedType (exportedType, assembly.MainModule, new DependencyInfo (DependencyKind.ExportedType, assembly));
1446-
markingHelpers.MarkForwardedScope (new TypeReference (exportedType.Namespace, exportedType.Name, assembly.MainModule, exportedType.Scope));
1446+
markingHelpers.MarkForwardedScope (CreateTypeReferenceForExportedTypeTarget (exportedType));
14471447
}
14481448

14491449
protected override void ProcessExtra ()
@@ -1456,6 +1456,18 @@ protected override void ProcessExtra ()
14561456
markingHelpers.MarkForwardedScope (typeReference);
14571457
}
14581458
}
1459+
1460+
TypeReference CreateTypeReferenceForExportedTypeTarget (ExportedType exportedType)
1461+
{
1462+
TypeReference? declaringTypeReference = null;
1463+
if (exportedType.DeclaringType != null) {
1464+
declaringTypeReference = CreateTypeReferenceForExportedTypeTarget (exportedType.DeclaringType);
1465+
}
1466+
1467+
return new TypeReference (exportedType.Namespace, exportedType.Name, assembly.MainModule, exportedType.Scope) {
1468+
DeclaringType = declaringTypeReference
1469+
};
1470+
}
14591471
}
14601472

14611473
void ProcessModuleType (AssemblyDefinition assembly)

test/Mono.Linker.Tests.Cases/TypeForwarding/Dependencies/NestedForwarderLibrary.il

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
}
2626
.class extern ForwardedNestedType
2727
{
28-
.class extern 'Mono.Linker.Tests.Cases.TypeForwarding.Dependencies.ImplementationLibrary'
28+
.class extern 'Mono.Linker.Tests.Cases.TypeForwarding.Dependencies.AnotherImplementationClass'
2929
}
3030

3131
.module 'NestedForwarderLibrary.dll'

test/Mono.Linker.Tests.Cases/TypeForwarding/Dependencies/NestedForwarderLibrary_2.il

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
}
2626
.class extern ForwardedNestedType
2727
{
28-
.class extern 'Mono.Linker.Tests.Cases.TypeForwarding.Dependencies.ImplementationLibrary'
28+
.class extern 'Mono.Linker.Tests.Cases.TypeForwarding.Dependencies.AnotherImplementationClass'
2929
}
3030

3131
.module 'NestedForwarderLibrary_2.dll'

test/Mono.Linker.Tests.Cases/TypeForwarding/MultiForwardedTypesWithCopyUsed.cs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,10 @@ namespace Mono.Linker.Tests.Cases.TypeForwarding
2222
[SetupLinkerAction ("copyused", "NestedForwarderLibrary")]
2323
[SetupLinkerAction ("copyused", "Implementation")]
2424

25-
// https://github.com/dotnet/linker/issues/2359
26-
// One of the type forwarders in NestedForwarderLibrary will not be kept.
27-
// Which one depends on order.
28-
//[KeptTypeInAssembly ("NestedForwarderLibrary.dll", typeof (ImplementationLibrary.ForwardedNestedType))]
29-
//[KeptTypeInAssembly ("NestedForwarderLibrary.dll", typeof (AnotherImplementationClass.ForwardedNestedType))]
30-
//[KeptTypeInAssembly ("NestedForwarderLibrary_2.dll", typeof (ImplementationLibrary.ForwardedNestedType))]
31-
//[KeptTypeInAssembly ("NestedForwarderLibrary_2.dll", typeof (AnotherImplementationClass.ForwardedNestedType))]
25+
[KeptTypeInAssembly ("NestedForwarderLibrary.dll", typeof (ImplementationLibrary.ForwardedNestedType))]
26+
[KeptTypeInAssembly ("NestedForwarderLibrary.dll", typeof (AnotherImplementationClass.ForwardedNestedType))]
27+
[KeptTypeInAssembly ("NestedForwarderLibrary_2.dll", typeof (ImplementationLibrary.ForwardedNestedType))]
28+
[KeptTypeInAssembly ("NestedForwarderLibrary_2.dll", typeof (AnotherImplementationClass.ForwardedNestedType))]
3229
[KeptTypeInAssembly ("Implementation.dll", typeof (ImplementationLibrary.ForwardedNestedType))]
3330
[KeptTypeInAssembly ("Implementation.dll", typeof (AnotherImplementationClass.ForwardedNestedType))]
3431

test/Mono.Linker.Tests.Cases/TypeForwarding/MultiForwardedTypesWithLink.cs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,10 @@ namespace Mono.Linker.Tests.Cases.TypeForwarding
2222
[SetupLinkerAction ("link", "NestedForwarderLibrary")]
2323
[SetupLinkerAction ("link", "Implementation")]
2424

25-
// https://github.com/dotnet/linker/issues/2359
26-
// One of the type forwarders in NestedForwarderLibrary will not be kept.
27-
// Which one depends on order.
28-
//[KeptTypeInAssembly ("NestedForwarderLibrary.dll", typeof (ImplementationLibrary.ForwardedNestedType))]
29-
//[KeptTypeInAssembly ("NestedForwarderLibrary.dll", typeof (AnotherImplementationClass.ForwardedNestedType))]
30-
//[KeptTypeInAssembly ("NestedForwarderLibrary_2.dll", typeof (ImplementationLibrary.ForwardedNestedType))]
31-
//[KeptTypeInAssembly ("NestedForwarderLibrary_2.dll", typeof (AnotherImplementationClass.ForwardedNestedType))]
25+
[KeptTypeInAssembly ("NestedForwarderLibrary.dll", typeof (ImplementationLibrary.ForwardedNestedType))]
26+
[KeptTypeInAssembly ("NestedForwarderLibrary.dll", typeof (AnotherImplementationClass.ForwardedNestedType))]
27+
[KeptTypeInAssembly ("NestedForwarderLibrary_2.dll", typeof (ImplementationLibrary.ForwardedNestedType))]
28+
[KeptTypeInAssembly ("NestedForwarderLibrary_2.dll", typeof (AnotherImplementationClass.ForwardedNestedType))]
3229
[KeptTypeInAssembly ("Implementation.dll", typeof (ImplementationLibrary.ForwardedNestedType))]
3330
[KeptTypeInAssembly ("Implementation.dll", typeof (AnotherImplementationClass.ForwardedNestedType))]
3431

0 commit comments

Comments
 (0)