Skip to content

Commit

Permalink
[release/8.0-staging] Fix Type System Equivalence Checks (#106667)
Browse files Browse the repository at this point in the history
* Initial fix for the type not being read bug.

* Addressed initial feedback to fully fix the bug.

---------

Co-authored-by: Ivan Diaz <[email protected]>
  • Loading branch information
github-actions[bot] and ivdiazsa authored Aug 21, 2024
1 parent 5ce9f84 commit 78b77de
Showing 1 changed file with 28 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public partial class EcmaType
private TypeIdentifierData ComputeTypeIdentifierFromGuids()
{
CustomAttributeValue<TypeDesc>? guidAttribute;

if (IsInterface && _typeDefinition.Attributes.HasFlag(TypeAttributes.Import))
{
// ComImport interfaces get scope from their GUID
Expand All @@ -23,6 +24,7 @@ private TypeIdentifierData ComputeTypeIdentifierFromGuids()
{
// other equivalent types get it from the declaring assembly
var attributeHandle = this.MetadataReader.GetCustomAttributeHandle(MetadataReader.GetAssemblyDefinition().GetCustomAttributes(), "System.Runtime.InteropServices", "GuidAttribute");

if (attributeHandle.IsNil)
return null;

Expand All @@ -40,6 +42,7 @@ private TypeIdentifierData ComputeTypeIdentifierFromGuids()

string scope = (string)guidAttribute.Value.FixedArguments[0].Value;
string name = this.Name;

if (this.Namespace != null)
name = this.Namespace + "." + name;

Expand All @@ -53,6 +56,7 @@ private TypeIdentifierData ComputeTypeIdentifierData()

// Check for type identifier attribute
var typeIdentifierAttribute = this.GetDecodedCustomAttribute("System.Runtime.InteropServices", "TypeIdentifierAttribute");

if (typeIdentifierAttribute.HasValue)
{
// If the type has a type identifier attribute it is always considered to be type equivalent
Expand All @@ -68,28 +72,38 @@ private TypeIdentifierData ComputeTypeIdentifierData()
if (typeIdentifierAttribute.Value.FixedArguments[1].Type != Context.GetWellKnownType(WellKnownType.String))
return null;

_data = new TypeIdentifierData((string)typeIdentifierAttribute.Value.FixedArguments[0].Value, (string)typeIdentifierAttribute.Value.FixedArguments[1].Value);
return _data;
return new TypeIdentifierData((string)typeIdentifierAttribute.Value.FixedArguments[0].Value, (string)typeIdentifierAttribute.Value.FixedArguments[1].Value);
}
else

// In addition to the TypeIdentifierAttribute certain other types may also be opted in to type equivalence
if (Context.SupportsCOMInterop)
{
// In addition to the TypeIdentifierAttribute certain other types may also be opted in to type equivalence
if (Context.SupportsCOMInterop)
// 1. The assembly is marked with ImportedFromTypeLibAttribute or PrimaryInteropAssemblyAttribute
// We will verify this by checking for their attribute handles using the Metadata Reader.

CustomAttributeHandle importedFromTypeLibHdl = this.MetadataReader.GetCustomAttributeHandle(
MetadataReader.GetAssemblyDefinition().GetCustomAttributes(),
"System.Runtime.InteropServices",
"ImportedFromTypeLibAttribute"
);

CustomAttributeHandle primaryInteropAssemblyHdl = this.MetadataReader.GetCustomAttributeHandle(
MetadataReader.GetAssemblyDefinition().GetCustomAttributes(),
"System.Runtime.InteropServices",
"PrimaryInteropAssemblyAttribute"
);

if (!importedFromTypeLibHdl.IsNil || !primaryInteropAssemblyHdl.IsNil)
{
// 1. Type is within assembly marked with ImportedFromTypeLibAttribute or PrimaryInteropAssemblyAttribute
if (this.HasCustomAttribute("System.Runtime.InteropServices", "ImportedFromTypeLibAttribute") || this.HasCustomAttribute("System.Runtime.InteropServices", "PrimaryInteropAssemblyAttribute"))
{
// This type has a TypeIdentifier attribute if it has an appropriate shape to be considered type equivalent
}

// This type has a TypeIdentifier attribute if it has an appropriate shape to be considered type equivalent
if (!TypeHasCharacteristicsRequiredToBeTypeEquivalent)
return null;

_data = ComputeTypeIdentifierFromGuids();
return ComputeTypeIdentifierFromGuids();
}

return null;
}

return null;
}

public override TypeIdentifierData TypeIdentifierData
Expand Down

0 comments on commit 78b77de

Please sign in to comment.