-
Notifications
You must be signed in to change notification settings - Fork 549
[Foundation] Fix nullability in NSMutableDictionary<TKey, TValue>. #24458
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
base: main
Are you sure you want to change the base?
[Foundation] Fix nullability in NSMutableDictionary<TKey, TValue>. #24458
Conversation
This is file 40 of 47 files with nullability disabled in Foundation.
* Enabled nullable reference types.
* Removed four SupportedOSPlatform attributes without version numbers.
* Replaced 'To be added.' XML comments with proper documentation.
* Added XML documentation for all public and protected members.
* Used ArgumentNullException.ThrowIfNull for parameter validation.
* Fixed XML comment formatting and added see cref attributes.
* Made ObjectForKey return nullable TValue? since keys may not exist.
* Made TryGetValue parameter nullable with NotNullWhen(true) attribute.
* Kept IDictionary interface implementations non-nullable per .NET contract.
* Used KeyNotFoundException when indexer accesses missing keys.
* Fixed IEnumerator implementations to skip null values in enumeration.
* Changed empty array initialization from 'new TValue[]{}' to '[]'.
* Remove some dead code.
* Add a few more tests.
Contributes towards #17285.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This PR enables nullable reference types for NSMutableDictionary<TKey, TValue>, which is file 40 of 47 in the Foundation nullability migration effort. The changes improve type safety by making nullability explicit in method signatures, particularly for methods that can return null for missing keys like ObjectForKey. The PR also improves the indexer behavior to throw KeyNotFoundException for missing keys (aligning with .NET conventions), replaces placeholder XML documentation with comprehensive summaries, and modernizes parameter validation using ArgumentNullException.ThrowIfNull.
Key changes:
- Made
ObjectForKeyreturnTValue?(nullable) since keys may not exist in the dictionary - Changed public indexer getter to throw
KeyNotFoundExceptionfor missing keys instead of returning null - Added comprehensive XML documentation for all public members
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
| src/Foundation/NSMutableDictionary_2.cs | Enabled nullable reference types, updated method signatures for nullability, replaced placeholder XML docs with comprehensive documentation, modernized parameter validation, changed indexer to throw KeyNotFoundException for missing keys |
| tests/monotouch-test/Foundation/NSMutableDictionary2Test.cs | Added comprehensive tests for missing key access behavior, testing ObjectForKey, TryGetValue, ContainsKey, indexer, and IDictionary interface behavior |
| tests/cecil-tests/Documentation.KnownFailures.txt | Removed entries for methods that now have proper XML documentation |
| { | ||
| return TryGetValue (key, out value); | ||
| var result = TryGetValue (key, out var nullableValue); | ||
| value = nullableValue!; |
Copilot
AI
Dec 16, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The coding guidelines state "Don't ever use ! to handle null!" The null-forgiving operator should be avoided. Instead, this could be rewritten to avoid the null-forgiving operator, for example by using a conditional assignment or restructuring the code.
| // But accessing via IDictionary interface should return null | ||
| IDictionary<NSString, NSDate> idict = dict; | ||
| Assert.IsNull (idict [keyMissing], "missing key via interface"); |
Copilot
AI
Dec 16, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The test expects the IDictionary indexer to return null for missing keys, but the implementation at line 380 calls the public indexer which throws KeyNotFoundException. According to the .NET IDictionary interface contract, the indexer getter should throw KeyNotFoundException when the key is not found, not return null. Either this test assertion is incorrect, or the IDictionary indexer implementation should be changed to return ObjectForKey directly to maintain backwards compatibility (though that would violate .NET conventions).
| // But IDictionary indexer should return null | ||
| IDictionary<NSString, NSDate> idict = dict; | ||
| Assert.IsNull (idict [keyMissing], "IDictionary indexer missing"); |
Copilot
AI
Dec 16, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The test expects the IDictionary indexer to return null for missing keys, but the implementation at line 380 calls the public indexer which throws KeyNotFoundException. According to the .NET IDictionary interface contract, the indexer getter should throw KeyNotFoundException when the key is not found, not return null. Either this test assertion is incorrect, or the IDictionary indexer implementation should be changed to return ObjectForKey directly to maintain backwards compatibility (though that would violate .NET conventions).
| IDictionary<NSString, NSDate> idict = dict; | ||
| Assert.IsNull (idict [keyMissing], "IDictionary indexer"); |
Copilot
AI
Dec 16, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The test expects the IDictionary indexer to return null for missing keys, but the implementation at line 380 calls the public indexer which throws KeyNotFoundException. According to the .NET IDictionary interface contract, the indexer getter should throw KeyNotFoundException when the key is not found, not return null. Either this test assertion is incorrect, or the IDictionary indexer implementation should be changed to return ObjectForKey directly to maintain backwards compatibility (though that would violate .NET conventions).
✅ [PR Build #7f9f0c3] Build passed (Detect API changes) ✅Pipeline on Agent |
✅ [CI Build #7f9f0c3] Build passed (Build packages) ✅Pipeline on Agent |
✅ API diff for current PR / commitNET (empty diffs)✅ API diff vs stableNET (empty diffs)ℹ️ Generator diffGenerator Diff: vsdrops (html) vsdrops (raw diff) gist (raw diff) - Please review changes) Pipeline on Agent |
✅ [CI Build #7f9f0c3] Build passed (Build macOS tests) ✅Pipeline on Agent |
❌ [CI Build #7f9f0c3] Tests on macOS X64 - Mac Sonoma (14) failed ❌Failed tests are:
Pipeline on Agent |
❌ [CI Build #7f9f0c3] Tests on macOS M1 - Mac Monterey (12) failed ❌Failed tests are:
Pipeline on Agent |
❌ [CI Build #7f9f0c3] Tests on macOS arm64 - Mac Sequoia (15) failed ❌Failed tests are:
Pipeline on Agent |
❌ [CI Build #7f9f0c3] Tests on macOS M1 - Mac Ventura (13) failed ❌Failed tests are:
Pipeline on Agent |
❌ [CI Build #7f9f0c3] Tests on macOS arm64 - Mac Tahoe (26) failed ❌Failed tests are:
Pipeline on Agent |
🔥 [CI Build #7f9f0c3] Test results 🔥Test results❌ Tests failed on VSTS: test results 0 tests crashed, 40 tests failed, 77 tests passed. Failures❌ fsharp tests1 tests failed, 3 tests passed.Failed tests
Html Report (VSDrops) Download ❌ linker tests1 tests failed, 43 tests passed.Failed tests
Html Report (VSDrops) Download ❌ monotouch tests (iOS)9 tests failed, 0 tests passed.Failed tests
Html Report (VSDrops) Download ❌ monotouch tests (MacCatalyst)11 tests failed, 0 tests passed.Failed tests
Html Report (VSDrops) Download ❌ monotouch tests (macOS)9 tests failed, 0 tests passed.Failed tests
Html Report (VSDrops) Download ❌ monotouch tests (tvOS)9 tests failed, 0 tests passed.Failed tests
Html Report (VSDrops) Download Successes✅ cecil: All 1 tests passed. Html Report (VSDrops) Download Pipeline on Agent |
This is file 40 of 47 files with nullability disabled in Foundation.
Contributes towards #17285.