Skip to content

Commit 4ef7681

Browse files
authored
Fix TypeRef for generic types (#197)
1 parent cd45658 commit 4ef7681

File tree

3 files changed

+12
-10
lines changed

3 files changed

+12
-10
lines changed

MetadataProcessor.Shared/Tables/nanoSignaturesTable.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Licensed to the .NET Foundation under one or more agreements.
1+
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
// Original work from Oleg Rakhmatulin.
@@ -477,7 +477,7 @@ public void WriteDataType(
477477
if (alsoWriteSubType)
478478
{
479479
GenericInstanceType genericType = (GenericInstanceType)typeDefinition;
480-
WriteDataType(genericType.Resolve(), writer, true, expandEnumType, isTypeDefinition);
480+
WriteDataType(genericType.ElementType, writer, true, expandEnumType, isTypeDefinition);
481481

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

MetadataProcessor.Shared/Tables/nanoTablesContext.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -269,8 +269,10 @@ public ushort GetMethodReferenceId(
269269
else if (memberReference is MethodReference &&
270270
MethodReferencesTable.TryGetMethodReferenceId(memberReference as MethodReference, out referenceId))
271271
{
272-
// check if method is external
273-
if (memberReference.DeclaringType.Scope.MetadataScopeType == MetadataScopeType.AssemblyNameReference)
272+
// check if method is external, unless it's a closed generic instance
273+
if (memberReference.DeclaringType.Scope.MetadataScopeType == MetadataScopeType.AssemblyNameReference
274+
&& !(memberReference.DeclaringType is GenericInstanceType genericInstanceType
275+
&& genericInstanceType.HasGenericArguments))
274276
{
275277
// method reference is external
276278
}

MetadataProcessor.Shared/nanoAssemblyBuilder.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -829,11 +829,6 @@ private HashSet<MetadataToken> BuildDependencyList(MetadataToken token)
829829
set.Add(v.VariableType.GetElementType().MetadataToken);
830830
}
831831
}
832-
else if (v.VariableType.IsValueType
833-
&& !v.VariableType.IsPrimitive)
834-
{
835-
set.Add(v.VariableType.MetadataToken);
836-
}
837832
else if (v.VariableType is GenericInstanceType)
838833
{
839834
// Cecil.Mono has a bug providing TypeSpecs Metadata tokens generic parameters variables, so we need to check against our internal table and build one from it
@@ -852,6 +847,11 @@ private HashSet<MetadataToken> BuildDependencyList(MetadataToken token)
852847
Debug.Fail($"Couldn't find a TypeSpec entry for {v.VariableType}");
853848
}
854849
}
850+
else if (v.VariableType.IsValueType
851+
&& !v.VariableType.IsPrimitive)
852+
{
853+
set.Add(v.VariableType.MetadataToken);
854+
}
855855
else if (v.VariableType.IsPointer)
856856
{
857857
string message = $"Pointer types in unsafe code aren't supported. Can't use {v.VariableType} variable in \"{md.FullName}\".";
@@ -1051,7 +1051,7 @@ private HashSet<MetadataToken> BuildDependencyList(MetadataToken token)
10511051
{
10521052
// add "fabricated" token for TypeSpec using the referenceId as RID
10531053
set.Add(new MetadataToken(TokenType.TypeSpec, tsRid));
1054-
set.Add(ts.GetElementType().MetadataToken);
1054+
set.Add(ts.MetadataToken);
10551055
}
10561056
else
10571057
{

0 commit comments

Comments
 (0)