Use NotNullWhen(true) instead of MaybeNullWhen(false) for concrete-type Try-methods - #655
Merged
Merged
Conversation
…te-type Try-methods PR #642 annotated all Try* out-parameters with [MaybeNullWhen(false)] on a non-nullable T, which is the correct pattern for unconstrained generic type parameters (as in Dictionary<TKey,TValue>.TryGetValue) but not for concrete reference types. For concrete types, the BCL convention (e.g. Uri.TryCreate) is [NotNullWhen(true)] out T? - this also gives callers a compiler warning if they dereference the out value without checking the return value first, which MaybeNullWhen(false) on non-nullable T silently allows. Updates IHelperResolver, IObjectDescriptorProvider (and implementers), IFormatterProvider (and implementers), TypeExtensions.IsAssignableToGenericType, and BlockAccumulatorContext.IsDetachedClosingElement. Generic-TValue Try-methods (LookupSlim, DictionarySlim, FixedSizeDictionary, CascadeIndex, ObservableIndex) are left as-is since they correctly mirror the BCL generic pattern. Fixes #654
rexm
enabled auto-merge
August 6, 2026 01:27
rexm
disabled auto-merge
August 6, 2026 01:27
rexm
enabled auto-merge
August 6, 2026 01:28
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.



Summary
PR #642 annotated all
Try*out-parameters with[MaybeNullWhen(false)] out T value(non-nullableT), regardless of whetherTwas an unconstrained generic type parameter or a concrete reference type. That pattern is only correct for the generic case (mirroringDictionary<TKey,TValue>.TryGetValue, which can't writeTValue?without aclassconstraint). For concrete reference types, the BCL's own convention (e.g.Uri.TryCreate) is[NotNullWhen(true)] out T?- which also gives a stronger compiler guarantee: dereferencing the out value without checking the return value first now correctly warns, whereasMaybeNullWhen(false)on a non-nullableTsilently allowed it.This is a compile-time-only change (nullable annotations erase to metadata, not IL), so it's not binary breaking, and normal
if (TryX(..., out var x))call sites are unaffected.IHelperResolver.TryResolveHelper/TryResolveBlockHelperIObjectDescriptorProvider.TryGetDescriptorand all implementers (ObjectDescriptorProvider,ObjectDescriptorFactory,LayoutViewModel.DescriptorProvider, the dictionary/enumerable/dynamic descriptor providers)ObjectDescriptor.TryCreate(both overloads)IFormatterProvider.TryCreateFormatterand all implementersTypeExtensions.IsAssignableToGenericTypeBlockAccumulatorContext.IsDetachedClosingElementLeft unchanged: the generic-
TValuecollection Try-methods (LookupSlim,DictionarySlim,FixedSizeDictionary,CascadeIndex,ObservableIndex) - these correctly mirror the BCL's own generic pattern already.Fixes #654
Test plan
dotnet buildacross all target frameworks (netstandard1.3/2.0/2.1, net6.0, net8.0) - 0 errors, no new warnings introduced (verified via clean-rebuild diff against master)dotnet test- 1867/1867 passing