Skip to content
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

Add nullable reference type annotations to System.Text.Json source gen #79613

Merged
merged 17 commits into from
Dec 16, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ public SyntaxTreeInfo(SyntaxTree tree, bool containsGlobalAliases, bool contains
ContainsAttributeList = containsAttributeList;
}

public bool Equals(SyntaxTreeInfo other)
public bool Equals(SyntaxTreeInfo? other)
=> Tree == other?.Tree;

public override bool Equals(object obj)
public override bool Equals(object? obj)
=> this.Equals(obj as SyntaxTreeInfo);

public override int GetHashCode()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,14 @@ internal static partial class ReflectionExtensions

public static Type? GetCompatibleGenericBaseClass(
this Type type,
Type baseType,
Type? baseType,
bool sourceGenType = false)
{
if (baseType is null)
{
return null;
}

Debug.Assert(baseType.IsGenericType);
Debug.Assert(!baseType.IsInterface);
Debug.Assert(baseType == baseType.GetGenericTypeDefinition());
Expand Down
47 changes: 27 additions & 20 deletions src/libraries/System.Text.Json/System.Text.Json.sln
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,19 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "System.Text.Json", "ref\Sys
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "System.Text.Json", "src\System.Text.Json.csproj", "{1285FF43-F491-4BE0-B92C-37DA689CBD4B}"
EndProject
Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "System.Text.Json.FSharp.Tests", "tests\System.Text.Json.FSharp.Tests\System.Text.Json.FSharp.Tests.fsproj", "{5720BF06-2031-4AD8-B9B4-31A01E27ABB8}"
Project("{6EC3EE1D-3C4E-46DD-8F32-0CC8E7565705}") = "System.Text.Json.FSharp.Tests", "tests\System.Text.Json.FSharp.Tests\System.Text.Json.FSharp.Tests.fsproj", "{5720BF06-2031-4AD8-B9B4-31A01E27ABB8}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "System.Text.Json.TestLibrary.Roslyn3.11", "tests\System.Text.Json.SourceGeneration.TestLibrary\System.Text.Json.TestLibrary.Roslyn3.11.csproj", "{5C0CE30B-DD4A-4F7A-87C0-5243F0C86885}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "System.Text.Json.TestLibrary.Roslyn4.0", "tests\System.Text.Json.SourceGeneration.TestLibrary\System.Text.Json.TestLibrary.Roslyn4.0.csproj", "{FCA21178-0411-45D6-B597-B7BE145CEE33}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "System.Text.Json.TestLibrary.Roslyn4.4", "tests\System.Text.Json.SourceGeneration.TestLibrary\System.Text.Json.TestLibrary.Roslyn4.4.csproj", "{FCA21178-0411-45D6-B597-B7BE145CEE33}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "System.Text.Json.SourceGeneration.Roslyn3.11.Tests", "tests\System.Text.Json.SourceGeneration.Tests\System.Text.Json.SourceGeneration.Roslyn3.11.Tests.csproj", "{66AD4B7E-CF15-4A8F-8BF8-7E1BC6176D07}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "System.Text.Json.SourceGeneration.Roslyn4.0.Tests", "tests\System.Text.Json.SourceGeneration.Tests\System.Text.Json.SourceGeneration.Roslyn4.0.Tests.csproj", "{33599A6C-F340-4E1B-9B4D-CB8946C22140}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "System.Text.Json.SourceGeneration.Roslyn4.4.Tests", "tests\System.Text.Json.SourceGeneration.Tests\System.Text.Json.SourceGeneration.Roslyn4.4.Tests.csproj", "{33599A6C-F340-4E1B-9B4D-CB8946C22140}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "System.Text.Json.SourceGeneration.Roslyn3.11.Unit.Tests", "tests\System.Text.Json.SourceGeneration.Unit.Tests\System.Text.Json.SourceGeneration.Roslyn3.11.Unit.Tests.csproj", "{256A4653-4287-44B3-BDEF-67FC1522ED2F}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "System.Text.Json.SourceGeneration.Roslyn4.0.Unit.Tests", "tests\System.Text.Json.SourceGeneration.Unit.Tests\System.Text.Json.SourceGeneration.Roslyn4.0.Unit.Tests.csproj", "{F6A18EB5-A8CC-4A39-9E85-5FA226019C3D}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "System.Text.Json.SourceGeneration.Roslyn4.4.Unit.Tests", "tests\System.Text.Json.SourceGeneration.Unit.Tests\System.Text.Json.SourceGeneration.Roslyn4.4.Unit.Tests.csproj", "{F6A18EB5-A8CC-4A39-9E85-5FA226019C3D}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "System.Text.Json.Tests", "tests\System.Text.Json.Tests\System.Text.Json.Tests.csproj", "{A0178BAA-A1AF-4C69-8E4A-A700A2723DDC}"
EndProject
Expand All @@ -67,6 +67,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{E49881A9-09F
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "gen", "gen", "{F254F143-4704-4432-9995-20D87FA8BF8D}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "System.Text.Json.SourceGeneration.NetCoreApp", "gen\System.Text.Json.SourceGeneration.NetCoreApp.csproj", "{120437F0-08E1-4C05-81C6-937790A2550C}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -193,41 +195,46 @@ Global
{A0178BAA-A1AF-4C69-8E4A-A700A2723DDC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A0178BAA-A1AF-4C69-8E4A-A700A2723DDC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A0178BAA-A1AF-4C69-8E4A-A700A2723DDC}.Release|Any CPU.Build.0 = Release|Any CPU
{120437F0-08E1-4C05-81C6-937790A2550C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{120437F0-08E1-4C05-81C6-937790A2550C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{120437F0-08E1-4C05-81C6-937790A2550C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{120437F0-08E1-4C05-81C6-937790A2550C}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{102945CA-3736-4B2C-8E68-242A0B247F2B} = {E07C6980-EB71-4D19-A80A-7BEB80B635B1}
{5720BF06-2031-4AD8-B9B4-31A01E27ABB8} = {E07C6980-EB71-4D19-A80A-7BEB80B635B1}
{5C0CE30B-DD4A-4F7A-87C0-5243F0C86885} = {E07C6980-EB71-4D19-A80A-7BEB80B635B1}
{FCA21178-0411-45D6-B597-B7BE145CEE33} = {E07C6980-EB71-4D19-A80A-7BEB80B635B1}
{66AD4B7E-CF15-4A8F-8BF8-7E1BC6176D07} = {E07C6980-EB71-4D19-A80A-7BEB80B635B1}
{33599A6C-F340-4E1B-9B4D-CB8946C22140} = {E07C6980-EB71-4D19-A80A-7BEB80B635B1}
{256A4653-4287-44B3-BDEF-67FC1522ED2F} = {E07C6980-EB71-4D19-A80A-7BEB80B635B1}
{F6A18EB5-A8CC-4A39-9E85-5FA226019C3D} = {E07C6980-EB71-4D19-A80A-7BEB80B635B1}
{A0178BAA-A1AF-4C69-8E4A-A700A2723DDC} = {E07C6980-EB71-4D19-A80A-7BEB80B635B1}
{73D5739C-E382-4E22-A7D3-B82705C58C74} = {0371C5D8-D5F5-4747-9810-D91D71D8C0E4}
{E9AA0AEB-AEAE-4B28-8D4D-17A6D7C89D17} = {E49881A9-09F6-442F-9E1D-6D87F5F837F1}
{282400DF-F3D8-4419-90F1-1E2F2D8B760C} = {0371C5D8-D5F5-4747-9810-D91D71D8C0E4}
{6E9E4359-44F8-45AA-AEC5-D0F9FFBB13D6} = {E49881A9-09F6-442F-9E1D-6D87F5F837F1}
{09F77672-101E-4495-9D88-29376919C121} = {0371C5D8-D5F5-4747-9810-D91D71D8C0E4}
{BE27618A-2916-4269-9AD5-6BC5EDC32B30} = {0371C5D8-D5F5-4747-9810-D91D71D8C0E4}
{1C8262DB-7355-40A8-A2EC-4EED7363134A} = {E49881A9-09F6-442F-9E1D-6D87F5F837F1}
{C2C7BA09-F9EE-4E43-8EE4-871CC000342C} = {0371C5D8-D5F5-4747-9810-D91D71D8C0E4}
{4774F56D-16A8-4ABB-8C73-5F57609F1773} = {0371C5D8-D5F5-4747-9810-D91D71D8C0E4}
{D05FD93A-BC51-466E-BD56-3F3D6BBE6B06} = {E49881A9-09F6-442F-9E1D-6D87F5F837F1}
{B6D364E7-E5DB-4CF4-B87F-3CEDA3FF7478} = {0371C5D8-D5F5-4747-9810-D91D71D8C0E4}
{B815304D-502E-402C-ACE1-878DB4985CCC} = {F254F143-4704-4432-9995-20D87FA8BF8D}
{E4B72517-C694-486A-950E-6AB03C651FDC} = {F254F143-4704-4432-9995-20D87FA8BF8D}
{FAB4FFF2-964D-45FF-89CC-8BB9CE618ED1} = {0371C5D8-D5F5-4747-9810-D91D71D8C0E4}
{C56337BB-8CBC-4EE5-AB4D-8BB0A922813E} = {0371C5D8-D5F5-4747-9810-D91D71D8C0E4}
{7015E94D-D20D-48C8-86D7-6A996BE99E0E} = {0371C5D8-D5F5-4747-9810-D91D71D8C0E4}
{E9AA0AEB-AEAE-4B28-8D4D-17A6D7C89D17} = {E49881A9-09F6-442F-9E1D-6D87F5F837F1}
{6E9E4359-44F8-45AA-AEC5-D0F9FFBB13D6} = {E49881A9-09F6-442F-9E1D-6D87F5F837F1}
{1C8262DB-7355-40A8-A2EC-4EED7363134A} = {E49881A9-09F6-442F-9E1D-6D87F5F837F1}
{D05FD93A-BC51-466E-BD56-3F3D6BBE6B06} = {E49881A9-09F6-442F-9E1D-6D87F5F837F1}
{9BCCDA15-8907-4AE3-8871-2F17775DDE4C} = {E49881A9-09F6-442F-9E1D-6D87F5F837F1}
{1285FF43-F491-4BE0-B92C-37DA689CBD4B} = {E49881A9-09F6-442F-9E1D-6D87F5F837F1}
{B815304D-502E-402C-ACE1-878DB4985CCC} = {F254F143-4704-4432-9995-20D87FA8BF8D}
{E4B72517-C694-486A-950E-6AB03C651FDC} = {F254F143-4704-4432-9995-20D87FA8BF8D}
{04AEB008-EE4F-44DE-A361-2DBD2D0FD6A4} = {F254F143-4704-4432-9995-20D87FA8BF8D}
{6485EED4-C313-4551-9865-8ADCED603629} = {F254F143-4704-4432-9995-20D87FA8BF8D}
{143AFE8A-3490-444C-A82D-6A375EB59F01} = {F254F143-4704-4432-9995-20D87FA8BF8D}
{7015E94D-D20D-48C8-86D7-6A996BE99E0E} = {0371C5D8-D5F5-4747-9810-D91D71D8C0E4}
{1285FF43-F491-4BE0-B92C-37DA689CBD4B} = {E49881A9-09F6-442F-9E1D-6D87F5F837F1}
{5720BF06-2031-4AD8-B9B4-31A01E27ABB8} = {E07C6980-EB71-4D19-A80A-7BEB80B635B1}
{5C0CE30B-DD4A-4F7A-87C0-5243F0C86885} = {E07C6980-EB71-4D19-A80A-7BEB80B635B1}
{FCA21178-0411-45D6-B597-B7BE145CEE33} = {E07C6980-EB71-4D19-A80A-7BEB80B635B1}
{66AD4B7E-CF15-4A8F-8BF8-7E1BC6176D07} = {E07C6980-EB71-4D19-A80A-7BEB80B635B1}
{33599A6C-F340-4E1B-9B4D-CB8946C22140} = {E07C6980-EB71-4D19-A80A-7BEB80B635B1}
{256A4653-4287-44B3-BDEF-67FC1522ED2F} = {E07C6980-EB71-4D19-A80A-7BEB80B635B1}
{F6A18EB5-A8CC-4A39-9E85-5FA226019C3D} = {E07C6980-EB71-4D19-A80A-7BEB80B635B1}
{A0178BAA-A1AF-4C69-8E4A-A700A2723DDC} = {E07C6980-EB71-4D19-A80A-7BEB80B635B1}
{120437F0-08E1-4C05-81C6-937790A2550C} = {F254F143-4704-4432-9995-20D87FA8BF8D}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {5868B757-D821-41FC-952E-2113A0519506}
Expand Down
10 changes: 5 additions & 5 deletions src/libraries/System.Text.Json/gen/ContextGenerationSpec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,17 @@ namespace System.Text.Json.SourceGeneration
[DebuggerDisplay("ContextTypeRef={ContextTypeRef}")]
internal sealed class ContextGenerationSpec
{
public Location Location { get; init; }
public required Location Location { get; init; }

public JsonSourceGenerationOptionsAttribute GenerationOptions { get; init; }
public required JsonSourceGenerationOptionsAttribute GenerationOptions { get; init; }

public Type ContextType { get; init; }
public required Type ContextType { get; init; }

public List<TypeGenerationSpec> RootSerializableTypes { get; } = new();

public HashSet<TypeGenerationSpec>? ImplicitlyRegisteredTypes { get; } = new();
public HashSet<TypeGenerationSpec> ImplicitlyRegisteredTypes { get; } = new();

public List<string> ContextClassDeclarationList { get; init; }
public required List<string> ContextClassDeclarationList { get; init; }

/// <summary>
/// Types that we have initiated serialization metadata generation for. A type may be discoverable in the object graph,
Expand Down
28 changes: 22 additions & 6 deletions src/libraries/System.Text.Json/gen/JsonSourceGenerator.Emitter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ private void AddSource(string fileName, string source, bool isRootContextDef = f
int declarationCount = declarationList.Count;
Debug.Assert(declarationCount >= 1);

string @namespace = _currentContext.ContextType.Namespace;
string? @namespace = _currentContext.ContextType.Namespace;
bool isInGlobalNamespace = @namespace == JsonConstants.GlobalNamespaceValue;

StringBuilder sb = new(@"// <auto-generated/>
Expand Down Expand Up @@ -226,8 +226,9 @@ private void GenerateTypeInfo(TypeGenerationSpec typeGenerationSpec)
break;
case ClassType.Nullable:
{
source = GenerateForNullable(typeGenerationSpec);
Debug.Assert(typeGenerationSpec.NullableUnderlyingTypeMetadata != null);

source = GenerateForNullable(typeGenerationSpec);
GenerateTypeInfo(typeGenerationSpec.NullableUnderlyingTypeMetadata);
}
break;
Expand All @@ -238,21 +239,26 @@ private void GenerateTypeInfo(TypeGenerationSpec typeGenerationSpec)
break;
case ClassType.Enumerable:
{
source = GenerateForCollection(typeGenerationSpec);
Debug.Assert(typeGenerationSpec.CollectionValueTypeMetadata != null);

source = GenerateForCollection(typeGenerationSpec);
GenerateTypeInfo(typeGenerationSpec.CollectionValueTypeMetadata);
}
break;
case ClassType.Dictionary:
{
source = GenerateForCollection(typeGenerationSpec);
Debug.Assert(typeGenerationSpec.CollectionKeyTypeMetadata != null);
Debug.Assert(typeGenerationSpec.CollectionValueTypeMetadata != null);

source = GenerateForCollection(typeGenerationSpec);
GenerateTypeInfo(typeGenerationSpec.CollectionKeyTypeMetadata);
GenerateTypeInfo(typeGenerationSpec.CollectionValueTypeMetadata);
}
break;
case ClassType.Object:
{
Debug.Assert(typeGenerationSpec.PropertyGenSpecList != null);

source = GenerateForObject(typeGenerationSpec);

foreach (PropertyGenerationSpec spec in typeGenerationSpec.PropertyGenSpecList)
Expand Down Expand Up @@ -492,6 +498,8 @@ private string GenerateForCollection(TypeGenerationSpec typeGenerationSpec)

private string GenerateFastPathFuncForEnumerable(TypeGenerationSpec typeGenerationSpec)
{
Debug.Assert(typeGenerationSpec.CollectionValueTypeMetadata != null);

TypeGenerationSpec valueTypeGenerationSpec = typeGenerationSpec.CollectionValueTypeMetadata;

Type elementType = valueTypeGenerationSpec.Type;
Expand Down Expand Up @@ -542,6 +550,9 @@ private string GenerateFastPathFuncForEnumerable(TypeGenerationSpec typeGenerati

private string GenerateFastPathFuncForDictionary(TypeGenerationSpec typeGenerationSpec)
{
Debug.Assert(typeGenerationSpec.CollectionKeyTypeMetadata != null);
Debug.Assert(typeGenerationSpec.CollectionValueTypeMetadata != null);

TypeGenerationSpec keyTypeGenerationSpec = typeGenerationSpec.CollectionKeyTypeMetadata;
TypeGenerationSpec valueTypeGenerationSpec = typeGenerationSpec.CollectionValueTypeMetadata;

Expand Down Expand Up @@ -758,6 +769,8 @@ string GenerateCtorParamMetadataInitFunc(TypeGenerationSpec typeGenerationSpec)
{
const string parametersVarName = "parameters";

Debug.Assert(typeGenerationSpec.CtorParamGenSpecArray != null);

ParameterGenerationSpec[] parameters = typeGenerationSpec.CtorParamGenSpecArray;
int paramCount = parameters.Length;
Debug.Assert(paramCount > 0);
Expand Down Expand Up @@ -945,6 +958,8 @@ private static bool ShouldIncludePropertyForFastPath(PropertyGenerationSpec prop

private static string GetParameterizedCtorInvocationFunc(TypeGenerationSpec typeGenerationSpec)
{
Debug.Assert(typeGenerationSpec.CtorParamGenSpecArray != null);

ParameterGenerationSpec[] parameters = typeGenerationSpec.CtorParamGenSpecArray;
int paramCount = parameters.Length;
Debug.Assert(paramCount != 0);
Expand Down Expand Up @@ -1017,7 +1032,7 @@ private static string GenerateFastPathFuncForType(TypeGenerationSpec typeGenSpec
}}";
}

private static string GetEarlyNullCheckSource(bool canBeNull)
private static string? GetEarlyNullCheckSource(bool canBeNull)
{
return canBeNull
? $@"if ({ValueVarName} == null)
Expand Down Expand Up @@ -1355,7 +1370,8 @@ string GetParamDefaultValueAsString(object? value, Type type, string typeRef)
{
// Roslyn gives us an instance of the underlying type, which is numerical.
#if DEBUG
Type runtimeType = _generationSpec.MetadataLoadContext.Resolve(value.GetType());
Type? runtimeType = _generationSpec.MetadataLoadContext.Resolve(value.GetType()!);
Debug.Assert(runtimeType != null);
Debug.Assert(_generationSpec.IsNumberType(runtimeType));
#endif

Expand Down
Loading