Skip to content

Commit

Permalink
fix duplicate registration of named tuples (#1464)
Browse files Browse the repository at this point in the history
* fix duplicate registration of named tuples

* fix: avoid unnecessary conversion
  • Loading branch information
honda-tatsuya authored Nov 19, 2022
1 parent 8a149fe commit 5a138c4
Showing 1 changed file with 27 additions and 8 deletions.
35 changes: 27 additions & 8 deletions src/MessagePack.GeneratorCore/CodeAnalysis/TypeCollector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -252,12 +252,15 @@ public class TypeCollector
private readonly List<GenericSerializationInfo> collectedGenericInfo = new();
private readonly List<UnionSerializationInfo> collectedUnionInfo = new();

private readonly Compilation compilation;

public TypeCollector(Compilation compilation, bool disallowInternal, bool isForceUseMap, string[]? ignoreTypeNames, Action<string> logger)
{
this.typeReferences = new ReferenceSymbols(compilation, logger);
this.disallowInternal = disallowInternal;
this.isForceUseMap = isForceUseMap;
this.externalIgnoreTypeNames = new HashSet<string>(ignoreTypeNames ?? Array.Empty<string>());
this.compilation = compilation;

targetTypes = compilation.GetNamedTypeSymbols()
.Where(x =>
Expand Down Expand Up @@ -329,7 +332,7 @@ private void CollectCore(ITypeSymbol typeSymbol)

if (typeSymbol is IArrayTypeSymbol arrayTypeSymbol)
{
this.CollectArray(arrayTypeSymbol);
this.CollectArray((IArrayTypeSymbol)ToTupleUnderlyingType(arrayTypeSymbol));
return;
}

Expand Down Expand Up @@ -357,13 +360,7 @@ private void CollectCore(ITypeSymbol typeSymbol)

if (type.IsGenericType)
{
this.CollectGeneric(type);
return;
}

if (type.TupleUnderlyingType != null)
{
CollectGeneric(type.TupleUnderlyingType);
this.CollectGeneric((INamedTypeSymbol)ToTupleUnderlyingType(type));
return;
}

Expand Down Expand Up @@ -459,6 +456,28 @@ private void CollectArray(IArrayTypeSymbol array)
this.collectedGenericInfo.Add(info);
}

private ITypeSymbol ToTupleUnderlyingType(ITypeSymbol typeSymbol)
{
if (typeSymbol is IArrayTypeSymbol array)
{
return compilation.CreateArrayTypeSymbol(ToTupleUnderlyingType(array.ElementType), array.Rank);
}

if (typeSymbol is not INamedTypeSymbol namedType || !namedType.IsGenericType)
{
return typeSymbol;
}

namedType = namedType.TupleUnderlyingType ?? namedType;
var newTypeArguments = namedType.TypeArguments.Select(ToTupleUnderlyingType).ToArray();
if (!namedType.TypeArguments.SequenceEqual(newTypeArguments))
{
return namedType.ConstructedFrom.Construct(newTypeArguments);
}

return namedType;
}

private void CollectGeneric(INamedTypeSymbol type)
{
INamedTypeSymbol genericType = type.ConstructUnboundGenericType();
Expand Down

0 comments on commit 5a138c4

Please sign in to comment.