diff options
author | Vitek Karas <vitek.karas@microsoft.com> | 2021-09-08 12:13:57 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-09-08 12:13:57 +0300 |
commit | a9888c29d40a725caa89473fee383afb4a2edcd9 (patch) | |
tree | 8eb3fee103120b793c18f7188579841108568a1b | |
parent | 43fb979a719c5c5b6e83469ee5e36fb725c21e2c (diff) |
Workaround NRE in SweepStep (#2264)
Basically a workaround for #2260. This simply goes back to using the `Resolve` method and avoid the cache for now.
-rw-r--r-- | src/linker/Linker.Steps/SweepStep.cs | 18 | ||||
-rw-r--r-- | test/Mono.Linker.Tests.Cases/Inheritance.AbstractClasses/NoKeptCtor/OverrideRemoval/OverrideOfAbstractIsKeptNonEmpty.cs | 8 |
2 files changed, 13 insertions, 13 deletions
diff --git a/src/linker/Linker.Steps/SweepStep.cs b/src/linker/Linker.Steps/SweepStep.cs index cd9b17f3e..797b15781 100644 --- a/src/linker/Linker.Steps/SweepStep.cs +++ b/src/linker/Linker.Steps/SweepStep.cs @@ -256,7 +256,7 @@ namespace Mono.Linker.Steps SweepAssemblyReferences (assembly); } - bool SweepAssemblyReferences (AssemblyDefinition assembly) + static bool SweepAssemblyReferences (AssemblyDefinition assembly) { // // We used to run over list returned by GetTypeReferences but @@ -266,7 +266,7 @@ namespace Mono.Linker.Steps // assembly.MainModule.AssemblyReferences.Clear (); - var ars = new AssemblyReferencesCorrector (assembly, Context); + var ars = new AssemblyReferencesCorrector (assembly); return ars.Process (); } @@ -568,16 +568,14 @@ namespace Mono.Linker.Steps { readonly AssemblyDefinition assembly; readonly DefaultMetadataImporter importer; - readonly ITryResolveMetadata resolver; HashSet<TypeReference> updated; bool changedAnyScopes; - public AssemblyReferencesCorrector (AssemblyDefinition assembly, ITryResolveMetadata resolver) + public AssemblyReferencesCorrector (AssemblyDefinition assembly) { this.assembly = assembly; this.importer = new DefaultMetadataImporter (assembly.MainModule); - this.resolver = resolver; updated = null; changedAnyScopes = false; @@ -925,7 +923,15 @@ namespace Mono.Linker.Steps // // Resolve to type definition to remove any type forwarding imports // - TypeDefinition td = resolver.TryResolve (type); + // Workaround for https://github.com/mono/linker/issues/2260 + // Context has a cache which stores ref->def mapping. This code runs during sweeping + // which can remove the type-def from its assembly, effectively making the ref unresolvable. + // But the cache doesn't know that, it would still "resolve" the type-ref to now defunct type-def. + // For this reason we can't use the context resolution here, and must force Cecil to perform + // real type resolution again (since it can fail, and that's OK). +#pragma warning disable RS0030 // Do not used banned APIs + TypeDefinition td = type.Resolve (); +#pragma warning restore RS0030 // Do not used banned APIs if (td == null) { // // This can happen when not all assembly refences were provided and we diff --git a/test/Mono.Linker.Tests.Cases/Inheritance.AbstractClasses/NoKeptCtor/OverrideRemoval/OverrideOfAbstractIsKeptNonEmpty.cs b/test/Mono.Linker.Tests.Cases/Inheritance.AbstractClasses/NoKeptCtor/OverrideRemoval/OverrideOfAbstractIsKeptNonEmpty.cs index 593465da5..f4dea2347 100644 --- a/test/Mono.Linker.Tests.Cases/Inheritance.AbstractClasses/NoKeptCtor/OverrideRemoval/OverrideOfAbstractIsKeptNonEmpty.cs +++ b/test/Mono.Linker.Tests.Cases/Inheritance.AbstractClasses/NoKeptCtor/OverrideRemoval/OverrideOfAbstractIsKeptNonEmpty.cs @@ -15,9 +15,7 @@ namespace Mono.Linker.Tests.Cases.Inheritance.AbstractClasses.NoKeptCtor.Overrid [KeptAssembly ("library.dll")] [KeptAssembly ("librarywithnonempty.dll")] - // Uncomment after this bug is fixed - // https://github.com/mono/linker/issues/2260 - //[RemovedTypeInAssembly ("library.dll", typeof (Dependencies.OverrideOfAbstractIsKeptNonEmpty_UnusedType))] + [RemovedTypeInAssembly ("library.dll", typeof (Dependencies.OverrideOfAbstractIsKeptNonEmpty_UnusedType))] public class OverrideOfAbstractIsKeptNonEmpty { @@ -28,10 +26,6 @@ namespace Mono.Linker.Tests.Cases.Inheritance.AbstractClasses.NoKeptCtor.Overrid Dependencies.OverrideOfAbstractIsKeptNonEmpty_BaseType c = HelperToMarkLibraryAndRequireItsBase (); c.Method (); - - // Remove after this bug is fixed - // https://github.com/mono/linker/issues/2260 - new Dependencies.OverrideOfAbstractIsKeptNonEmpty_UnusedType (); } [Kept] |