FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Fix some source generator issues by AlanLiu90 · Pull Request #1873 · MessagePack-CSharp/MessagePack-CSharp · GitHub

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .cs  (12) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ public virtual bool Equals(QualifiedTypeName? other)
this.NestingType == other.NestingType &&
this.AccessModifier == other.AccessModifier &&
this.Kind == other.Kind &&
this.ArrayRank == other.ArrayRank &&
this.Name == other.Name &&
this.TypeParameters.SequenceEqual(other.TypeParameters);
}
Expand Down Expand Up @@ -263,6 +264,12 @@ private static int Compare(QualifiedTypeName? left, QualifiedTypeName? right)
return result;
}

result = left.ArrayRank.CompareTo(right.ArrayRank);
if (result != 0)
{
return result;
}

result = StringComparer.Ordinal.Compare(left.Name, right.Name);
if (result != 0)
{
Expand All @@ -275,7 +282,28 @@ private static int Compare(QualifiedTypeName? left, QualifiedTypeName? right)
return result;
}

return StringComparer.Ordinal.Compare(left.Name, right.Name);
result = StringComparer.Ordinal.Compare(left.Name, right.Name);
if (result != 0)
{
return result;
}

result = left.TypeParameters.Length.CompareTo(right.TypeParameters.Length);
if (result != 0)
{
return result;
}

for (int i = 0; i < left.TypeParameters.Length; i++)
{
result = StringComparer.Ordinal.Compare(left.TypeParameters[i], right.TypeParameters[i]);
if (result != 0)
{
return result;
}
}

return 0;
}
}

Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,6 @@ public class TypeCollector
private readonly Action<Diagnostic>? reportDiagnostic;

private readonly ITypeSymbol? targetType;
private readonly bool excludeArrayElement;

// visitor workspace:
private readonly Dictionary<ITypeSymbol, bool> alreadyCollected = new(SymbolEqualityComparer.Default);
Expand All @@ -204,7 +203,6 @@ private TypeCollector(Compilation compilation, AnalyzerOptions options, Referenc
this.reportDiagnostic = reportAnalyzerDiagnostic;
this.options = options;
this.compilation = compilation;
this.excludeArrayElement = true;

bool isInaccessible = false;
foreach (BaseTypeDeclarationSyntax? decl in CodeAnalysisUtilities.FindInaccessibleTypes(targetType))
Expand Down Expand Up @@ -423,22 +421,14 @@ private void CollectGenericUnion(INamedTypeSymbol type)
private bool CollectArray(IArrayTypeSymbol array)
{
ITypeSymbol elemType = array.ElementType;
if (!this.excludeArrayElement)
{
if (!this.CollectCore(elemType))
{
return false;
}
}

if (this.alreadyCollected.ContainsKey(elemType))
if (!this.CollectCore(elemType))
{
return true;
return false;
}

if (elemType is ITypeParameterSymbol)
if (elemType is ITypeParameterSymbol || (elemType is INamedTypeSymbol symbol && IsOpenGenericTypeRecursively(symbol)))
{
this.alreadyCollected.Add(elemType, true);
return true;
}

Expand Down Expand Up @@ -493,7 +483,7 @@ private bool CollectGeneric(INamedTypeSymbol type)
var genericTypeDefinitionString = typeDefinition.ToDisplayString();
string? genericTypeNamespace = type.ContainingNamespace?.ToDisplayString(SymbolDisplayFormat.MinimallyQualifiedFormat);
var fullName = type.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat);
int unboundArity = this.IsOpenGenericTypeRecursively(type) ? type.Arity : 0;
var isOpenGenericType = IsOpenGenericTypeRecursively(type);
string formattedTypeFullName = type.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat);
string? formatterName;

Expand All @@ -517,12 +507,16 @@ private bool CollectGeneric(INamedTypeSymbol type)
return true;
}

formatterName = $"NullableFormatter<{firstTypeArgument.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat)}>";
var info = GenericSerializationInfo.Create(type, this.options.Generator.Resolver) with
if (!isOpenGenericType)
{
Formatter = new QualifiedTypeName("MsgPack::Formatters", TypeKind.Class, formatterName),
};
this.collectedGenericInfo.Add(info);
formatterName = $"NullableFormatter<{firstTypeArgument.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat)}>";
var info = GenericSerializationInfo.Create(type, this.options.Generator.Resolver) with
{
Formatter = new QualifiedTypeName("MsgPack::Formatters", TypeKind.Class, formatterName),
};
this.collectedGenericInfo.Add(info);
}

return true;
}

Expand All @@ -534,14 +528,23 @@ private bool CollectGeneric(INamedTypeSymbol type)
this.CollectCore(item);
}

GenericSerializationInfo info = GenericSerializationInfo.Create(type, this.options.Generator.Resolver);
int indexOfLastPeriod = formatterFullName.LastIndexOf('.');
info = info with
GenericSerializationInfo info;

if (isOpenGenericType)
{
Formatter = new(formatterFullName.Substring(0, indexOfLastPeriod), TypeKind.Class, formatterFullName.Substring(indexOfLastPeriod + 1), info.DataType.TypeParameters),
};
return true;
}
else
{
info = GenericSerializationInfo.Create(type, this.options.Generator.Resolver);
int indexOfLastPeriod = formatterFullName.LastIndexOf('.');
info = info with
{
Formatter = new(formatterFullName.Substring(0, indexOfLastPeriod), TypeKind.Class, formatterFullName.Substring(indexOfLastPeriod + 1), info.DataType.TypeParameters),
};

this.collectedGenericInfo.Add(info);
this.collectedGenericInfo.Add(info);
}

if (genericTypeDefinitionString != "System.Linq.ILookup<,>")
{
Expand Down Expand Up @@ -599,8 +602,12 @@ private bool CollectGeneric(INamedTypeSymbol type)
}
}

GenericSerializationInfo genericSerializationInfo = GenericSerializationInfo.Create(type, this.options.Generator.Resolver);
this.collectedGenericInfo.Add(genericSerializationInfo);
if (!isOpenGenericType)
{
GenericSerializationInfo genericSerializationInfo = GenericSerializationInfo.Create(type, this.options.Generator.Resolver);
this.collectedGenericInfo.Add(genericSerializationInfo);
}

return true;
}

Expand Down Expand Up @@ -1207,8 +1214,8 @@ private static bool IsAllowAccessibility(ITypeSymbol symbol)
return true;
}

private bool IsOpenGenericTypeRecursively(INamedTypeSymbol type)
private static bool IsOpenGenericTypeRecursively(INamedTypeSymbol type)
{
return type.IsGenericType && type.TypeArguments.Any(x => x is ITypeParameterSymbol || (x is INamedTypeSymbol symbol && this.IsOpenGenericTypeRecursively(symbol)));
return type.IsGenericType && type.TypeArguments.Any(x => x is ITypeParameterSymbol || (x is INamedTypeSymbol symbol && IsOpenGenericTypeRecursively(symbol)));
}
}
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
$"{AttributeNamespace}.{CompositeResolverAttributeName}",
predicate: static (node, ct) => true,
transform: static (context, ct) => (
ResolverNamespace: context.TargetSymbol.ContainingNamespace.Name,
ResolverNamespace: context.TargetSymbol.ContainingNamespace.GetFullNamespaceName() ?? string.Empty,
ResolverName: context.TargetSymbol.Name,
Attribute: context.Attributes.Single()));

Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,12 @@ static FormatterCache()

private static class GeneratedMessagePackResolverGetFormatterHelper
{
private static readonly global::System.Collections.Generic.Dictionary<global::System.Type, int> closedTypeLookup = new(3)
private static readonly global::System.Collections.Generic.Dictionary<global::System.Type, int> closedTypeLookup = new(4)
{
{ typeof(global::TempProject.MyGenericObject<int, string>), 0 },
{ typeof(global::TempProject.MyObject), 1 },
{ typeof(global::TempProject.MyObjectNested), 2 },
{ typeof(global::TempProject.MyGenericObject<global::TempProject.MyGenericObject<int, string>, global::TempProject.MyGenericObject<int, string>>), 0 },
{ typeof(global::TempProject.MyGenericObject<int, string>), 1 },
{ typeof(global::TempProject.MyObject), 2 },
{ typeof(global::TempProject.MyObjectNested), 3 },
};
private static readonly global::System.Collections.Generic.Dictionary<global::System.Type, int> openTypeLookup = new(1)
{
Expand All @@ -56,9 +57,10 @@ internal static object GetFormatter(global::System.Type t)
{
return closedKey switch
{
0 => new global::MessagePack.GeneratedMessagePackResolver.TempProject.MyGenericObjectFormatter<int, string>(),
1 => new global::MessagePack.GeneratedMessagePackResolver.TempProject.MyObjectFormatter(),
2 => new global::MessagePack.GeneratedMessagePackResolver.TempProject.MyObjectNestedFormatter(),
0 => new global::MessagePack.GeneratedMessagePackResolver.TempProject.MyGenericObjectFormatter<global::TempProject.MyGenericObject<int, string>, global::TempProject.MyGenericObject<int, string>>(),
1 => new global::MessagePack.GeneratedMessagePackResolver.TempProject.MyGenericObjectFormatter<int, string>(),
2 => new global::MessagePack.GeneratedMessagePackResolver.TempProject.MyObjectFormatter(),
3 => new global::MessagePack.GeneratedMessagePackResolver.TempProject.MyObjectNestedFormatter(),
_ => null, // unreachable
};
}
Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,12 @@ static FormatterCache()

private static class GeneratedMessagePackResolverGetFormatterHelper
{
private static readonly global::System.Collections.Generic.Dictionary<global::System.Type, int> closedTypeLookup = new(3)
private static readonly global::System.Collections.Generic.Dictionary<global::System.Type, int> closedTypeLookup = new(4)
{
{ typeof(global::TempProject.MyGenericObject<int>), 0 },
{ typeof(global::TempProject.MyObject), 1 },
{ typeof(global::TempProject.MyObjectNested), 2 },
{ typeof(global::TempProject.MyGenericObject<global::TempProject.MyGenericObject<int>>), 0 },
{ typeof(global::TempProject.MyGenericObject<int>), 1 },
{ typeof(global::TempProject.MyObject), 2 },
{ typeof(global::TempProject.MyObjectNested), 3 },
};
private static readonly global::System.Collections.Generic.Dictionary<global::System.Type, int> openTypeLookup = new(1)
{
Expand All @@ -56,9 +57,10 @@ internal static object GetFormatter(global::System.Type t)
{
return closedKey switch
{
0 => new global::MessagePack.GeneratedMessagePackResolver.TempProject.MyGenericObjectFormatter<int>(),
1 => new global::MessagePack.GeneratedMessagePackResolver.TempProject.MyObjectFormatter(),
2 => new global::MessagePack.GeneratedMessagePackResolver.TempProject.MyObjectNestedFormatter(),
0 => new global::MessagePack.GeneratedMessagePackResolver.TempProject.MyGenericObjectFormatter<global::TempProject.MyGenericObject<int>>(),
1 => new global::MessagePack.GeneratedMessagePackResolver.TempProject.MyGenericObjectFormatter<int>(),
2 => new global::MessagePack.GeneratedMessagePackResolver.TempProject.MyObjectFormatter(),
3 => new global::MessagePack.GeneratedMessagePackResolver.TempProject.MyObjectNestedFormatter(),
_ => null, // unreachable
};
}
Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,12 @@ static FormatterCache()

private static class GeneratedMessagePackResolverGetFormatterHelper
{
private static readonly global::System.Collections.Generic.Dictionary<global::System.Type, int> closedTypeLookup = new(3)
private static readonly global::System.Collections.Generic.Dictionary<global::System.Type, int> closedTypeLookup = new(4)
{
{ typeof(global::System.Collections.Generic.List<global::TempProject.MyObject2>), 0 },
{ typeof(global::TempProject.MyObject), 1 },
{ typeof(global::TempProject.MyObject2), 2 },
{ typeof(global::TempProject.MyObject2[]), 1 },
{ typeof(global::TempProject.MyObject), 2 },
{ typeof(global::TempProject.MyObject2), 3 },
};
private static readonly global::System.Collections.Generic.Dictionary<global::System.Type, int> openTypeLookup = new(1)
{
Expand All @@ -57,8 +58,9 @@ internal static object GetFormatter(global::System.Type t)
return closedKey switch
{
0 => new MsgPack::Formatters.ListFormatter<global::TempProject.MyObject2>(),
1 => new global::MessagePack.GeneratedMessagePackResolver.TempProject.MyObjectFormatter(),
2 => new global::MessagePack.GeneratedMessagePackResolver.TempProject.MyObject2Formatter(),
1 => new MsgPack::Formatters.ArrayFormatter<global::TempProject.MyObject2>(),
2 => new global::MessagePack.GeneratedMessagePackResolver.TempProject.MyObjectFormatter(),
3 => new global::MessagePack.GeneratedMessagePackResolver.TempProject.MyObject2Formatter(),
_ => null, // unreachable
};
}
Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,13 @@ static FormatterCache()

private static class GeneratedMessagePackResolverGetFormatterHelper
{
private static readonly global::System.Collections.Generic.Dictionary<global::System.Type, int> closedTypeLookup = new(3)
private static readonly global::System.Collections.Generic.Dictionary<global::System.Type, int> closedTypeLookup = new(5)
{
{ typeof(global::System.Int32[]), 0 },
{ typeof(global::System.Collections.Generic.IEnumerable<global::System.Guid>), 1 },
{ typeof(global::TempProject.Wrapper<string>), 2 },
{ typeof(global::TempProject.Wrapper<global::System.Collections.Generic.IEnumerable<global::System.Guid>>), 2 },
{ typeof(global::TempProject.Wrapper<int[]>), 3 },
{ typeof(global::TempProject.Wrapper<string>), 4 },
};
private static readonly global::System.Collections.Generic.Dictionary<global::System.Type, int> openTypeLookup = new(1)
{
Expand All @@ -58,7 +60,9 @@ internal static object GetFormatter(global::System.Type t)
{
0 => new MsgPack::Formatters.ArrayFormatter<global::System.Int32>(),
1 => new MsgPack::Formatters.InterfaceEnumerableFormatter<global::System.Guid>(),
2 => new global::MessagePack.GeneratedMessagePackResolver.TempProject.WrapperFormatter<string>(),
2 => new global::MessagePack.GeneratedMessagePackResolver.TempProject.WrapperFormatter<global::System.Collections.Generic.IEnumerable<global::System.Guid>>(),
3 => new global::MessagePack.GeneratedMessagePackResolver.TempProject.WrapperFormatter<int[]>(),
4 => new global::MessagePack.GeneratedMessagePackResolver.TempProject.WrapperFormatter<string>(),
_ => null, // unreachable
};
}
Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,22 @@ static FormatterCache()

private static class GeneratedMessagePackResolverGetFormatterHelper
{
private static readonly global::System.Collections.Generic.Dictionary<global::System.Type, int> closedTypeLookup = new(6)
private static readonly global::System.Collections.Generic.Dictionary<global::System.Type, int> closedTypeLookup = new(14)
{
{ typeof(global::System.Int32[]), 0 },
{ typeof(global::System.Collections.Generic.List<string>), 1 },
{ typeof(global::System.Collections.Generic.IEnumerable<global::System.Guid>), 2 },
{ typeof(global::TempProject.MyGenericObject<string>), 3 },
{ typeof(global::TempProject.MyInnerGenericObject<string>), 4 },
{ typeof(global::TempProject.Wrapper<string>), 5 },
{ typeof(global::System.Collections.Generic.List<global::System.Collections.Generic.IEnumerable<global::System.Guid>>), 1 },
{ typeof(global::System.Collections.Generic.List<int[]>), 2 },
{ typeof(global::System.Collections.Generic.List<string>), 3 },
{ typeof(global::System.Collections.Generic.IEnumerable<global::System.Guid>), 4 },
{ typeof(global::TempProject.MyGenericObject<global::System.Collections.Generic.IEnumerable<global::System.Guid>>), 5 },
{ typeof(global::TempProject.MyGenericObject<int[]>), 6 },
{ typeof(global::TempProject.MyGenericObject<string>), 7 },
{ typeof(global::TempProject.MyInnerGenericObject<global::System.Collections.Generic.IEnumerable<global::System.Guid>>), 8 },
{ typeof(global::TempProject.MyInnerGenericObject<int[]>), 9 },
{ typeof(global::TempProject.MyInnerGenericObject<string>), 10 },
{ typeof(global::TempProject.Wrapper<global::System.Collections.Generic.IEnumerable<global::System.Guid>>), 11 },
{ typeof(global::TempProject.Wrapper<int[]>), 12 },
{ typeof(global::TempProject.Wrapper<string>), 13 },
};
private static readonly global::System.Collections.Generic.Dictionary<global::System.Type, int> openTypeLookup = new(3)
{
Expand All @@ -62,11 +70,19 @@ internal static object GetFormatter(global::System.Type t)
return closedKey switch
{
0 => new MsgPack::Formatters.ArrayFormatter<global::System.Int32>(),
1 => new MsgPack::Formatters.ListFormatter<string>(),
2 => new MsgPack::Formatters.InterfaceEnumerableFormatter<global::System.Guid>(),
3 => new global::MessagePack.GeneratedMessagePackResolver.TempProject.MyGenericObjectFormatter<string>(),
4 => new global::MessagePack.GeneratedMessagePackResolver.TempProject.MyInnerGenericObjectFormatter<string>(),
5 => new global::MessagePack.GeneratedMessagePackResolver.TempProject.WrapperFormatter<string>(),
1 => new MsgPack::Formatters.ListFormatter<global::System.Collections.Generic.IEnumerable<global::System.Guid>>(),
2 => new MsgPack::Formatters.ListFormatter<int[]>(),
3 => new MsgPack::Formatters.ListFormatter<string>(),
4 => new MsgPack::Formatters.InterfaceEnumerableFormatter<global::System.Guid>(),
5 => new global::MessagePack.GeneratedMessagePackResolver.TempProject.MyGenericObjectFormatter<global::System.Collections.Generic.IEnumerable<global::System.Guid>>(),
6 => new global::MessagePack.GeneratedMessagePackResolver.TempProject.MyGenericObjectFormatter<int[]>(),
7 => new global::MessagePack.GeneratedMessagePackResolver.TempProject.MyGenericObjectFormatter<string>(),
8 => new global::MessagePack.GeneratedMessagePackResolver.TempProject.MyInnerGenericObjectFormatter<global::System.Collections.Generic.IEnumerable<global::System.Guid>>(),
9 => new global::MessagePack.GeneratedMessagePackResolver.TempProject.MyInnerGenericObjectFormatter<int[]>(),
10 => new global::MessagePack.GeneratedMessagePackResolver.TempProject.MyInnerGenericObjectFormatter<string>(),
11 => new global::MessagePack.GeneratedMessagePackResolver.TempProject.WrapperFormatter<global::System.Collections.Generic.IEnumerable<global::System.Guid>>(),
12 => new global::MessagePack.GeneratedMessagePackResolver.TempProject.WrapperFormatter<int[]>(),
13 => new global::MessagePack.GeneratedMessagePackResolver.TempProject.WrapperFormatter<string>(),
_ => null, // unreachable
};
}
Expand Down
Loading

Back | FazBrowse Home | New Git URL