[XSG] Fix SourceGen OnPlatform default View null handling#34186
[XSG] Fix SourceGen OnPlatform default View null handling#34186StephaneDelcroix merged 3 commits intomainfrom
Conversation
There was a problem hiding this comment.
Pull request overview
Fixes MAUI XAML SourceGen behavior for <OnPlatform x:TypeArguments="View"> when there’s no matching platform and no Default, preventing nullability warnings and runtime null-dereference in generated namescope/source-info code while adding a regression test.
Changes:
- Generate
default!for reference-type OnPlatform placeholder nodes (instead ofdefault) and avoid emitting source-info registration for those placeholders. - Skip namescope/transientNamescope emission for OnPlatform default placeholder nodes.
- Add a regression unit test covering the missing-platform
Viewscenario and validating generated output characteristics.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| src/Controls/tests/SourceGen.UnitTests/InitializeComponent/SimplifyOnPlatform.cs | Adds regression coverage for OnPlatform View missing-platform scenario and asserts generated output/diagnostics expectations. |
| src/Controls/src/SourceGen/Visitors/SetNamescopesAndRegisterNames.cs | Avoids emitting namescope/transientNamescope operations for OnPlatform default placeholder nodes (prevents null deref). |
| src/Controls/src/SourceGen/Visitors/CreateValuesVisitor.cs | Emits default! for reference-type placeholders and skips source-info registration for these placeholder nodes. |
| Assert.DoesNotContain(result.Diagnostics, d => d.Id == "CS8600" || d.Id == "CS8602"); | ||
| Assert.Contains("global::Microsoft.Maui.Controls.View", generated, StringComparison.Ordinal); | ||
| Assert.Contains("default!;", generated, StringComparison.Ordinal); | ||
| Assert.DoesNotContain(".transientNamescope", generated, StringComparison.Ordinal); |
There was a problem hiding this comment.
The new regression test checks for default!; and absence of specific diagnostics (CS8600/CS8602), but it can still pass if other warnings/errors are introduced or if default!; appears for an unrelated reason in generated output. Consider tightening the assertions to (1) fail on any error diagnostics (and optionally any diagnostics beyond the ones you explicitly allow) and (2) match the specific generated assignment for the OnPlatform placeholder (rather than a generic default!; substring) so the test reliably detects regressions in this scenario.
Prevent nullability and null-dereference issues when OnPlatform has no matching platform and no default for reference types. Skip source-info/namescope operations for default placeholder nodes and add regression coverage. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Add Maui34074 Xaml issue test to verify missing-platform defaults and matching-platform behavior are consistent across Runtime, XamlC, and SourceGen inflators. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
3af2253 to
7e9a6c0
Compare
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
<!-- 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! ## Summary - fix SourceGen handling for `<OnPlatform x:TypeArguments="View">` when no matching platform/default exists - avoid nullability and null-dereference codegen by using `default!` for reference-type placeholders - skip namescope/source-info operations for OnPlatform default placeholder nodes - add regression coverage for the View OnPlatform missing-platform scenario ## Related issues - Fixes dotnet#34074 ## Validation - `dotnet test src/Controls/tests/SourceGen.UnitTests/SourceGen.UnitTests.csproj --filter "SimplifyOnPlatform.OnPlatformWithMissingTargetPlatformShouldUseDefault|SimplifyOnPlatform.OnPlatformViewWithMissingTargetPlatformShouldNotEmitNullabilityWarnings"` --------- Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
<!-- 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! ## Summary - fix SourceGen handling for `<OnPlatform x:TypeArguments="View">` when no matching platform/default exists - avoid nullability and null-dereference codegen by using `default!` for reference-type placeholders - skip namescope/source-info operations for OnPlatform default placeholder nodes - add regression coverage for the View OnPlatform missing-platform scenario ## Related issues - Fixes dotnet#34074 ## Validation - `dotnet test src/Controls/tests/SourceGen.UnitTests/SourceGen.UnitTests.csproj --filter "SimplifyOnPlatform.OnPlatformWithMissingTargetPlatformShouldUseDefault|SimplifyOnPlatform.OnPlatformViewWithMissingTargetPlatformShouldNotEmitNullabilityWarnings"` --------- Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
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!
Summary
<OnPlatform x:TypeArguments="View">when no matching platform/default existsdefault!for reference-type placeholdersRelated issues
Validation
dotnet test src/Controls/tests/SourceGen.UnitTests/SourceGen.UnitTests.csproj --filter "SimplifyOnPlatform.OnPlatformWithMissingTargetPlatformShouldUseDefault|SimplifyOnPlatform.OnPlatformViewWithMissingTargetPlatformShouldNotEmitNullabilityWarnings"