Guard DynamicDependency processing against `TypeSystemException`s (#85285)
authorMichal Strehovský <MichalStrehovsky@users.noreply.github.com>
Wed, 26 Apr 2023 01:09:13 +0000 (10:09 +0900)
committerGitHub <noreply@github.com>
Wed, 26 Apr 2023 01:09:13 +0000 (10:09 +0900)
Works around https://github.com/dotnet/fsharp/issues/15140 and it's also just good hygiene.

src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/DependencyAnalysis/DynamicDependencyAttributesOnEntityNode.cs

index 95fcd87..7c07dfd 100644 (file)
@@ -55,21 +55,22 @@ namespace ILCompiler.DependencyAnalysis
         public override IEnumerable<DependencyListEntry> GetStaticDependencies(NodeFactory factory)
         {
             DependencyList dependencies = null;
-            switch (_entity)
+            try
             {
-                case EcmaMethod method:
-                    foreach (var attribute in method.GetDecodedCustomAttributes("System.Diagnostics.CodeAnalysis", "DynamicDependencyAttribute"))
-                    {
-                        AddDependenciesDueToDynamicDependencyAttribute(ref dependencies, factory, method, method.OwningType, attribute);
-                    }
-                    break;
+                (TypeDesc owningType, IEnumerable<CustomAttributeValue<TypeDesc>> attributes) = _entity switch
+                {
+                    EcmaMethod method => (method.OwningType, method.GetDecodedCustomAttributes("System.Diagnostics.CodeAnalysis", "DynamicDependencyAttribute")),
+                    _ => (((EcmaField)_entity).OwningType, ((EcmaField)_entity).GetDecodedCustomAttributes("System.Diagnostics.CodeAnalysis", "DynamicDependencyAttribute")),
+                };
 
-                case EcmaField field:
-                    foreach (var attribute in field.GetDecodedCustomAttributes("System.Diagnostics.CodeAnalysis", "DynamicDependencyAttribute"))
-                    {
-                        AddDependenciesDueToDynamicDependencyAttribute(ref dependencies, factory, field, field.OwningType, attribute);
-                    }
-                    break;
+                foreach (CustomAttributeValue<TypeDesc> attribute in attributes)
+                {
+                    AddDependenciesDueToDynamicDependencyAttribute(ref dependencies, factory, _entity, owningType, attribute);
+                }
+            }
+            catch (TypeSystemException)
+            {
+                // Ignore entities with custom attributes that don't work.
             }
 
             return dependencies;