Fix KeyValuePair nullability detection on Mono #120910
Open
+2
−1
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fix KeyValuePair nullability detection on Mono
Summary
Fixes incorrect nullability detection for
KeyValuePair<TKey, TValue>generic parameters on Mono, which was causing ASP.NET Core DataAnnotations tests to fail.Problem
On Mono,
KeyValuePair<TKey, TValue>generic parameters (TKeyandTValue) were being detected asNotNullinstead ofNullable, while CoreCLR correctly detects them asNullable.Root Cause: Mono's
KeyValuePair<TKey, TValue>generic parameters are missing theNullableAttribute(2)that CoreCLR has. This causesTryUpdateGenericParameterNullabilityto returnfalseand fall back toNullableContextAttribute(1)=NotNull.Failing Tests
Microsoft.AspNetCore.Mvc.DataAnnotations.DataAnnotationsMetadataProviderTest.IsNullableReferenceType_ReturnsFalse_ForKeyValuePairWithoutNullableConstraintsMicrosoft.AspNetCore.Mvc.DataAnnotations.DataAnnotationsMetadataProviderTest.IsNullableReferenceType_ReturnsTrue_ForKeyValuePairWithNullableConstraintsSolution
Added special handling in
TryUpdateGenericParameterNullabilityto treatKeyValuePair<TKey, TValue>generic parameters as nullable by default when nullable attributes are missing.The fix:
System.Collections.Generic.KeyValuePair2`ReadStateandWriteStatetoNullableTesting
Reproduction Case
Test Results
Files Changed
src/libraries/System.Private.CoreLib/src/System/Reflection/NullabilityInfoContext.csVerification
The fix has been tested with the full ASP.NET Core DataAnnotations test suite and successfully resolves the failing tests without introducing regressions.
cc: @giritrivedi