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 NullabilityInfoContext.Create to properly analyze types with nested generic types #63556

Merged
merged 3 commits into from
Jan 18, 2022
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 @@ -341,10 +341,13 @@ private NotAnnotatedStatus PopulateAnnotationInfo(IList<CustomAttributeData> cus
return NotAnnotatedStatus.None;
}

private NullabilityInfo GetNullabilityInfo(MemberInfo memberInfo, Type type, IList<CustomAttributeData> customAttributes) =>
GetNullabilityInfo(memberInfo, type, customAttributes, 0);
private NullabilityInfo GetNullabilityInfo(MemberInfo memberInfo, Type type, IList<CustomAttributeData> customAttributes)
{
int index = 0;
return GetNullabilityInfo(memberInfo, type, customAttributes, ref index);
}

private NullabilityInfo GetNullabilityInfo(MemberInfo memberInfo, Type type, IList<CustomAttributeData> customAttributes, int index)
private NullabilityInfo GetNullabilityInfo(MemberInfo memberInfo, Type type, IList<CustomAttributeData> customAttributes, ref int index)
{
NullabilityState state = NullabilityState.Unknown;
NullabilityInfo? elementState = null;
Expand All @@ -364,17 +367,22 @@ private NullabilityInfo GetNullabilityInfo(MemberInfo memberInfo, Type type, ILi
underlyingType = type;
state = NullabilityState.NotNull;
}

if (underlyingType.IsGenericType)
{
index++;
}
}
else
{
if (!ParseNullableState(customAttributes, index, ref state))
if (!ParseNullableState(customAttributes, index++, ref state))
{
state = GetNullableContext(memberInfo);
}

if (type.IsArray)
{
elementState = GetNullabilityInfo(memberInfo, type.GetElementType()!, customAttributes, index + 1);
elementState = GetNullabilityInfo(memberInfo, type.GetElementType()!, customAttributes, ref index);
}
}

Expand All @@ -383,16 +391,9 @@ private NullabilityInfo GetNullabilityInfo(MemberInfo memberInfo, Type type, ILi
Type[] genericArguments = underlyingType.GetGenericArguments();
genericArgumentsState = new NullabilityInfo[genericArguments.Length];

for (int i = 0, offset = 0; i < genericArguments.Length; i++)
for (int i = 0; i < genericArguments.Length; i++)
{
Type t = Nullable.GetUnderlyingType(genericArguments[i]) ?? genericArguments[i];

if (!t.IsValueType || t.IsGenericType)
{
offset++;
}

genericArgumentsState[i] = GetNullabilityInfo(memberInfo, genericArguments[i], customAttributes, index + offset);
genericArgumentsState[i] = GetNullabilityInfo(memberInfo, genericArguments[i], customAttributes, ref index);
}
}

Expand Down Expand Up @@ -500,7 +501,8 @@ private void CheckGenericParameters(NullabilityInfo nullability, MemberInfo meta
{
if (genericArguments[i].IsGenericParameter)
{
NullabilityInfo n = GetNullabilityInfo(metaMember, genericArguments[i], genericArguments[i].GetCustomAttributesData(), i + 1);
int index = i + 1;
NullabilityInfo n = GetNullabilityInfo(metaMember, genericArguments[i], genericArguments[i].GetCustomAttributesData(), ref index);
nullability.GenericTypeArguments[i].ReadState = n.ReadState;
nullability.GenericTypeArguments[i].WriteState = n.WriteState;
}
Expand All @@ -523,7 +525,8 @@ private void UpdateGenericArrayElements(NullabilityInfo? elementState, MemberInf
&& metaType.GetElementType()!.IsGenericParameter)
{
Type elementType = metaType.GetElementType()!;
NullabilityInfo n = GetNullabilityInfo(metaMember, elementType, elementType.GetCustomAttributesData(), 0);
int index = 0;
NullabilityInfo n = GetNullabilityInfo(metaMember, elementType, elementType.GetCustomAttributesData(), ref index);
elementState.ReadState = n.ReadState;
elementState.WriteState = n.WriteState;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -850,6 +850,42 @@ public void AttributedParametersTest()
Assert.Equal(NullabilityState.NotNull, nullabilityContext.Create(tryGetOutParameters[0]).ReadState);
}

public static IEnumerable<object[]> NestedGenericsCorrectOrderData()
{
yield return new object[] {
"MethodReturnsNonTupleNonTupleNullStringNullStringNullString",
new[] { NullabilityState.NotNull, NullabilityState.NotNull, NullabilityState.Nullable, NullabilityState.Nullable, NullabilityState.Nullable },
new[] { typeof(Tuple<Tuple<string, string>, string>), typeof(Tuple<string, string>), typeof(string), typeof(string), typeof(string) }
};
yield return new object[] {
"MethodReturnsNonTupleNonTupleNullStringNullStringNonString",
new[] { NullabilityState.NotNull, NullabilityState.NotNull, NullabilityState.Nullable, NullabilityState.Nullable, NullabilityState.NotNull },
new[] { typeof(Tuple<Tuple<string, string>, string>), typeof(Tuple<string, string>), typeof(string), typeof(string), typeof(string) }
};
}

[Theory]
[MemberData(nameof(NestedGenericsCorrectOrderData))]
[SkipOnMono("Nullability attributes trimmed on Mono")]
public void NestedGenericsCorrectOrderTest(string methodName, NullabilityState[] nullStates, Type[] types)
{
NullabilityInfo parentInfo = nullabilityContext.Create(typeof(TypeWithNotNullContext).GetMethod(methodName)!.ReturnParameter);
var dataIndex = 0;
Examine(parentInfo);
Assert.Equal(nullStates.Length, dataIndex);
Assert.Equal(types.Length, dataIndex);

void Examine(NullabilityInfo info)
{
Assert.Equal(types[dataIndex], info.Type);
Assert.Equal(nullStates[dataIndex++], info.ReadState);
for (var i = 0; i < info.GenericTypeArguments.Length; i++)
{
Examine(info.GenericTypeArguments[i]);
}
}
}

public static IEnumerable<object[]> NestedGenericsReturnParameterData()
{
// public IEnumerable<Tuple<(string name, object? value), int>?> MethodReturnsEnumerableNonTupleNonNonNullValueTupleNonNullNon() => null!;
Expand Down Expand Up @@ -1089,6 +1125,8 @@ public void MethodParametersUnknown(string s, IDictionary<Type, string[]> dict)
public IEnumerable<GenericStruct<Tuple<string, object?>?, int>?>? MethodReturnsEnumerableNullStructNullNonNullTupleNonNullNull() => null;
public IEnumerable<Tuple<GenericStruct<string, object?>?, int>?>? MethodReturnsEnumerableNullTupleNullNonNullStructNonNullNull() => null;
public IEnumerable<(GenericStruct<string, object?> str, int? count)> MethodReturnsEnumerableNonValueTupleNonNullNonStructNonNullNon() => null!;
public Tuple<Tuple<string?, string?>, string?> MethodReturnsNonTupleNonTupleNullStringNullStringNullString() => null!;
public Tuple<Tuple<string?, string?>, string> MethodReturnsNonTupleNonTupleNullStringNullStringNonString() => null!;
public void MethodNullNonNullNonNon(string? s, IDictionary<Type, string?[]> dict) { }
public void MethodNonNullNonNullNotNull(string s, [NotNull] IDictionary<Type, string[]?>? dict) { dict = new Dictionary<Type, string[]?>(); }
public void MethodNullNonNullNullNon(string? s, IDictionary<Type, string?[]?> dict) { }
Expand Down