You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
NullabilityInfoContext returns incorrect results for an indexer parameter if the class has a property with a value type return type. #101071
If you have a class with:
a) a property which returns a value tuple; and
b) an indexer with a non-nullable reference type parameter
then when looking at the NullabilityInfoContext for the indexer parameter, it appears as Unknown instead of NotNull.
Reproduction Steps
Create a console app with the following code:
internalclassProgram{publicclassFoo{publicstringthis[stringname]=>thrownewNotImplementedException();// *** If you comment out this property, the problem goes away ***public(intA,intB)ValueTupleProp{get;}}staticvoidMain(string[]args){// Get the PropertyInfo for the indexer.PropertyInfopropInfo=typeof(Foo).GetProperty("Item")!;// Get the ParameterInfo for the indexer parameter (non-nullable string).ParameterInfoparamInfo=propInfo.GetIndexParameters()[0];varnullabilityInfo=newNullabilityInfoContext().Create(paramInfo);Console.WriteLine($"{nullabilityInfo.ReadState}, {nullabilityInfo.WriteState}");Console.ReadKey();}}
Expected behavior
The NullabilityInfoContext properties report NotNull.
Actual behavior
The NullabilityInfoContext properties report Unknown. However, if you comment out the ValueTupleProp property, then it magically works as expected.
Regression?
No response
Known Workarounds
No response
Configuration
Tried running in .NET 6.0 and .NET 8.0 on Windows 11.
The reason is because NullabilityInfoContext.Create retrieves NullableContextAttribute from ParameterInfo.Member, which is the indexer property for indexed parameters rather than the getter method, where NullableContext is annotated in IL.
As for the ValueTuple padding, when there's only one property in a type, the nullability context is annotated on the type itself, therefore the member's annotation isn't involved.
Activity
dotnet-policy-service commentedon Apr 15, 2024
Tagging subscribers to this area: @dotnet/area-system-reflection
See info in area-owners.md if you want to be subscribed.
karakasa commentedon May 25, 2024
The reason is because
NullabilityInfoContext.CreateretrievesNullableContextAttributefromParameterInfo.Member, which is the indexer property for indexed parameters rather than the getter method, whereNullableContextis annotated in IL.As for the
ValueTuplepadding, when there's only one property in a type, the nullability context is annotated on the type itself, therefore the member's annotation isn't involved.fix dotnet#101071