Skip to content

Fix WriteDataType for generic types and arrays #188

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 7, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 25 additions & 20 deletions MetadataProcessor.Shared/Tables/nanoSignaturesTable.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Licensed to the .NET Foundation under one or more agreements.
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

// Original work from Oleg Rakhmatulin.
Expand Down Expand Up @@ -433,20 +433,22 @@ public void WriteDataType(
{
writer.WriteByte((byte)NanoCLRDataType.DATATYPE_SZARRAY);

var array = (ArrayType)typeDefinition;

if (array.ElementType.IsGenericParameter)
if (alsoWriteSubType)
{
// ECMA 335 VI.B.4.3 Metadata
writer.WriteByte((byte)NanoCLRDataType.DATATYPE_VAR);
ArrayType array = (ArrayType)typeDefinition;

// OK to use byte here as we won't support more than 0x7F generic parameters
writer.WriteByte((byte)(array.ElementType as GenericParameter).Position);
}
else if (alsoWriteSubType)
{
if (array.ElementType.IsGenericParameter)
{
// ECMA 335 VI.B.4.3 Metadata
writer.WriteByte((byte)NanoCLRDataType.DATATYPE_VAR);

WriteDataType(array.ElementType, writer, true, expandEnumType, isTypeDefinition);
// OK to use byte here as we won't support more than 0x7F generic parameters
writer.WriteByte((byte)(array.ElementType as GenericParameter).Position);
}
else
{
WriteDataType(array.ElementType, writer, true, expandEnumType, isTypeDefinition);
}
}

return;
Expand All @@ -458,7 +460,7 @@ public void WriteDataType(

if (alsoWriteSubType)
{
var resolvedType = typeDefinition.Resolve();
TypeDefinition resolvedType = typeDefinition.Resolve();

WriteDataType(resolvedType, writer, false, expandEnumType, isTypeDefinition);
}
Expand All @@ -472,15 +474,18 @@ public void WriteDataType(
// II.23.2.12 Type
writer.WriteByte((byte)NanoCLRDataType.DATATYPE_GENERICINST);

var genericType = (GenericInstanceType)typeDefinition;
WriteDataType(genericType.Resolve(), writer, true, expandEnumType, isTypeDefinition);
if (alsoWriteSubType)
{
GenericInstanceType genericType = (GenericInstanceType)typeDefinition;
WriteDataType(genericType.Resolve(), writer, true, expandEnumType, isTypeDefinition);

// OK to use byte here as we won't support more than 0x7F arguments
writer.WriteByte((byte)genericType.GenericArguments.Count);
// OK to use byte here as we won't support more than 0x7F arguments
writer.WriteByte((byte)genericType.GenericArguments.Count);

foreach (var a in genericType.GenericArguments)
{
WriteDataType(a, writer, true, expandEnumType, isTypeDefinition);
foreach (TypeReference a in genericType.GenericArguments)
{
WriteDataType(a, writer, true, expandEnumType, isTypeDefinition);
}
}

return;
Expand Down