Skip to content

[XSG] Fix SourceGen OnPlatform default View null handling#34186

Merged
StephaneDelcroix merged 3 commits intomainfrom
dev/stdelc/issue34074-onplatform-null-default
Feb 25, 2026
Merged

[XSG] Fix SourceGen OnPlatform default View null handling#34186
StephaneDelcroix merged 3 commits intomainfrom
dev/stdelc/issue34074-onplatform-null-default

Conversation

@StephaneDelcroix
Copy link
Copy Markdown
Contributor

@StephaneDelcroix StephaneDelcroix commented Feb 23, 2026

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

  • 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

Validation

  • dotnet test src/Controls/tests/SourceGen.UnitTests/SourceGen.UnitTests.csproj --filter "SimplifyOnPlatform.OnPlatformWithMissingTargetPlatformShouldUseDefault|SimplifyOnPlatform.OnPlatformViewWithMissingTargetPlatformShouldNotEmitNullabilityWarnings"

Copilot AI review requested due to automatic review settings February 23, 2026 11:01
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 of default) 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 View scenario 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.

Comment on lines +433 to +436
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);
Copy link

Copilot AI Feb 23, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
@StephaneDelcroix StephaneDelcroix changed the title Fix SourceGen OnPlatform default View null handling [XSG] Fix SourceGen OnPlatform default View null handling Feb 24, 2026
StephaneDelcroix and others added 2 commits February 24, 2026 14:49
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>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@StephaneDelcroix StephaneDelcroix merged commit bd82423 into main Feb 25, 2026
24 of 27 checks passed
@StephaneDelcroix StephaneDelcroix deleted the dev/stdelc/issue34074-onplatform-null-default branch February 25, 2026 13:48
jfversluis pushed a commit to yuriikyry4enko/maui that referenced this pull request Feb 26, 2026
<!-- 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>
TamilarasanSF4853 pushed a commit to TamilarasanSF4853/maui that referenced this pull request Mar 2, 2026
<!-- 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>
@github-actions github-actions bot locked and limited conversation to collaborators Mar 28, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

NullReferenceException thrown at runtime when using OnPlatform with SourceGen enabled

3 participants