[XSG] Generate unwrapping for compiled bindings with conditional access to non-nullable value types#32402
Conversation
There was a problem hiding this comment.
Pull Request Overview
This PR enhances the compiled bindings source generator to correctly handle nullable types in binding paths. When a binding path contains nullable intermediate types leading to non-nullable value types, the generator now produces code that properly handles null propagation with fallback to TargetNullValue or default.
Key Changes
- Modified getter lambda generation to detect conditional access patterns and non-nullable value type targets
- Added null-coalescing fallback logic to unwrap nullable value types when the binding path contains nullable references
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
src/Controls/src/SourceGen/CompiledBindingMarkup.cs |
Enhanced GenerateGetterLambda to detect conditional access and generate appropriate null-coalescing expressions for non-nullable value types |
src/Controls/tests/SourceGen.UnitTests/InitializeComponent/CompiledBindings.cs |
Added comprehensive test case verifying correct code generation for nullable reference types leading to non-nullable value types |
src/Controls/tests/SourceGen.UnitTests/InitializeComponent/CompiledBindings.cs
Outdated
Show resolved
Hide resolved
src/Controls/tests/SourceGen.UnitTests/InitializeComponent/CompiledBindings.cs
Outdated
Show resolved
Hide resolved
…itional-access-to-non-nullable-value-types
|
@simonrozsival related question: |
|
/azp run MAUI-UITests-public |
|
Azure Pipelines successfully started running 1 pipeline(s). |
@dainius-r with indexers it's going to be a bit more tricky. I would suggest doing this a bit more manually and defining a new property in the view model: public Product FirstProduct => Product.Length > 0 ? Product[0] : someFallbackValue;Now based on your sample it doesn't seem you publish any |
Description of Change
Consider the following scenario:
The property accessed by the binding is
Sizewhich is anint. TheProductproperty on theTestPageis nullable though. The getter expression looks like this:This is a problem, as the conditional access to the
Productvalue changes the property type frominttoint?. There were two options I was considering:inttoint?TargetNullValue(if available) or the default valueI chose the second, since the first option would not use
TargetNullValueat all. Feedback is welcome.Issues Fixed
Contributes to #32398