From beeb9c1ef095fe98d663c0e979f809229e35cc97 Mon Sep 17 00:00:00 2001 From: michaelv Date: Wed, 4 Mar 2026 12:59:07 -0500 Subject: [PATCH] Adjustments to how compiler generated types are ignored. Unity is still verifying the ILLink tests mostly pass when ran through an old version of UnityLinker that still supports .NET Framework. We also have tests that will root types in `test.exe`. Both of these scenarios lead to compiler generated polyfills that survive and mess up asserting. `AssemblyChecker` had some logic to deal with compiler generated attributes. These specific attributes no longer appear to require special handling during the illink tests so I've removed them. In order to help Unity deal with it's tests I've exposed a new method `PrepareToVerifyAssembly` that we can hook into and do the same sort of thing that was happening for attributes such as `EmbeddedAttribute` but also do it for other types such as `NullableAttribute`. I've exposed a helper method `IgnoreTypeAndItsMembers` that makes it easier to ignore a type and it's members. --- .../TestCasesRunner/AssemblyChecker.cs | 28 +++++++++++++------ 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/src/tools/illink/test/Mono.Linker.Tests/TestCasesRunner/AssemblyChecker.cs b/src/tools/illink/test/Mono.Linker.Tests/TestCasesRunner/AssemblyChecker.cs index 383764e0aba9de..05f0c47812be1b 100644 --- a/src/tools/illink/test/Mono.Linker.Tests/TestCasesRunner/AssemblyChecker.cs +++ b/src/tools/illink/test/Mono.Linker.Tests/TestCasesRunner/AssemblyChecker.cs @@ -67,14 +67,7 @@ IEnumerable GetFailures() return s.FullName; }), StringComparer.Ordinal); - // Workaround for compiler injected attribute to describe the language version - linkedMembers.Remove("System.Void Microsoft.CodeAnalysis.EmbeddedAttribute::.ctor()"); - linkedMembers.Remove("System.Int32 System.Runtime.CompilerServices.RefSafetyRulesAttribute::Version"); - linkedMembers.Remove("System.Void System.Runtime.CompilerServices.RefSafetyRulesAttribute::.ctor(System.Int32)"); - - // Workaround for compiler injected attribute to describe the language version - verifiedGeneratedTypes.Add("Microsoft.CodeAnalysis.EmbeddedAttribute"); - verifiedGeneratedTypes.Add("System.Runtime.CompilerServices.RefSafetyRulesAttribute"); + PrepareToVerifyAssembly(originalAssembly); var membersToAssert = originalAssembly.MainModule.Types; foreach (var originalMember in membersToAssert) @@ -104,6 +97,25 @@ IEnumerable GetFailures() } } + /// + /// An opportunity to adjust what has been verified. Helpful for dealing with polyfills or compiler generated types + /// + /// + protected virtual void PrepareToVerifyAssembly(AssemblyDefinition original) + { + } + + /// + /// The type will not be verified + /// + /// + protected void IgnoreGeneratedTypeAndItsMembers(TypeDefinition type) + { + verifiedGeneratedTypes.Add(type.FullName); + foreach (var member in type.AllMembers()) + linkedMembers.Remove(member.FullName); + } + static bool IsBackingField(FieldDefinition field) => field.Name.StartsWith("<") && field.Name.EndsWith(">k__BackingField"); protected virtual IEnumerable VerifyModule(ModuleDefinition original, ModuleDefinition linked)