Skip to content

Fix CS8602 nullable dereference in ToolbarBadgePage#34963

Merged
jfversluis merged 2 commits intonet11.0from
fix/toolbar-badge-nullable
Apr 15, 2026
Merged

Fix CS8602 nullable dereference in ToolbarBadgePage#34963
jfversluis merged 2 commits intonet11.0from
fix/toolbar-badge-nullable

Conversation

@PureWeen
Copy link
Copy Markdown
Member

Summary

Fix nullable dereference warnings (CS8602) in ToolbarBadgePage.cs that break Samples integration tests on both Windows and macOS.

Root Cause

PR #34669 ("Add BadgeText, BadgeColor, and BadgeTextColor support to ToolbarItem") introduced ToolbarBadgePage.cs where _statusLabel is initialized on line 68 but captured in lambda closures on lines 33, 46, and 60 — before the assignment. The compiler cannot prove non-null at the capture points, emitting CS8602 on all 3 lines across all TFMs.

Fix

Move _statusLabel initialization before the lambda closures that reference it. No behavioral change — the label object is the same, just created earlier in the constructor.

Validation

  • dotnet build Maui.Controls.Sample.csproj -f net11.0 -p:TreatWarningsAsErrors=true — 0 warnings, 0 errors

Copilot AI review requested due to automatic review settings April 14, 2026 19:03
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Apr 14, 2026

🚀 Dogfood this PR with:

⚠️ WARNING: Do not do this without first carefully reviewing the code of this PR to satisfy yourself it is safe.

curl -fsSL https://github.com/dotnet/maui/main/eng/scripts/get-maui-pr.sh | bash -s -- 34963

Or

  • Run remotely in PowerShell:
iex "& { $(irm https://github.com/dotnet/maui/main/eng/scripts/get-maui-pr.ps1) } 34963"

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 CS8602 nullable dereference warnings in ToolbarBadgePage by ensuring _statusLabel is initialized before it’s captured by toolbar item click handlers, restoring warning-free builds with TreatWarningsAsErrors=true.

Changes:

  • Move _statusLabel initialization earlier in the constructor so lambda closures can safely reference it.
  • Remove the later duplicate _statusLabel initialization block.

@PureWeen
Copy link
Copy Markdown
Member Author

/azp run maui-pr-uitests, maui-pr-devicetests

@azure-pipelines
Copy link
Copy Markdown

Azure Pipelines successfully started running 2 pipeline(s).

@PureWeen
Copy link
Copy Markdown
Member Author

CI Failure Analysis

All CI failures on this PR are pre-existing on net11.0 and unrelated to our nullable fixes. Here's the full breakdown.

Failures Fixed by This PR

Error File Root Cause
CS8602 (lines 33, 46, 60) ToolbarBadgePage.cs _statusLabel captured in lambdas before initialization
CS8602 (lines 88, 94) UINSWindow.iOS.cs PerformSelector/GetItem<T> returns now nullable in macios SDK 26.2.11587
CS8602, CS8619 (lines 250, 258) AssertionExtensions.iOS.cs GetCurrentContext()/GetImageFromCurrentImageContext() now nullable
CS8622 (lines 87, 95) MauiTestApplicationDelegate.cs Lambda sender parameter nullability mismatch

Validation: Full solution build (Microsoft.Maui-mac.slnf) with TreatWarningsAsErrors=true passes with 0 errors locally. Windows Release build passed in CI. macOS build shows 0 CS errors (only fails on actool infra issue below).

Pre-existing CI Failures (Not Related to This PR)

1. WinUI UITests — All categories, 0/2500+ tests passing (app crash)

Build Date Source WinUI Results
1369882 Apr 8 c43df61b (before #34669) 2,668 passed, 1 failed
1371858 Apr 9 53be2cd6 (#34669 merged) 0 passed, 5,020 failed

2. macOS Debug Build — actool/ibtool failures

  • Error: iOS 26.2 Platform Not Installed / No simulator runtime version from ["22F77"] available to use with iphonesimulator SDK version 23C53
  • CI macOS agents have Xcode 26.2 but are missing the iOS 26.2 simulator runtime
  • Agent image was updated between Apr 7-14 (added Xcode 26.3/26.4), likely disrupting the iOS 26.2 runtime
  • Infrastructure issue — not fixable from code

3. AOT macOS — strip/dsymutil failures

  • Error: MauiPlatformInterop.framework/MauiPlatformInterop: No such file or directory
  • Known macios SDK bug — framework binary not produced during NativeAOT builds
  • Same failure on net11.0 baseline build 1376548

4. Windows Debug Build — File locking flake

  • MSB3026: Could not copy DLLs during build (file locked by another process)
  • Transient — passed on retry in previous builds

@PureWeen
Copy link
Copy Markdown
Member Author

/rebase

github-actions Bot and others added 2 commits April 15, 2026 00:31
Move _statusLabel initialization before the lambda closures that
capture it, so the compiler can prove non-null at the dereference
sites (lines 33, 46, 60).

Fixes Samples windows and Samples macOS integration test failures
introduced by #34669.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
- UINSWindow.iOS.cs: Add null checks for PerformSelector and GetItem
  returns that are now nullable in macios SDK 26.2.11587
- AssertionExtensions.iOS.cs: Guard CGContext null from GetCurrentContext,
  use null-conditional on UIImage.Size
- MauiTestApplicationDelegate.cs: Fix lambda sender parameter nullability
  to match updated MarshalManagedExceptionHandler/MarshalObjectiveCExceptionHandler
  delegate signatures

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@jfversluis jfversluis merged commit 4e6b863 into net11.0 Apr 15, 2026
13 of 34 checks passed
@jfversluis jfversluis deleted the fix/toolbar-badge-nullable branch April 15, 2026 07:29
jfversluis added a commit that referenced this pull request Apr 15, 2026
jfversluis added a commit that referenced this pull request Apr 16, 2026
## Problem

PR #34669 (ToolbarItem Badge Support) added Issue8305_Toolbar.cs with
`[Issue(IssueTracker.Github, 8305, ...)]`, but Issue8305.cs (Shell
Badge, from PR #34659) already uses issue number 8305. Both generated
the same `DisplayName = "G8305"`, causing `VerifyNoDuplicates()` in
`TestCases.cs` to throw `NotSupportedException` at app startup, crashing
the **entire HostApp** and failing **ALL** Windows UI tests (not just
toolbar tests).

This crash has been present since build
[1371858](https://dev.azure.com/dnceng-public/public/_build/results?buildId=1371858)
(PR #34669 merge). The follow-up PR #34963 fixed nullable warnings but
did not address this crash.

## Root Cause

`TestCases.cs:VerifyNoDuplicates()` (line 161-175) uses a HashSet to
check for duplicate `DisplayName` values. When two `[Issue]` attributes
share the same tracker and number with default `issueTestNumber=0`, they
produce identical display names, triggering the crash.

## Fix

1. **Add `issueTestNumber: 1`** to `Issue8305_Toolbar`'s `[Issue]`
attribute, producing unique `DisplayName = "G8305 (1)"` instead of
duplicate `"G8305"`
2. **Replace non-existent icon images** (`envelope.png`, `bell.png`,
`cart.png`) with images that exist in HostApp resources (`bank.png`,
`calculator.png`, `shopping_cart.png`)

## Verification

- Both `Controls.TestCases.HostApp` (Windows TFM) and
`Controls.TestCases.Shared.Tests` build successfully
- The `issueTestNumber` pattern is well-established (used by Issue11769,
Issue13126_2, Issue1583_1, Issue28986_Border, and others)

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants