Skip to content

Commit

Permalink
Avoid try/catch when generating type forwarder metadata (#69702)
Browse files Browse the repository at this point in the history
The code was written before we introduced the concept of `NotFoundBehavior`. This avoids hitting first-chance exceptions when mscorlib becomes part of the closure for whatever reason. First chance exception are annoying.

For context, NativeAOT currently generates type forwarder metadata for everything that had a definition generated (e.g. if System.Object is generated and mscorlib and System.Runtime is generated, we generate the metadata for System.Object and the forwarders in both S.Runtime and mscorlib).

We'll want to eventually model type forwarders in the dependency graph and only generate them when needed (e.g. only if `Type.GetType("System.Object, mscorlib")` is present). It's what IL Linker now does. It's a possible future size optimization.
  • Loading branch information
MichalStrehovsky committed May 24, 2022
1 parent e86511e commit a014478
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -254,21 +254,19 @@ public bool GeneratesMetadata(EcmaModule module, CustomAttributeHandle caHandle)

public bool GeneratesMetadata(EcmaModule module, ExportedTypeHandle exportedTypeHandle)
{
try
{
// We'll possibly need to do something else here if we ever use this MetadataManager
// with compilation modes that generate multiple metadata blobs.
// (Multi-module or .NET Native style shared library.)
// We are currently missing type forwarders pointing to the other blobs.
var targetType = (MetadataType)module.GetObject(exportedTypeHandle);
return GeneratesMetadata(targetType);
}
catch (TypeSystemException)
// We'll possibly need to do something else here if we ever use this MetadataManager
// with compilation modes that generate multiple metadata blobs.
// (Multi-module or .NET Native style shared library.)
// We are currently missing type forwarders pointing to the other blobs.
var targetType = (MetadataType)module.GetObject(exportedTypeHandle, NotFoundBehavior.ReturnNull);
if (targetType == null)
{
// No harm in generating a forwarder that didn't resolve.
// We'll get matching behavior at runtime.
return true;
}

return GeneratesMetadata(targetType);
}

public bool IsBlocked(MetadataType typeDef)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -885,19 +885,16 @@ public bool GeneratesMetadata(EcmaModule module, CustomAttributeHandle caHandle)

public bool GeneratesMetadata(EcmaModule module, ExportedTypeHandle exportedTypeHandle)
{
try
{
// Generate the forwarder only if we generated the target type.
// If the target type is in a different compilation group, assume we generated it there.
var targetType = (MetadataType)module.GetObject(exportedTypeHandle);
return GeneratesMetadata(targetType) || !_factory.CompilationModuleGroup.ContainsType(targetType);
}
catch (TypeSystemException)
// Generate the forwarder only if we generated the target type.
// If the target type is in a different compilation group, assume we generated it there.
var targetType = (MetadataType)module.GetObject(exportedTypeHandle, NotFoundBehavior.ReturnNull);
if (targetType == null)
{
// No harm in generating a forwarder that didn't resolve.
// We'll get matching behavior at runtime.
return true;
}
return GeneratesMetadata(targetType) || !_factory.CompilationModuleGroup.ContainsType(targetType);
}

public bool IsBlocked(MetadataType typeDef)
Expand Down

0 comments on commit a014478

Please sign in to comment.