Commit 8fe90ca
authored
Fix OnPlatform SourceGen for abstract types and protected constructors (dotnet#33214)
<!-- Please let the below note in for people that find this PR -->
> [!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](https://github.com/dotnet/maui/wiki/Testing-PR-Builds) from
this PR and let us know in a comment if this change resolves your issue.
Thank you!
## Description
Fixes two XAML SourceGen issues reported:
1. **"Cannot create an instance of the abstract type or interface
'Brush'"** - When `OnPlatform<Brush>` was used with
`<OnPlatform.Default>` syntax and no matching platform.
2. **"error CS0122: 'View.View()' is inaccessible due to its protection
level"** - When `OnPlatform<View>` was used and no matching platform.
### Example XAML that was failing:
```xml
<OnPlatform x:Key="RadEntryInvalidBorderBrush" x:TypeArguments="Brush">
<On Platform="WinUI">
<LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1">
<LinearGradientBrush.GradientStops>
<GradientStop Offset="0.00" Color="{StaticResource RadOnAppSurfaceColorAlpha6}" />
<GradientStop Offset="0.93" Color="{StaticResource RadOnAppSurfaceColorAlpha6}" />
<GradientStop Offset="0.93" Color="{StaticResource RadErrorColor}" />
<GradientStop Offset="1.00" Color="{StaticResource RadErrorColor}" />
</LinearGradientBrush.GradientStops>
</LinearGradientBrush>
</On>
<OnPlatform.Default>
<SolidColorBrush Color="{StaticResource RadErrorColor}" />
</OnPlatform.Default>
</OnPlatform>
<OnPlatform x:TypeArguments="View">
<On Platform="WinUI">
<telerikMauiControls:RadBorder Grid.Row="2"
BackgroundColor="{DynamicResource RadAIPromptInputViewFooterBackgroundColor}" />
</On>
</OnPlatform>
```
## Root Cause
Two issues in `SimplifyOnPlatformVisitor.cs`:
1. `GetDefault()` only checked for `Default` property with empty
namespace (`new XmlName("", "Default")`), but when using
`<OnPlatform.Default>` element syntax, the property is stored with the
MAUI namespace URI.
2. When no matching platform and no Default was found, the code created
an `ElementNode` from the `x:TypeArguments` type, which later failed
when `CreateValuesVisitor` tried to instantiate abstract types or types
with protected constructors.
## Fix
1. Updated `GetDefault()` to check both empty namespace (attribute
syntax) and MAUI namespace (element syntax `<OnPlatform.Default>`)
2. When no matching platform AND no Default is found, skip
simplification and keep the `OnPlatform` element for runtime resolution
instead of trying to create a default value. This is the safe approach
because the runtime `OnPlatform<T>` correctly returns `default(T)`.
## Tests Added
Added 6 new tests in `OnPlatformAbstractTypes.cs` covering:
- `OnPlatformWithAbstractBrushTypeUsesDefault` - Brush with Default on
non-matching platform
- `OnPlatformWithAbstractBrushTypeMatchesPlatform` - Brush when platform
matches
- `OnPlatformWithViewTypeUsesDefault` - View with Default on
non-matching platform
- `OnPlatformWithViewTypeMatchesPlatform` - View when platform matches
- `OnPlatformWithAbstractTypeNoDefaultNoMatchingPlatform` - Brush
without Default on non-matching platform
- `OnPlatformWithProtectedCtorTypeNoDefaultNoMatchingPlatform` - View
without Default on non-matching platform1 parent 80b6844 commit 8fe90ca
File tree
11 files changed
+574
-10
lines changed- src/Controls
- src
- SourceGen/Visitors
- Xaml
- tests
- SourceGen.UnitTests/InitializeComponent
- Xaml.UnitTests/Issues
11 files changed
+574
-10
lines changedLines changed: 11 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
38 | 38 | | |
39 | 39 | | |
40 | 40 | | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
41 | 52 | | |
42 | 53 | | |
43 | 54 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
87 | 87 | | |
88 | 88 | | |
89 | 89 | | |
90 | | - | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
91 | 94 | | |
92 | 95 | | |
93 | | - | |
94 | 96 | | |
95 | | - | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
96 | 101 | | |
97 | 102 | | |
98 | 103 | | |
| |||
190 | 195 | | |
191 | 196 | | |
192 | 197 | | |
| 198 | + | |
193 | 199 | | |
194 | 200 | | |
| 201 | + | |
| 202 | + | |
195 | 203 | | |
196 | 204 | | |
197 | 205 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
110 | 110 | | |
111 | 111 | | |
112 | 112 | | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
113 | 120 | | |
114 | 121 | | |
115 | 122 | | |
| |||
150 | 157 | | |
151 | 158 | | |
152 | 159 | | |
153 | | - | |
| 160 | + | |
| 161 | + | |
154 | 162 | | |
155 | 163 | | |
156 | 164 | | |
| |||
0 commit comments