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

Fix binder gen nullability issue wrt binding ref type members #89900

Merged
merged 1 commit into from
Aug 3, 2023
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -73,7 +73,8 @@ private void EmitBindCoreCall(
string tempIdentifier = GetIncrementalIdentifier(Identifier.temp);
if (initKind is InitializationKind.AssignmentWithNullCheck)
{
_writer.WriteLine($"{type.MinimalDisplayString} {tempIdentifier} = {memberAccessExpr};");
Debug.Assert(!type.IsValueType);
_writer.WriteLine($"{type.MinimalDisplayString}? {tempIdentifier} = {memberAccessExpr};");
EmitBindCoreCall(tempIdentifier, InitializationKind.AssignmentWithNullCheck);
}
else if (initKind is InitializationKind.None && type.IsValueType)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -837,14 +837,14 @@ private bool EmitBindImplForMember(

EmitStartBlock($"if ({sectionValidationCall} is {Identifier.IConfigurationSection} {sectionIdentifier})");

bool success = !EmitInitException(effectiveMemberType);
if (success)
bool canInit = !EmitInitException(effectiveMemberType);
if (canInit)
{
EmitBindCoreCallForMember(member, memberAccessExpr, sectionIdentifier, canSet);
}

EmitEndBlock();
return success;
return canInit;
}

private void EmitBindCoreCallForMember(
Expand All @@ -856,8 +856,6 @@ private void EmitBindCoreCallForMember(

TypeSpec memberType = member.Type;
TypeSpec effectiveMemberType = memberType.EffectiveType;
string effectiveMemberTypeDisplayString = effectiveMemberType.MinimalDisplayString;
bool canGet = member.CanGet;

string tempIdentifier = GetIncrementalIdentifier(Identifier.temp);
InitializationKind initKind;
Expand All @@ -871,6 +869,7 @@ private void EmitBindCoreCallForMember(
}

Debug.Assert(canSet);
string effectiveMemberTypeDisplayString = effectiveMemberType.MinimalDisplayString;
initKind = InitializationKind.None;

if (memberType.SpecKind is TypeSpecKind.Nullable)
Expand All @@ -889,7 +888,7 @@ private void EmitBindCoreCallForMember(

targetObjAccessExpr = tempIdentifier;
}
else if (canGet)
else if (member.CanGet)
{
targetObjAccessExpr = memberAccessExpr;
initKind = InitializationKind.AssignmentWithNullCheck;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -605,7 +605,7 @@ public class QueueNamespaces
{
public string Namespace { get; set; }

public Dictionary<string, QueueProperties> Queues { get; set; } = new();
public Dictionary<string, QueueProperties>? Queues { get; set; } = new();
}

public class QueueProperties
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,31 +198,31 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration

if (AsConfigWithChildren(configuration.GetSection("CustomDictionary")) is IConfigurationSection section1)
{
Program.CustomDictionary<string, int> temp3 = obj.CustomDictionary;
Program.CustomDictionary<string, int>? temp3 = obj.CustomDictionary;
temp3 ??= new Program.CustomDictionary<string, int>();
BindCore(section1, ref temp3, binderOptions);
obj.CustomDictionary = temp3;
}

if (AsConfigWithChildren(configuration.GetSection("CustomList")) is IConfigurationSection section4)
{
Program.CustomList temp6 = obj.CustomList;
Program.CustomList? temp6 = obj.CustomList;
temp6 ??= new Program.CustomList();
BindCore(section4, ref temp6, binderOptions);
obj.CustomList = temp6;
}

if (AsConfigWithChildren(configuration.GetSection("IReadOnlyList")) is IConfigurationSection section7)
{
IReadOnlyList<int> temp9 = obj.IReadOnlyList;
IReadOnlyList<int>? temp9 = obj.IReadOnlyList;
temp9 = temp9 is null ? new List<int>() : new List<int>(temp9);
BindCore(section7, ref temp9, binderOptions);
obj.IReadOnlyList = temp9;
}

if (AsConfigWithChildren(configuration.GetSection("IReadOnlyDictionary")) is IConfigurationSection section10)
{
IReadOnlyDictionary<string, int> temp12 = obj.IReadOnlyDictionary;
IReadOnlyDictionary<string, int>? temp12 = obj.IReadOnlyDictionary;
temp12 = temp12 is null ? new Dictionary<string, int>() : temp12.ToDictionary(pair => pair.Key, pair => pair.Value);
BindCore(section10, ref temp12, binderOptions);
obj.IReadOnlyDictionary = temp12;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,23 +97,23 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration

if (AsConfigWithChildren(configuration.GetSection("MyList")) is IConfigurationSection section2)
{
List<int> temp4 = obj.MyList;
List<int>? temp4 = obj.MyList;
temp4 ??= new List<int>();
BindCore(section2, ref temp4, binderOptions);
obj.MyList = temp4;
}

if (AsConfigWithChildren(configuration.GetSection("MyDictionary")) is IConfigurationSection section5)
{
Dictionary<string, string> temp7 = obj.MyDictionary;
Dictionary<string, string>? temp7 = obj.MyDictionary;
temp7 ??= new Dictionary<string, string>();
BindCore(section5, ref temp7, binderOptions);
obj.MyDictionary = temp7;
}

if (AsConfigWithChildren(configuration.GetSection("MyComplexDictionary")) is IConfigurationSection section8)
{
Dictionary<string, Program.MyClass2> temp10 = obj.MyComplexDictionary;
Dictionary<string, Program.MyClass2>? temp10 = obj.MyComplexDictionary;
temp10 ??= new Dictionary<string, Program.MyClass2>();
BindCore(section8, ref temp10, binderOptions);
obj.MyComplexDictionary = temp10;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,23 +91,23 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration

if (AsConfigWithChildren(configuration.GetSection("MyList")) is IConfigurationSection section2)
{
List<int> temp4 = obj.MyList;
List<int>? temp4 = obj.MyList;
temp4 ??= new List<int>();
BindCore(section2, ref temp4, binderOptions);
obj.MyList = temp4;
}

if (AsConfigWithChildren(configuration.GetSection("MyDictionary")) is IConfigurationSection section5)
{
Dictionary<string, string> temp7 = obj.MyDictionary;
Dictionary<string, string>? temp7 = obj.MyDictionary;
temp7 ??= new Dictionary<string, string>();
BindCore(section5, ref temp7, binderOptions);
obj.MyDictionary = temp7;
}

if (AsConfigWithChildren(configuration.GetSection("MyComplexDictionary")) is IConfigurationSection section8)
{
Dictionary<string, Program.MyClass2> temp10 = obj.MyComplexDictionary;
Dictionary<string, Program.MyClass2>? temp10 = obj.MyComplexDictionary;
temp10 ??= new Dictionary<string, Program.MyClass2>();
BindCore(section8, ref temp10, binderOptions);
obj.MyComplexDictionary = temp10;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,23 +91,23 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration

if (AsConfigWithChildren(configuration.GetSection("MyList")) is IConfigurationSection section2)
{
List<int> temp4 = obj.MyList;
List<int>? temp4 = obj.MyList;
temp4 ??= new List<int>();
BindCore(section2, ref temp4, binderOptions);
obj.MyList = temp4;
}

if (AsConfigWithChildren(configuration.GetSection("MyDictionary")) is IConfigurationSection section5)
{
Dictionary<string, string> temp7 = obj.MyDictionary;
Dictionary<string, string>? temp7 = obj.MyDictionary;
temp7 ??= new Dictionary<string, string>();
BindCore(section5, ref temp7, binderOptions);
obj.MyDictionary = temp7;
}

if (AsConfigWithChildren(configuration.GetSection("MyComplexDictionary")) is IConfigurationSection section8)
{
Dictionary<string, Program.MyClass2> temp10 = obj.MyComplexDictionary;
Dictionary<string, Program.MyClass2>? temp10 = obj.MyComplexDictionary;
temp10 ??= new Dictionary<string, Program.MyClass2>();
BindCore(section8, ref temp10, binderOptions);
obj.MyComplexDictionary = temp10;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,23 +91,23 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration

if (AsConfigWithChildren(configuration.GetSection("MyList")) is IConfigurationSection section2)
{
List<int> temp4 = obj.MyList;
List<int>? temp4 = obj.MyList;
temp4 ??= new List<int>();
BindCore(section2, ref temp4, binderOptions);
obj.MyList = temp4;
}

if (AsConfigWithChildren(configuration.GetSection("MyDictionary")) is IConfigurationSection section5)
{
Dictionary<string, string> temp7 = obj.MyDictionary;
Dictionary<string, string>? temp7 = obj.MyDictionary;
temp7 ??= new Dictionary<string, string>();
BindCore(section5, ref temp7, binderOptions);
obj.MyDictionary = temp7;
}

if (AsConfigWithChildren(configuration.GetSection("MyComplexDictionary")) is IConfigurationSection section8)
{
Dictionary<string, Program.MyClass2> temp10 = obj.MyComplexDictionary;
Dictionary<string, Program.MyClass2>? temp10 = obj.MyComplexDictionary;
temp10 ??= new Dictionary<string, Program.MyClass2>();
BindCore(section8, ref temp10, binderOptions);
obj.MyComplexDictionary = temp10;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,23 +129,23 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration

if (AsConfigWithChildren(configuration.GetSection("MyList")) is IConfigurationSection section6)
{
List<int> temp8 = obj.MyList;
List<int>? temp8 = obj.MyList;
temp8 ??= new List<int>();
BindCore(section6, ref temp8, binderOptions);
obj.MyList = temp8;
}

if (AsConfigWithChildren(configuration.GetSection("MyArray")) is IConfigurationSection section9)
{
int[] temp11 = obj.MyArray;
int[]? temp11 = obj.MyArray;
temp11 ??= new int[0];
BindCore(section9, ref temp11, binderOptions);
obj.MyArray = temp11;
}

if (AsConfigWithChildren(configuration.GetSection("MyDictionary")) is IConfigurationSection section12)
{
Dictionary<string, string> temp14 = obj.MyDictionary;
Dictionary<string, string>? temp14 = obj.MyDictionary;
temp14 ??= new Dictionary<string, string>();
BindCore(section12, ref temp14, binderOptions);
obj.MyDictionary = temp14;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,23 +112,23 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration

if (AsConfigWithChildren(configuration.GetSection("MyList")) is IConfigurationSection section5)
{
List<int> temp7 = obj.MyList;
List<int>? temp7 = obj.MyList;
temp7 ??= new List<int>();
BindCore(section5, ref temp7, binderOptions);
obj.MyList = temp7;
}

if (AsConfigWithChildren(configuration.GetSection("MyArray")) is IConfigurationSection section8)
{
int[] temp10 = obj.MyArray;
int[]? temp10 = obj.MyArray;
temp10 ??= new int[0];
BindCore(section8, ref temp10, binderOptions);
obj.MyArray = temp10;
}

if (AsConfigWithChildren(configuration.GetSection("MyDictionary")) is IConfigurationSection section11)
{
Dictionary<string, string> temp13 = obj.MyDictionary;
Dictionary<string, string>? temp13 = obj.MyDictionary;
temp13 ??= new Dictionary<string, string>();
BindCore(section11, ref temp13, binderOptions);
obj.MyDictionary = temp13;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,23 +112,23 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration

if (AsConfigWithChildren(configuration.GetSection("MyList")) is IConfigurationSection section5)
{
List<int> temp7 = obj.MyList;
List<int>? temp7 = obj.MyList;
temp7 ??= new List<int>();
BindCore(section5, ref temp7, binderOptions);
obj.MyList = temp7;
}

if (AsConfigWithChildren(configuration.GetSection("MyArray")) is IConfigurationSection section8)
{
int[] temp10 = obj.MyArray;
int[]? temp10 = obj.MyArray;
temp10 ??= new int[0];
BindCore(section8, ref temp10, binderOptions);
obj.MyArray = temp10;
}

if (AsConfigWithChildren(configuration.GetSection("MyDictionary")) is IConfigurationSection section11)
{
Dictionary<string, string> temp13 = obj.MyDictionary;
Dictionary<string, string>? temp13 = obj.MyDictionary;
temp13 ??= new Dictionary<string, string>();
BindCore(section11, ref temp13, binderOptions);
obj.MyDictionary = temp13;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration

if (AsConfigWithChildren(configuration.GetSection("MyList")) is IConfigurationSection section3)
{
List<int> temp5 = obj.MyList;
List<int>? temp5 = obj.MyList;
temp5 ??= new List<int>();
BindCore(section3, ref temp5, binderOptions);
obj.MyList = temp5;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration

if (AsConfigWithChildren(configuration.GetSection("MyList")) is IConfigurationSection section3)
{
List<int> temp5 = obj.MyList;
List<int>? temp5 = obj.MyList;
temp5 ??= new List<int>();
BindCore(section3, ref temp5, binderOptions);
obj.MyList = temp5;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration

if (AsConfigWithChildren(configuration.GetSection("MyList")) is IConfigurationSection section3)
{
List<int> temp5 = obj.MyList;
List<int>? temp5 = obj.MyList;
temp5 ??= new List<int>();
BindCore(section3, ref temp5, binderOptions);
obj.MyList = temp5;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,23 +150,23 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration

if (AsConfigWithChildren(configuration.GetSection("MyList")) is IConfigurationSection section5)
{
List<int> temp7 = obj.MyList;
List<int>? temp7 = obj.MyList;
temp7 ??= new List<int>();
BindCore(section5, ref temp7, binderOptions);
obj.MyList = temp7;
}

if (AsConfigWithChildren(configuration.GetSection("MyList2")) is IConfigurationSection section8)
{
List<Program.MyClass2> temp10 = obj.MyList2;
List<Program.MyClass2>? temp10 = obj.MyList2;
temp10 ??= new List<Program.MyClass2>();
BindCore(section8, ref temp10, binderOptions);
obj.MyList2 = temp10;
}

if (AsConfigWithChildren(configuration.GetSection("MyDictionary")) is IConfigurationSection section11)
{
Dictionary<string, string> temp13 = obj.MyDictionary;
Dictionary<string, string>? temp13 = obj.MyDictionary;
temp13 ??= new Dictionary<string, string>();
BindCore(section11, ref temp13, binderOptions);
obj.MyDictionary = temp13;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,23 +150,23 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration

if (AsConfigWithChildren(configuration.GetSection("MyList")) is IConfigurationSection section5)
{
List<int> temp7 = obj.MyList;
List<int>? temp7 = obj.MyList;
temp7 ??= new List<int>();
BindCore(section5, ref temp7, binderOptions);
obj.MyList = temp7;
}

if (AsConfigWithChildren(configuration.GetSection("MyList2")) is IConfigurationSection section8)
{
List<Program.MyClass2> temp10 = obj.MyList2;
List<Program.MyClass2>? temp10 = obj.MyList2;
temp10 ??= new List<Program.MyClass2>();
BindCore(section8, ref temp10, binderOptions);
obj.MyList2 = temp10;
}

if (AsConfigWithChildren(configuration.GetSection("MyDictionary")) is IConfigurationSection section11)
{
Dictionary<string, string> temp13 = obj.MyDictionary;
Dictionary<string, string>? temp13 = obj.MyDictionary;
temp13 ??= new Dictionary<string, string>();
BindCore(section11, ref temp13, binderOptions);
obj.MyDictionary = temp13;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,23 +150,23 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration

if (AsConfigWithChildren(configuration.GetSection("MyList")) is IConfigurationSection section5)
{
List<int> temp7 = obj.MyList;
List<int>? temp7 = obj.MyList;
temp7 ??= new List<int>();
BindCore(section5, ref temp7, binderOptions);
obj.MyList = temp7;
}

if (AsConfigWithChildren(configuration.GetSection("MyList2")) is IConfigurationSection section8)
{
List<Program.MyClass2> temp10 = obj.MyList2;
List<Program.MyClass2>? temp10 = obj.MyList2;
temp10 ??= new List<Program.MyClass2>();
BindCore(section8, ref temp10, binderOptions);
obj.MyList2 = temp10;
}

if (AsConfigWithChildren(configuration.GetSection("MyDictionary")) is IConfigurationSection section11)
{
Dictionary<string, string> temp13 = obj.MyDictionary;
Dictionary<string, string>? temp13 = obj.MyDictionary;
temp13 ??= new Dictionary<string, string>();
BindCore(section11, ref temp13, binderOptions);
obj.MyDictionary = temp13;
Expand Down
Loading