diff --git a/src/coreclr/tools/aot/ILCompiler.MetadataTransform/ILCompiler/Metadata/Transform.Type.cs b/src/coreclr/tools/aot/ILCompiler.MetadataTransform/ILCompiler/Metadata/Transform.Type.cs index ca81bd42d8e4c2..6ce9159a12dafd 100644 --- a/src/coreclr/tools/aot/ILCompiler.MetadataTransform/ILCompiler/Metadata/Transform.Type.cs +++ b/src/coreclr/tools/aot/ILCompiler.MetadataTransform/ILCompiler/Metadata/Transform.Type.cs @@ -261,17 +261,43 @@ private void InitializeTypeDef(Cts.MetadataType entity, TypeDefinition record) record.PackingSize = checked((ushort)layoutMetadata.PackingSize); record.Flags = GetTypeAttributes(entity); - if (entity.HasBaseType) + try { - record.BaseType = HandleType(entity.BaseType); + if (entity.HasBaseType) + { + record.BaseType = HandleType(entity.BaseType); + } + } + catch (Cts.TypeSystemException) when (HasNestedTypes(entity)) + { + // We might have been forced to generate metadata for a type + // that wasn't looked at during code generation because it's an owning + // type of a type we did look at. Allow those to generate incomplete + // metadata. The ultimate fix is to rewrite metadata generation to be + // System.Reflection.Metadata-based as opposed to type system based. + // If there's no nested types, this is a bug and should tear down + // the compiler at this point. } - record.Interfaces.Capacity = entity.ExplicitlyImplementedInterfaces.Length; - foreach (var interfaceType in entity.ExplicitlyImplementedInterfaces) + try + { + record.Interfaces.Capacity = entity.ExplicitlyImplementedInterfaces.Length; + foreach (var interfaceType in entity.ExplicitlyImplementedInterfaces) + { + if (IsBlocked(interfaceType)) + continue; + record.Interfaces.Add(HandleType(interfaceType)); + } + } + catch (Cts.TypeSystemException) when (HasNestedTypes(entity)) { - if (IsBlocked(interfaceType)) - continue; - record.Interfaces.Add(HandleType(interfaceType)); + // We might have been forced to generate metadata for a type + // that wasn't looked at during code generation because it's an owning + // type of a type we did look at. Allow those to generate incomplete + // metadata. The ultimate fix is to rewrite metadata generation to be + // System.Reflection.Metadata-based as opposed to type system based. + // If there's no nested types, this is a bug and should tear down + // the compiler at this point. } if (entity.HasInstantiation) @@ -346,6 +372,9 @@ private void InitializeTypeDef(Cts.MetadataType entity, TypeDefinition record) record.MethodImpls.Add(methodImplRecord); }*/ } + + static bool HasNestedTypes(Cts.MetadataType entity) + => entity.GetNestedTypes().GetEnumerator().MoveNext(); } private TypeAttributes GetTypeAttributes(Cts.MetadataType type)