feat: honor null-accepting user-defined conversion operators#2371
Open
fdipuma wants to merge 4 commits into
Open
feat: honor null-accepting user-defined conversion operators#2371fdipuma wants to merge 4 commits into
fdipuma wants to merge 4 commits into
Conversation
…evel and member mappings
…ors; add coverage Rebuild the null-accepting-operator cast only after the normal pipeline has already selected a CastMapping for the nullable source, so higher-priority conversions (Parse, ctor, enumerable, ...) keep precedence over the null-accepting operator shortcut. Adds regression coverage for the priority collision plus nested-member, implicit-operator, and value-type-guard cases, and documents the RMG089 suppression for nullable member -> non-nullable target member via a null-accepting operator.
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.
Description
Implements parts 1–2 of #2317: Mapperly now honors a user-defined conversion operator's nullable reference type annotations, consistent with how NRT annotations are trusted elsewhere.
When a nullable source is converted through a user-defined operator whose parameter is nullable (e.g.
explicit operator string?(Code? c)), the operator can acceptnulldirectly, so no null guard is generated:An operator whose parameter is non-nullable keeps the guard, so it is never invoked with
null:This applies to object member/property mappings, nested members, and top-level mapper-method signatures.
How it works
A detector (
MappingBuilderContext.HasNullAcceptingUserDefinedConversion) checks the operator's parameter annotation viaCompilation.ClassifyConversion. At the two points where source nullability is normally stripped (FindOrBuildLooseNullableMappingfor members,NullableMappingBuilderfor top-level), the source nullability is preserved for such conversions so the resultingCastMappingkeeps a nullable source type, and the existing null-handling logic then skips the guard. No guard-decision code was changed. Builder priority is preserved: the guard is only dropped when a cast is the conversion Mapperly would have selected anyway (a higher-priorityParse/constructor/etc. still wins for a nullable source).Notes
QueryableProjectionNullHandlingprojection change, part 3 of Annotation-driven null-guard strategy for user-defined conversions (with a configurable override) #2317), no shared files; can be reviewed/merged in any order.RMG089diagnostic is not reported (the rebuilt cast satisfies the same exemptionRMG090uses); behavior is pinned by a test. Happy to adjust if you'd prefer a diagnostic there.Checklist