From 5eee321d050b14f0399233895b7b4517e845b73f Mon Sep 17 00:00:00 2001 From: Vitek Karas Date: Mon, 22 Nov 2021 14:50:31 +0100 Subject: [PATCH] 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) --- src/linker/Linker.Steps/MarkStep.cs | 14 +++++++++++++- .../Dependencies/NestedForwarderLibrary.il | 2 +- .../Dependencies/NestedForwarderLibrary_2.il | 2 +- .../MultiForwardedTypesWithCopyUsed.cs | 11 ++++------- .../TypeForwarding/MultiForwardedTypesWithLink.cs | 11 ++++------- 5 files changed, 23 insertions(+), 17 deletions(-) diff --git a/src/linker/Linker.Steps/MarkStep.cs b/src/linker/Linker.Steps/MarkStep.cs index 4921299ef311..7fe2319395ec 100644 --- a/src/linker/Linker.Steps/MarkStep.cs +++ b/src/linker/Linker.Steps/MarkStep.cs @@ -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 () @@ -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) diff --git a/test/Mono.Linker.Tests.Cases/TypeForwarding/Dependencies/NestedForwarderLibrary.il b/test/Mono.Linker.Tests.Cases/TypeForwarding/Dependencies/NestedForwarderLibrary.il index edb8ded478bc..572248fab454 100644 --- a/test/Mono.Linker.Tests.Cases/TypeForwarding/Dependencies/NestedForwarderLibrary.il +++ b/test/Mono.Linker.Tests.Cases/TypeForwarding/Dependencies/NestedForwarderLibrary.il @@ -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' diff --git a/test/Mono.Linker.Tests.Cases/TypeForwarding/Dependencies/NestedForwarderLibrary_2.il b/test/Mono.Linker.Tests.Cases/TypeForwarding/Dependencies/NestedForwarderLibrary_2.il index 2b0faefc622e..84c16d84cf43 100644 --- a/test/Mono.Linker.Tests.Cases/TypeForwarding/Dependencies/NestedForwarderLibrary_2.il +++ b/test/Mono.Linker.Tests.Cases/TypeForwarding/Dependencies/NestedForwarderLibrary_2.il @@ -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' diff --git a/test/Mono.Linker.Tests.Cases/TypeForwarding/MultiForwardedTypesWithCopyUsed.cs b/test/Mono.Linker.Tests.Cases/TypeForwarding/MultiForwardedTypesWithCopyUsed.cs index e2e5c39bfbbd..a75bce83325f 100644 --- a/test/Mono.Linker.Tests.Cases/TypeForwarding/MultiForwardedTypesWithCopyUsed.cs +++ b/test/Mono.Linker.Tests.Cases/TypeForwarding/MultiForwardedTypesWithCopyUsed.cs @@ -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))] diff --git a/test/Mono.Linker.Tests.Cases/TypeForwarding/MultiForwardedTypesWithLink.cs b/test/Mono.Linker.Tests.Cases/TypeForwarding/MultiForwardedTypesWithLink.cs index 2d61e1e6b6d4..6500443d5855 100644 --- a/test/Mono.Linker.Tests.Cases/TypeForwarding/MultiForwardedTypesWithLink.cs +++ b/test/Mono.Linker.Tests.Cases/TypeForwarding/MultiForwardedTypesWithLink.cs @@ -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))]