-
Notifications
You must be signed in to change notification settings - Fork 1.9k
[XSG] Fix CS8601 error in XAML SourceGen setter for nullable reference types #32940
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
[XSG] Fix CS8601 error in XAML SourceGen setter for nullable reference types #32940
Conversation
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 pull request fixes CS8601 compiler errors that occur when XAML SourceGen generates setters for bindings with nullable access paths targeting non-nullable properties. The fix adds null checks in the generated setter code to prevent assigning potentially null values to non-nullable properties.
Key Changes:
- Added early return logic in
AppendSetterLambdafor nullable values when the target property doesn't accept nulls - Handles both nullable reference types (with
is nullcheck) and nullable value types (withHasValuecheck and.Valueunwrapping) - Updated test expectations to reflect the new null-check behavior
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| src/Controls/src/SourceGen/CompiledBindingMarkup.cs | Implements null-checking logic in setter generation that mirrors the existing implementation in BindingCodeWriter.cs for C# binding interceptors |
| src/Controls/tests/SourceGen.UnitTests/InitializeComponent/CompiledBindings.cs | Updates test expectations to include the new null-check in generated setters for nullable reference type bindings |
| if (value is null) | ||
| { | ||
| return; | ||
| } |
Copilot
AI
Dec 1, 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 only covers nullable reference types. Consider adding a test case for nullable value types (e.g., int?, DateTime?) to ensure the HasValue check and .Value unwrapping logic works correctly. For example, a binding path like Product?.Quantity where Product is nullable and Quantity is int?, being bound to a non-nullable int property.
When binding path contains nullable access (e.g., Child.Message where Child is nullable), the generated setter was assigning a nullable value to a non-nullable property, causing CS8601 'Possible null reference assignment'. The fix adds an early return for null values in the setter when: - PropertyType.IsNullable is true (the binding path involves nullable access) - SetterOptions.AcceptsNullValue is false (the target property doesn't accept null) This mirrors the existing logic in BindingCodeWriter.cs used for C# binding interceptors. Fixes #32893
3b044fc to
bdb47d6
Compare
simonrozsival
left a comment
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.
I'd like to see more test coverage:
- a scenario with a value type that's at the end of an expression with conditional access in the middle
- a scenario with a non-nullable reference type that's at the end of an expression with conditional access in the middle
- a scenario with a nullable reference type that's at the end of an expression with conditional access in the middle -- should not contain the early return
The test verifies that binding to non-nullable value type properties through nullable paths works correctly. In XAML SourceGen, value types accessed through nullable paths remain non-nullable (using ?? default fallback in getter) rather than becoming Nullable<T>, so the setter uses pattern matching instead of HasValue checks.
|
@copilot I'd like to see more test coverage:
What other scenarios would be helpful to allow us to prevent future regressions with nullable types in compiled bindings? |
|
@simonrozsival I've opened a new pull request, #32951, to work on those changes. Once the pull request is ready, I'll request review from you. |
…al access paths (#32951) * Initial plan * Add comprehensive nullable type tests for conditional access paths Co-authored-by: simonrozsival <374616+simonrozsival@users.noreply.github.com> * Refactor test assertions to use regex matching and full expressions Co-authored-by: simonrozsival <374616+simonrozsival@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: simonrozsival <374616+simonrozsival@users.noreply.github.com>
|
/azp run MAUI-UITests-public |
|
Azure Pipelines successfully started running 1 pipeline(s). |
Note
Are you waiting for the changes in this PR to be merged?
It would be very helpful if you could test the resulting artifacts from this PR and let us know in a comment if this change resolves your issue. Thank you!
Description
When binding path contains nullable access (e.g.,
Child.MessagewhereChildis nullable), the XAML SourceGen was generating a setter that assigns a nullable value to a non-nullable property, causing CS8601 "Possible null reference assignment" error.Before (generated code):
After (generated code):
Changes
AppendSetterLambdawhen:PropertyType.IsNullableis true (the binding path involves nullable access)SetterOptions.AcceptsNullValueis false (the target property does not accept null)BindingCodeWriter.csused for C# binding interceptorsCorrectlyDetectsNullableReferenceTypestest to expect the new null-checkReproduction
The issue was reproduced using the test project from this comment.