Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Nov 10, 2025

Fix Implemented and Tested - Review Feedback Addressed

Issue Analysis:

  • When binding to source.SomeNullableObject.SomeStringProperty where SomeNullableObject is nullable and SomeStringProperty is a reference type, the generated code produces CS8603 error
  • The bug was in CompiledBindingMarkup.cs in the TryParsePath method (lines 410-422)
  • Lines 410-420 applied nullable annotation, but line 422 immediately overwrote it with previousPartType

Fix Applied:

  • Moved the nullable annotation logic AFTER the propertyType = previousPartType assignment
  • Only apply nullable annotation for reference types (not value types, as they use ?? default fallback)
  • This ensures the TypedBinding signature correctly reflects nullable reference types in the binding path

Review Feedback Addressed:

  • Changed StringProperty from string? to string in test cases (nullability comes from nullable object, not the property)
  • Removed WriteLine debugging statements
  • Converted first test to snapshot test with full expected output
  • Split combined assertions into separate asserts
  • Removed unnecessary blank lines from expected output

Testing:

  • Added test cases for nullable reference types
  • Fixed the bug in CompiledBindingMarkup.cs
  • All SourceGen.UnitTests pass (4/4 CompiledBindings tests)
  • All BindingSourceGen.UnitTests pass (113/113 tests)
  • Ready for final review

Files modified:

  • src/Controls/src/SourceGen/CompiledBindingMarkup.cs - Fixed nullable annotation logic
  • src/Controls/tests/BindingSourceGen.UnitTests/IntegrationTests.cs - Updated test cases per review feedback
  • src/Controls/tests/SourceGen.UnitTests/InitializeComponent/CompiledBindings.cs - Updated test cases per review feedback
Original prompt

This section details on the original issue you should resolve

<issue_title>error CS8603: Possible null reference return in generated code using SourceGen</issue_title>
<issue_description>### Description

cc @simonrozsival, c.f. discussion in MauiVerse Discord

Given the following xaml:

<Entry Text="{Binding SomeNullableObject.SomeStringProperty}"/>

using <MauiXamlInflator>SourceGen</MauiXamlInflator>
this will cause a

error CS8603: Possible null reference return.

in the generated delegate

return Create(
	getter: static source => source.SomeNullableObject?.SomeStringProperty,
	extension.Mode,
	extension.Converter,
	extension.ConverterParameter,
	extension.StringFormat,
	extension.Source,
	extension.FallbackValue,
	extension.TargetNullValue);

I guess this is related to #32402, where something was implemented for value types, not reference types.

I guess it should work for strings specifically, as we often want to bind nullable strings to Entry, Editor, or other wrapped custom controls.

Steps to Reproduce

No response

Link to public reproduction project repository

No response

Version with bug

10.0.0-rc.2

Is this a regression from previous behavior?

No, this is something new

Last version that worked well

No response

Affected platforms

Android, Windows, iOS

Affected platform versions

No response

Did you find any workaround?

No response

Relevant log output

```</issue_description>

<agent_instructions>I believe this fix will be very similar to https://github.com/dotnet/maui/pull/32402. Ideally reuse or generalise some of that code to reduce code complexity.</agent_instructions>

## Comments on the Issue (you are @copilot in this section)

<comments>
</comments>

💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI and others added 2 commits November 10, 2025 14:24
When binding path contains conditional access to a nullable object followed by a reference type property (e.g., source.NullableObject?.StringProperty where StringProperty is string?), the property type now correctly includes nullable annotation. This prevents CS8603 "Possible null reference return" errors in generated binding code.

The fix ensures reference types with conditional access in the path are marked as nullable in the TypedBinding signature, while preserving the existing behavior for value types (which use ?? default fallback).

Fixes issue where XAML bindings like <Entry Text="{Binding SomeNullableObject.SomeStringProperty}"/> would generate code with CS8603 errors when SomeNullableObject is nullable and SomeStringProperty is a nullable reference type.

Co-authored-by: simonrozsival <[email protected]>
Copilot AI changed the title [WIP] Fix error CS8603 for nullable object in SourceGen Fix CS8603 error for nullable reference types in SourceGen binding paths Nov 10, 2025
Copilot AI requested a review from simonrozsival November 10, 2025 14:37
Copy link
Member

@simonrozsival simonrozsival left a comment

Choose a reason for hiding this comment

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

@copilot PTAL

- Changed StringProperty from nullable (string?) to non-nullable (string) in test cases - the nullability comes from NullableObject being nullable, not the property itself
- Removed WriteLine debugging statements from tests
- Converted CorrectlyDetectsNullableReferenceTypes test to a snapshot test with full expected output
- Split combined assertions into separate asserts for better test clarity
- Removed unnecessary blank lines from expected output formatting
- Updated setter expectations to include null check when property is non-nullable

Co-authored-by: simonrozsival <[email protected]>
Copilot AI requested a review from simonrozsival November 10, 2025 15:14
@PureWeen PureWeen added this to the .NET 10.0 SR2 milestone Nov 10, 2025
@simonrozsival simonrozsival marked this pull request as ready for review November 10, 2025 20:28
Copilot AI review requested due to automatic review settings November 10, 2025 20:28
@simonrozsival
Copy link
Member

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 3 pipeline(s).

Copy link
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

This PR fixes CS8603 errors in SourceGen-generated code when binding to nullable reference types through a nullable path (e.g., source.SomeNullableObject?.SomeStringProperty).

Key Changes:

  • Fixed bug in CompiledBindingMarkup.cs where nullable annotation was being overwritten
  • Added comprehensive test coverage for nullable reference types in binding paths
  • Tests verify both the generated code structure and absence of CS8603 errors

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

File Description
src/Controls/src/SourceGen/CompiledBindingMarkup.cs Fixed nullable annotation logic by moving assignment before annotation
src/Controls/tests/SourceGen.UnitTests/InitializeComponent/CompiledBindings.cs Added snapshot test and error verification test for nullable reference types
src/Controls/tests/BindingSourceGen.UnitTests/IntegrationTests.cs Added integration tests for SetBinding and Create with nullable paths

@StephaneDelcroix StephaneDelcroix merged commit 1c7ca4f into main Nov 11, 2025
169 of 170 checks passed
@StephaneDelcroix StephaneDelcroix deleted the copilot/fix-null-reference-error branch November 11, 2025 19:08
@ceubanks14
Copy link

Is this part of the latest .NET release today?

@PureWeen PureWeen added the area-xaml XAML, CSS, Triggers, Behaviors label Dec 18, 2025
evgenygunko pushed a commit to evgenygunko/CopyWordsDA that referenced this pull request Dec 18, 2025
This PR contains the following updates:

| Package | Type | Update | Change |
|---|---|---|---|
| [Microsoft.Maui.Controls](https://github.com/dotnet/maui) | nuget | patch | `10.0.11` -> `10.0.20` |

---

### Release Notes

<details>
<summary>dotnet/maui (Microsoft.Maui.Controls)</summary>

### [`v10.0.20`](https://github.com/dotnet/maui/releases/tag/10.0.20): SR2

[Compare Source](dotnet/maui@10.0.11...10.0.20)

##### What's Changed

**Note:** This is a service release (SR2) tracking ongoing release/10.0.1xx-sr2 branch development. The version number 10.0.20 represents the second service release for .NET MAUI 10.

.NET MAUI 10.0.20 development introduces significant improvements across all platforms with focus on quality, performance, and developer experience. Current development includes enhancements to XAML Source Generator, iOS/macOS fixes, AppThemeBinding improvements, and improved developer tooling.

##### AI

-   PR Reviewer Agent by [@&#8203;PureWeen](https://github.com/PureWeen) in dotnet/maui#32340

-   \[AI] The Issue-Resolver agent by [@&#8203;kubaflo](https://github.com/kubaflo) in dotnet/maui#32804

-   \[AI] Fixed links for agents by [@&#8203;kubaflo](https://github.com/kubaflo) in dotnet/maui#32818

-   Add checkpoint/resume system for PR review agent by [@&#8203;kubaflo](https://github.com/kubaflo) in dotnet/maui#32774

-   Improved pr-reviewer AI agent and modified the usage instructions in README by [@&#8203;kubaflo](https://github.com/kubaflo) in dotnet/maui#32718

-   Agents scripts by [@&#8203;PureWeen](https://github.com/PureWeen) in dotnet/maui#32819

-   Simplify agent instructions and consolidate sandbox testing workflow by [@&#8203;Copilot](https://github.com/Copilot) in dotnet/maui#32950

##### Brushes

-   Fix SolidColorBrush.Equals to compare Color values instead of references by [@&#8203;StephaneDelcroix](https://github.com/StephaneDelcroix) in dotnet/maui#32956

    <details>
    <summary>🔧 Fixes</summary>

    -   [Using DynamicResource with OnPlatform<Color> causes infinite loop](dotnet/maui#27281)

    </details>

##### Data Binding

-   Fix CS8603 error for nullable reference types in SourceGen binding paths by [@&#8203;Copilot](https://github.com/Copilot) in dotnet/maui#32480

    <details>
    <summary>🔧 Fixes</summary>

    -   [error CS8603: Possible null reference return in generated code using SourceGen](dotnet/maui#32478)

    </details>

##### Docs

-   Add README-AI.md file by [@&#8203;PureWeen](https://github.com/PureWeen) in dotnet/maui#32502

-   Improve AI agent documentation and testing instructions by [@&#8203;Copilot](https://github.com/Copilot) in https://github.com/dotnet/maui/p...
@github-actions github-actions bot locked and limited conversation to collaborators Jan 17, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

area-xaml XAML, CSS, Triggers, Behaviors

Projects

None yet

Development

Successfully merging this pull request may close these issues.

error CS8603: Possible null reference return in generated code using SourceGen

5 participants