Skip to content

Commit b61c3e8

Browse files
authored
Added another case in the switch block to handle the object returned by Mono which differs from the object returned by CoreCLR (#115885)
* Fixes #115295 . Added another case in the switch block to handle the object returned by Mono which differs from CoreCLR. * Added test cases where we see a discrepancy in NullabilityState between Mono and CoreCLR
1 parent d91f6c4 commit b61c3e8

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

src/libraries/System.Private.CoreLib/src/System/Reflection/NullabilityInfoContext.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -657,6 +657,11 @@ public bool ParseNullableState(int index, ref NullabilityState state)
657657
when index < args.Count && args[index].Value is byte elementB:
658658
state = TranslateByte(elementB);
659659
return true;
660+
#if MONO
661+
case byte[] ba when index < ba.Length:
662+
state = TranslateByte(ba[index]);
663+
return true;
664+
#endif
660665
default:
661666
return false;
662667
}

src/libraries/System.Runtime/tests/System.Runtime.Tests/System/Reflection/NullabilityInfoContextTests.cs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,57 @@ public void FieldTest(string fieldName, NullabilityState readState, NullabilityS
5050
Assert.Null(nullability.ElementType);
5151
}
5252

53+
public class FI_FieldArray
54+
{
55+
public int[] intArray;
56+
public object?[] objectArray;
57+
public string?[] stringArray;
58+
}
59+
60+
public class FI_GenericClassField<T>
61+
{
62+
public object?[] objectArray;
63+
public T? genparamField;
64+
public T?[] gparrayField;
65+
}
66+
67+
public static IEnumerable<object[]> FieldArrayTestData()
68+
{
69+
yield return new object[] { "objectArray", NullabilityState.NotNull, NullabilityState.NotNull, typeof(object[]) };
70+
yield return new object[] { "stringArray", NullabilityState.NotNull, NullabilityState.NotNull, typeof(string[]) };
71+
}
72+
73+
[Theory]
74+
[MemberData(nameof(FieldArrayTestData))]
75+
public void FieldArrayTest(string fieldName, NullabilityState readState, NullabilityState writeState, Type type)
76+
{
77+
FieldInfo field = typeof(FI_FieldArray).GetField(fieldName, flags);
78+
NullabilityInfo nullability = nullabilityContext.Create(field);
79+
Assert.Equal(readState, nullability.ReadState);
80+
Assert.Equal(writeState, nullability.WriteState);
81+
Assert.Equal(type, nullability.Type);
82+
Assert.Empty(nullability.GenericTypeArguments);
83+
}
84+
85+
public static IEnumerable<object[]> GenericArrayFieldTypeTestData()
86+
{
87+
yield return new object[] { "objectArray", NullabilityState.NotNull, NullabilityState.NotNull, typeof(object[]) };
88+
yield return new object[] { "genparamField", NullabilityState.NotNull, NullabilityState.NotNull, typeof(int) };
89+
yield return new object[] { "gparrayField", NullabilityState.NotNull, NullabilityState.NotNull, typeof(int[]) };
90+
}
91+
92+
[Theory]
93+
[MemberData(nameof(GenericArrayFieldTypeTestData))]
94+
public void GenericArrayFieldTypeTest(string fieldName, NullabilityState readState, NullabilityState writeState, Type type)
95+
{
96+
FieldInfo field = typeof(FI_GenericClassField<int>).GetField(fieldName, flags)!;
97+
NullabilityInfo nullability = nullabilityContext.Create(field);
98+
Assert.Equal(readState, nullability.ReadState);
99+
Assert.Equal(writeState, nullability.WriteState);
100+
Assert.Equal(type, nullability.Type);
101+
Assert.Empty(nullability.GenericTypeArguments);
102+
}
103+
53104
public static IEnumerable<object[]> EventTestData()
54105
{
55106
yield return new object[] { "EventNullable", NullabilityState.Nullable, NullabilityState.Nullable, typeof(EventHandler) };

0 commit comments

Comments
 (0)