diff options
author | David Wrighton <davidwr@microsoft.com> | 2021-03-31 20:50:25 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-03-31 20:50:25 +0300 |
commit | 4581290e1b608e08ff628cceb6de2f3365a9912d (patch) | |
tree | 5bcfd1d4df5a91248a16284f1af133194725581e /src/coreclr/tools/aot | |
parent | 94bb5d2f5c64e2059a64fc76680ae6c2246392a2 (diff) |
Refactor missing reference errors to allow not throwing for all cases (#50437)
- Build the concept of a cacheable resolution failure
- Plumb it through the Ecma type loader and the required public api surfaces
- Use it within the Mibc parser to avoid throwing
Diffstat (limited to 'src/coreclr/tools/aot')
3 files changed, 27 insertions, 9 deletions
diff --git a/src/coreclr/tools/aot/ILCompiler.ReadyToRun/IBC/IBCProfileParser.cs b/src/coreclr/tools/aot/ILCompiler.ReadyToRun/IBC/IBCProfileParser.cs index b26d9ab6226..8b419d4b395 100644 --- a/src/coreclr/tools/aot/ILCompiler.ReadyToRun/IBC/IBCProfileParser.cs +++ b/src/coreclr/tools/aot/ILCompiler.ReadyToRun/IBC/IBCProfileParser.cs @@ -106,7 +106,7 @@ namespace ILCompiler.IBC case CorTokenType.mdtMethodDef: case CorTokenType.mdtMemberRef: case CorTokenType.mdtMethodSpec: - object metadataObject = ecmaModule.GetObject(System.Reflection.Metadata.Ecma335.MetadataTokens.EntityHandle((int)entry.Token)); + object metadataObject = ecmaModule.GetObject(System.Reflection.Metadata.Ecma335.MetadataTokens.EntityHandle((int)entry.Token), NotFoundBehavior.ReturnNull); if (metadataObject is MethodDesc) { associatedMethod = (MethodDesc)metadataObject; @@ -346,7 +346,7 @@ namespace ILCompiler.IBC if (!(m is EcmaModule)) continue; - foundType = (EcmaType)m.GetType(typeNamespace, typeName, throwIfNotFound: false); + foundType = (EcmaType)m.GetType(typeNamespace, typeName, NotFoundBehavior.ReturnNull); if (foundType != null) { externalModule = foundType.EcmaModule; @@ -356,7 +356,7 @@ namespace ILCompiler.IBC } else { - foundType = (EcmaType)externalModule.GetType(typeNamespace, typeName, throwIfNotFound: false); + foundType = (EcmaType)externalModule.GetType(typeNamespace, typeName, NotFoundBehavior.ReturnNull); } if (foundType == null) @@ -451,7 +451,7 @@ namespace ILCompiler.IBC { if (EcmaModule.MetadataReader.GetTableRowCount(TableIndex.AssemblyRef) < index) return null; - return EcmaModule.GetObject(MetadataTokens.EntityHandle(((int)CorTokenType.mdtAssemblyRef) | index)) as EcmaModule; + return EcmaModule.GetObject(MetadataTokens.EntityHandle(((int)CorTokenType.mdtAssemblyRef) | index), NotFoundBehavior.ReturnNull) as EcmaModule; } } diff --git a/src/coreclr/tools/aot/ILCompiler.ReadyToRun/IBC/MIbcProfileParser.cs b/src/coreclr/tools/aot/ILCompiler.ReadyToRun/IBC/MIbcProfileParser.cs index 987300df24b..22e54a8922a 100644 --- a/src/coreclr/tools/aot/ILCompiler.ReadyToRun/IBC/MIbcProfileParser.cs +++ b/src/coreclr/tools/aot/ILCompiler.ReadyToRun/IBC/MIbcProfileParser.cs @@ -41,7 +41,12 @@ namespace ILCompiler.IBC // token type is 0, therefore it can't be a type return new TypeSystemEntityOrUnknown((int)token); } - return new TypeSystemEntityOrUnknown((TypeDesc)_ilBody.GetObject((int)token)); + TypeDesc foundType = _ilBody.GetObject((int)token, NotFoundBehavior.ReturnNull) as TypeDesc; + if (foundType == null) + { + return new TypeSystemEntityOrUnknown((int)token & 0x00FFFFFF); + } + return new TypeSystemEntityOrUnknown(foundType); } catch { @@ -265,7 +270,9 @@ namespace ILCompiler.IBC metadataObject = null; try { - metadataObject = ilBody.GetObject(token); + metadataObject = ilBody.GetObject(token, NotFoundBehavior.ReturnNull); + if (metadataObject == null) + metadataObject = metadataNotResolvable; } catch (TypeSystemException) { @@ -509,7 +516,7 @@ namespace ILCompiler.IBC throw new NotImplementedException(); } - public override MetadataType GetType(string nameSpace, string name, bool throwIfNotFound = true) + public override MetadataType GetType(string nameSpace, string name, NotFoundBehavior notFoundBehavior) { TypeSystemContext context = Context; @@ -519,9 +526,14 @@ namespace ILCompiler.IBC return Context.UniversalCanonType; else { - if (throwIfNotFound) + if (notFoundBehavior != NotFoundBehavior.ReturnNull) { - throw new TypeLoadException($"{nameSpace}.{name}"); + var failure = ResolutionFailure.GetTypeLoadResolutionFailure(nameSpace, name, "System.Private.Canon"); + ModuleDesc.GetTypeResolutionFailure = failure; + if (notFoundBehavior == NotFoundBehavior.Throw) + failure.Throw(); + + return null; } return null; } diff --git a/src/coreclr/tools/aot/ILCompiler.TypeSystem.ReadyToRun/ILCompiler.TypeSystem.ReadyToRun.csproj b/src/coreclr/tools/aot/ILCompiler.TypeSystem.ReadyToRun/ILCompiler.TypeSystem.ReadyToRun.csproj index c8e83d94bf8..580438dcc29 100644 --- a/src/coreclr/tools/aot/ILCompiler.TypeSystem.ReadyToRun/ILCompiler.TypeSystem.ReadyToRun.csproj +++ b/src/coreclr/tools/aot/ILCompiler.TypeSystem.ReadyToRun/ILCompiler.TypeSystem.ReadyToRun.csproj @@ -146,6 +146,12 @@ <Compile Include="..\..\Common\TypeSystem\Common\ModuleDesc.cs"> <Link>TypeSystem\Common\ModuleDesc.cs</Link> </Compile> + <Compile Include="..\..\Common\TypeSystem\Common\NotFoundBehavior.cs"> + <Link>TypeSystem\Common\NotFoundBehavior.cs</Link> + </Compile> + <Compile Include="..\..\Common\TypeSystem\Common\ResolutionFailure.cs"> + <Link>TypeSystem\Common\ResolutionFailure.cs</Link> + </Compile> <Compile Include="..\..\Common\TypeSystem\Common\TypeSystemEntity.cs"> <Link>TypeSystem\Common\TypeSystemEntity.cs</Link> </Compile> |