Skip to content

Commit

Permalink
Fix a number of source generator bugs. (#87796)
Browse files Browse the repository at this point in the history
* Fix a number of source generator bugs.

* Extract type attribute processing to a standalone method.
  • Loading branch information
eiriktsarpalis authored Jun 20, 2023
1 parent 88cd88d commit 139826b
Show file tree
Hide file tree
Showing 22 changed files with 489 additions and 249 deletions.
11 changes: 11 additions & 0 deletions src/libraries/System.Text.Json/gen/Helpers/RoslynExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ internal static class RoslynExtensions
public static Location? GetDiagnosticLocation(this ISymbol typeSymbol)
=> typeSymbol.Locations.Length > 0 ? typeSymbol.Locations[0] : null;

public static Location? GetDiagnosticLocation(this AttributeData attributeData)
{
SyntaxReference? reference = attributeData.ApplicationSyntaxReference;
return reference?.SyntaxTree.GetLocation(reference.Span);
}

/// <summary>
/// Creates a copy of the Location instance that does not capture a reference to Compilation.
/// </summary>
Expand Down Expand Up @@ -60,6 +66,11 @@ public static ITypeSymbol EraseCompileTimeMetadata(this Compilation compilation,
}
else if (namedType.IsGenericType)
{
if (namedType.IsUnboundGenericType)
{
return namedType;
}

ImmutableArray<ITypeSymbol> typeArguments = namedType.TypeArguments;
INamedTypeSymbol? containingType = namedType.ContainingType;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,14 @@ internal static class DiagnosticDescriptors
category: JsonConstants.SystemTextJsonSourceGenerationName,
defaultSeverity: DiagnosticSeverity.Warning,
isEnabledByDefault: true);

public static DiagnosticDescriptor JsonConverterAttributeInvalidType { get; } = new DiagnosticDescriptor(
id: "SYSLIB1041",
title: new LocalizableResourceString(nameof(SR.JsonConverterAttributeInvalidTypeTitle), SR.ResourceManager, typeof(FxResources.System.Text.Json.SourceGeneration.SR)),
messageFormat: new LocalizableResourceString(nameof(SR.JsonConverterAttributeInvalidTypeMessageFormat), SR.ResourceManager, typeof(FxResources.System.Text.Json.SourceGeneration.SR)),
category: JsonConstants.SystemTextJsonSourceGenerationName,
defaultSeverity: DiagnosticSeverity.Warning,
isEnabledByDefault: true);
}
}
}
322 changes: 127 additions & 195 deletions src/libraries/System.Text.Json/gen/JsonSourceGenerator.Parser.cs

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions src/libraries/System.Text.Json/gen/Model/ClassType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ namespace System.Text.Json.SourceGeneration
public enum ClassType
{
/// <summary>
/// Types that are not supported yet by source gen including types with constructor parameters.
/// Types that are not supported at all and will be warned on/skipped by the source generator.
/// </summary>
TypeUnsupportedBySourceGen = 0,
Object = 1,
BuiltInSupportType = 2,
/// <summary>
/// Known types such as System.Type and System.IntPtr that throw NotSupportedException.
/// Known types such as System.Type and System.IntPtr that throw NotSupportedException at runtime.
/// </summary>
UnsupportedType = 3,
TypeWithDesignTimeProvidedCustomConverter = 4,
Expand Down
6 changes: 6 additions & 0 deletions src/libraries/System.Text.Json/gen/Resources/Strings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -177,4 +177,10 @@
<data name="JsonStringEnumConverterNotSupportedMessageFormat" xml:space="preserve">
<value>The member '{0}' has been annotated with 'JsonStringEnumConverter' which is not supported in native AOT. Consider using the generic 'JsonStringEnumConverter&lt;TEnum&gt;' instead.</value>
</data>
<data name="JsonConverterAttributeInvalidTypeTitle" xml:space="preserve">
<value>The 'JsonConverterAttribute.Type' contains an invalid or inaccessible argument.</value>
</data>
<data name="JsonConverterAttributeInvalidTypeMessageFormat" xml:space="preserve">
<value>The 'JsonConverterAttribute' type '{0}' specified on member '{1}' is not a converter type or does not contain an accessible parameterless constructor.</value>
</data>
</root>
10 changes: 10 additions & 0 deletions src/libraries/System.Text.Json/gen/Resources/xlf/Strings.cs.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,16 @@
<target state="translated">Deserializace vlastností pouze pro inicializaci se v současnosti v režimu generování zdroje nepodporuje.</target>
<note />
</trans-unit>
<trans-unit id="JsonConverterAttributeInvalidTypeMessageFormat">
<source>The 'JsonConverterAttribute' type '{0}' specified on member '{1}' is not a converter type or does not contain an accessible parameterless constructor.</source>
<target state="new">The 'JsonConverterAttribute' type '{0}' specified on member '{1}' is not a converter type or does not contain an accessible parameterless constructor.</target>
<note />
</trans-unit>
<trans-unit id="JsonConverterAttributeInvalidTypeTitle">
<source>The 'JsonConverterAttribute.Type' contains an invalid or inaccessible argument.</source>
<target state="new">The 'JsonConverterAttribute.Type' contains an invalid or inaccessible argument.</target>
<note />
</trans-unit>
<trans-unit id="JsonStringEnumConverterNotSupportedMessageFormat">
<source>The member '{0}' has been annotated with 'JsonStringEnumConverter' which is not supported in native AOT. Consider using the generic 'JsonStringEnumConverter&lt;TEnum&gt;' instead.</source>
<target state="translated">Člen {0} byl opatřen poznámkou jsonStringEnumConverter, což není v nativním AOT podporováno. Zvažte použití obecného objektu JsonStringEnumConverter&lt;TEnum&gt;.</target>
Expand Down
10 changes: 10 additions & 0 deletions src/libraries/System.Text.Json/gen/Resources/xlf/Strings.de.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,16 @@
<target state="translated">Die Deserialisierung von reinen init-Eigenschaften wird im Quellgenerierungsmodus derzeit nicht unterstützt.</target>
<note />
</trans-unit>
<trans-unit id="JsonConverterAttributeInvalidTypeMessageFormat">
<source>The 'JsonConverterAttribute' type '{0}' specified on member '{1}' is not a converter type or does not contain an accessible parameterless constructor.</source>
<target state="new">The 'JsonConverterAttribute' type '{0}' specified on member '{1}' is not a converter type or does not contain an accessible parameterless constructor.</target>
<note />
</trans-unit>
<trans-unit id="JsonConverterAttributeInvalidTypeTitle">
<source>The 'JsonConverterAttribute.Type' contains an invalid or inaccessible argument.</source>
<target state="new">The 'JsonConverterAttribute.Type' contains an invalid or inaccessible argument.</target>
<note />
</trans-unit>
<trans-unit id="JsonStringEnumConverterNotSupportedMessageFormat">
<source>The member '{0}' has been annotated with 'JsonStringEnumConverter' which is not supported in native AOT. Consider using the generic 'JsonStringEnumConverter&lt;TEnum&gt;' instead.</source>
<target state="translated">Der Member "{0}" wurde mit "JsonStringEnumConverter" kommentiert, was in nativem AOT nicht unterstützt wird. Erwägen Sie stattdessen die Verwendung des generischen "JsonStringEnumConverter&lt;TEnum&gt;".</target>
Expand Down
10 changes: 10 additions & 0 deletions src/libraries/System.Text.Json/gen/Resources/xlf/Strings.es.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,16 @@
<target state="translated">Actualmente no se admite la deserialización de propiedades de solo inicialización en el modo de generación de origen.</target>
<note />
</trans-unit>
<trans-unit id="JsonConverterAttributeInvalidTypeMessageFormat">
<source>The 'JsonConverterAttribute' type '{0}' specified on member '{1}' is not a converter type or does not contain an accessible parameterless constructor.</source>
<target state="new">The 'JsonConverterAttribute' type '{0}' specified on member '{1}' is not a converter type or does not contain an accessible parameterless constructor.</target>
<note />
</trans-unit>
<trans-unit id="JsonConverterAttributeInvalidTypeTitle">
<source>The 'JsonConverterAttribute.Type' contains an invalid or inaccessible argument.</source>
<target state="new">The 'JsonConverterAttribute.Type' contains an invalid or inaccessible argument.</target>
<note />
</trans-unit>
<trans-unit id="JsonStringEnumConverterNotSupportedMessageFormat">
<source>The member '{0}' has been annotated with 'JsonStringEnumConverter' which is not supported in native AOT. Consider using the generic 'JsonStringEnumConverter&lt;TEnum&gt;' instead.</source>
<target state="translated">El miembro '{0}' se ha anotado con 'JsonStringEnumConverter', que no se admite en AOT nativo. Considere la posibilidad de usar el elemento genérico "JsonStringEnumConverter&lt;TEnum&gt;" en su lugar.</target>
Expand Down
10 changes: 10 additions & 0 deletions src/libraries/System.Text.Json/gen/Resources/xlf/Strings.fr.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,16 @@
<target state="translated">La désérialisation des propriétés d’initialisation uniquement n’est actuellement pas prise en charge en mode de génération de source.</target>
<note />
</trans-unit>
<trans-unit id="JsonConverterAttributeInvalidTypeMessageFormat">
<source>The 'JsonConverterAttribute' type '{0}' specified on member '{1}' is not a converter type or does not contain an accessible parameterless constructor.</source>
<target state="new">The 'JsonConverterAttribute' type '{0}' specified on member '{1}' is not a converter type or does not contain an accessible parameterless constructor.</target>
<note />
</trans-unit>
<trans-unit id="JsonConverterAttributeInvalidTypeTitle">
<source>The 'JsonConverterAttribute.Type' contains an invalid or inaccessible argument.</source>
<target state="new">The 'JsonConverterAttribute.Type' contains an invalid or inaccessible argument.</target>
<note />
</trans-unit>
<trans-unit id="JsonStringEnumConverterNotSupportedMessageFormat">
<source>The member '{0}' has been annotated with 'JsonStringEnumConverter' which is not supported in native AOT. Consider using the generic 'JsonStringEnumConverter&lt;TEnum&gt;' instead.</source>
<target state="translated">Le membre '{0}' a été annoté avec 'JsonStringEnumConverter', ce qui n’est pas pris en charge dans AOT natif. Utilisez plutôt le générique 'JsonStringEnumConverter&lt;TEnum&gt;'.</target>
Expand Down
10 changes: 10 additions & 0 deletions src/libraries/System.Text.Json/gen/Resources/xlf/Strings.it.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,16 @@
<target state="translated">La deserializzazione delle proprietà di sola inizializzazione al momento non è supportata nella modalità di generazione di origine.</target>
<note />
</trans-unit>
<trans-unit id="JsonConverterAttributeInvalidTypeMessageFormat">
<source>The 'JsonConverterAttribute' type '{0}' specified on member '{1}' is not a converter type or does not contain an accessible parameterless constructor.</source>
<target state="new">The 'JsonConverterAttribute' type '{0}' specified on member '{1}' is not a converter type or does not contain an accessible parameterless constructor.</target>
<note />
</trans-unit>
<trans-unit id="JsonConverterAttributeInvalidTypeTitle">
<source>The 'JsonConverterAttribute.Type' contains an invalid or inaccessible argument.</source>
<target state="new">The 'JsonConverterAttribute.Type' contains an invalid or inaccessible argument.</target>
<note />
</trans-unit>
<trans-unit id="JsonStringEnumConverterNotSupportedMessageFormat">
<source>The member '{0}' has been annotated with 'JsonStringEnumConverter' which is not supported in native AOT. Consider using the generic 'JsonStringEnumConverter&lt;TEnum&gt;' instead.</source>
<target state="translated">Il membro '{0}' è stato annotato con 'JsonStringEnumConverter' che non è supportato in AOT nativo. Provare a usare il generico 'JsonStringEnumConverter&lt;TEnum&gt;'.</target>
Expand Down
10 changes: 10 additions & 0 deletions src/libraries/System.Text.Json/gen/Resources/xlf/Strings.ja.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,16 @@
<target state="translated">現在、ソース生成モードでは init-only プロパティの逆シリアル化はサポートされていません。</target>
<note />
</trans-unit>
<trans-unit id="JsonConverterAttributeInvalidTypeMessageFormat">
<source>The 'JsonConverterAttribute' type '{0}' specified on member '{1}' is not a converter type or does not contain an accessible parameterless constructor.</source>
<target state="new">The 'JsonConverterAttribute' type '{0}' specified on member '{1}' is not a converter type or does not contain an accessible parameterless constructor.</target>
<note />
</trans-unit>
<trans-unit id="JsonConverterAttributeInvalidTypeTitle">
<source>The 'JsonConverterAttribute.Type' contains an invalid or inaccessible argument.</source>
<target state="new">The 'JsonConverterAttribute.Type' contains an invalid or inaccessible argument.</target>
<note />
</trans-unit>
<trans-unit id="JsonStringEnumConverterNotSupportedMessageFormat">
<source>The member '{0}' has been annotated with 'JsonStringEnumConverter' which is not supported in native AOT. Consider using the generic 'JsonStringEnumConverter&lt;TEnum&gt;' instead.</source>
<target state="translated">メンバー '{0}' には、ネイティブ AOT ではサポートされていない 'JsonStringEnumConverter' の注釈が付けられています。 代わりに汎用の 'JsonStringEnumConverter&lt;TEnum&gt;' を使用することを検討してください。</target>
Expand Down
10 changes: 10 additions & 0 deletions src/libraries/System.Text.Json/gen/Resources/xlf/Strings.ko.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,16 @@
<target state="translated">초기화 전용 속성의 역직렬화는 현재 원본 생성 모드에서 지원되지 않습니다.</target>
<note />
</trans-unit>
<trans-unit id="JsonConverterAttributeInvalidTypeMessageFormat">
<source>The 'JsonConverterAttribute' type '{0}' specified on member '{1}' is not a converter type or does not contain an accessible parameterless constructor.</source>
<target state="new">The 'JsonConverterAttribute' type '{0}' specified on member '{1}' is not a converter type or does not contain an accessible parameterless constructor.</target>
<note />
</trans-unit>
<trans-unit id="JsonConverterAttributeInvalidTypeTitle">
<source>The 'JsonConverterAttribute.Type' contains an invalid or inaccessible argument.</source>
<target state="new">The 'JsonConverterAttribute.Type' contains an invalid or inaccessible argument.</target>
<note />
</trans-unit>
<trans-unit id="JsonStringEnumConverterNotSupportedMessageFormat">
<source>The member '{0}' has been annotated with 'JsonStringEnumConverter' which is not supported in native AOT. Consider using the generic 'JsonStringEnumConverter&lt;TEnum&gt;' instead.</source>
<target state="translated">'{0}' 멤버에 네이티브 AOT에서 지원되지 않는 'JsonStringEnumConverter'로 주석이 달렸습니다. 대신 제네릭 'JsonStringEnumConverter&lt;TEnum&gt;'을 사용해 보세요.</target>
Expand Down
10 changes: 10 additions & 0 deletions src/libraries/System.Text.Json/gen/Resources/xlf/Strings.pl.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,16 @@
<target state="translated">Deserializacja właściwości tylko do inicjowania nie jest obecnie obsługiwana w trybie generowania źródła.</target>
<note />
</trans-unit>
<trans-unit id="JsonConverterAttributeInvalidTypeMessageFormat">
<source>The 'JsonConverterAttribute' type '{0}' specified on member '{1}' is not a converter type or does not contain an accessible parameterless constructor.</source>
<target state="new">The 'JsonConverterAttribute' type '{0}' specified on member '{1}' is not a converter type or does not contain an accessible parameterless constructor.</target>
<note />
</trans-unit>
<trans-unit id="JsonConverterAttributeInvalidTypeTitle">
<source>The 'JsonConverterAttribute.Type' contains an invalid or inaccessible argument.</source>
<target state="new">The 'JsonConverterAttribute.Type' contains an invalid or inaccessible argument.</target>
<note />
</trans-unit>
<trans-unit id="JsonStringEnumConverterNotSupportedMessageFormat">
<source>The member '{0}' has been annotated with 'JsonStringEnumConverter' which is not supported in native AOT. Consider using the generic 'JsonStringEnumConverter&lt;TEnum&gt;' instead.</source>
<target state="translated">Element członkowski '{0}' został opatrzony adnotacją 'JsonStringEnumConverter', która nie jest obsługiwana w natywnym AOT. Zamiast tego należy rozważyć użycie ogólnego konwertera „JsonStringEnumConverter&lt;TEnum&gt;”.</target>
Expand Down
10 changes: 10 additions & 0 deletions src/libraries/System.Text.Json/gen/Resources/xlf/Strings.pt-BR.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,16 @@
<target state="translated">A desserialização de propriedades apenas de inicialização não é atualmente suportada no modo de geração de origem.</target>
<note />
</trans-unit>
<trans-unit id="JsonConverterAttributeInvalidTypeMessageFormat">
<source>The 'JsonConverterAttribute' type '{0}' specified on member '{1}' is not a converter type or does not contain an accessible parameterless constructor.</source>
<target state="new">The 'JsonConverterAttribute' type '{0}' specified on member '{1}' is not a converter type or does not contain an accessible parameterless constructor.</target>
<note />
</trans-unit>
<trans-unit id="JsonConverterAttributeInvalidTypeTitle">
<source>The 'JsonConverterAttribute.Type' contains an invalid or inaccessible argument.</source>
<target state="new">The 'JsonConverterAttribute.Type' contains an invalid or inaccessible argument.</target>
<note />
</trans-unit>
<trans-unit id="JsonStringEnumConverterNotSupportedMessageFormat">
<source>The member '{0}' has been annotated with 'JsonStringEnumConverter' which is not supported in native AOT. Consider using the generic 'JsonStringEnumConverter&lt;TEnum&gt;' instead.</source>
<target state="translated">O membro "{0}" foi anotado com "JsonStringEnumConverter" que não tem suporte na AOT nativa. Considere usar o genérico "JsonStringEnumConverter&lt;TEnum&gt;".</target>
Expand Down
10 changes: 10 additions & 0 deletions src/libraries/System.Text.Json/gen/Resources/xlf/Strings.ru.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,16 @@
<target state="translated">Десериализация свойств, предназначенных только для инициализации, сейчас не поддерживается в режиме создания исходного кода.</target>
<note />
</trans-unit>
<trans-unit id="JsonConverterAttributeInvalidTypeMessageFormat">
<source>The 'JsonConverterAttribute' type '{0}' specified on member '{1}' is not a converter type or does not contain an accessible parameterless constructor.</source>
<target state="new">The 'JsonConverterAttribute' type '{0}' specified on member '{1}' is not a converter type or does not contain an accessible parameterless constructor.</target>
<note />
</trans-unit>
<trans-unit id="JsonConverterAttributeInvalidTypeTitle">
<source>The 'JsonConverterAttribute.Type' contains an invalid or inaccessible argument.</source>
<target state="new">The 'JsonConverterAttribute.Type' contains an invalid or inaccessible argument.</target>
<note />
</trans-unit>
<trans-unit id="JsonStringEnumConverterNotSupportedMessageFormat">
<source>The member '{0}' has been annotated with 'JsonStringEnumConverter' which is not supported in native AOT. Consider using the generic 'JsonStringEnumConverter&lt;TEnum&gt;' instead.</source>
<target state="translated">Элемент "{0}" содержит примечание JsonStringEnumConverter, что не поддерживается в собственном AOT. Вместо этого рассмотрите возможность использовать универсальный параметр JsonStringEnumConverter&lt;TEnum&gt;.</target>
Expand Down
Loading

0 comments on commit 139826b

Please sign in to comment.