Repository navigation
fix(safearea): accept static lambdas in CreateWeakHandler debug assert - #1655
Conversation
- Static lambdas compile to instance methods on the cached `<>c` singleton, so `Delegate.Target` is never null and the assert failed for every SafeArea in Debug builds, crashing the app on load - Treat a delegate as non-capturing when its target has no instance fields (closure display classes and bound `this` still fail) - Add `WeakHandler_IsNonCapturing` runtime test covering static, capturing and instance-bound delegates Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01AG4ybou3qxfwwx9oeYgeRC
There was a problem hiding this comment.
Warning
Copilot couldn't run its full agentic review because it didn't start before the timeout. Make sure your repository has a runner available, or add a copilot-code-review.yml file specifying one with the runs-on attribute. See the docs for more details.
Copilot review overview
Review effort: Lite
Findings: 2
Open (3)
Given the new implementation relies on 'target has no instance fields' as a proxy for<>c, add a… · New As implemented,IsNonCapturingwill returntruefor any delegate bound to an instance whose… · New The suppression justification says this code is \"Only evaluated inside Debug.Assert\", but… · New
What changed in this PR
Fixes a Debug-only assertion regression in SafeAreaDetails.CreateWeakHandler so that static (non-capturing) lambdas no longer trip the “Target must be null” check, while still catching capturing lambdas and bound instance delegates.
Changes:
- Replaced
Delegate.Target is nulldebug assertions inCreateWeakHandlerwith a newIsNonCapturing(Delegate)helper that detects compiler-emitted static lambdas (<>c) via “no instance fields”. - Added trimming-warning suppression for reflective field inspection used by the debug assertion.
- Added a runtime test verifying
IsNonCapturingbehavior for static vs capturing vs bound delegates.
| File | Description |
|---|---|
| src/Uno.Toolkit.UI/Controls/SafeArea/SafeArea.cs | Adjusts the Debug.Assert predicate to correctly accept compiler-generated static lambdas while still rejecting closures/bound delegates. |
| src/Uno.Toolkit.RuntimeTests/Tests/SafeAreaTests.cs | Adds a regression test covering static lambda, capturing lambda, and bound instance method detection. |
💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.
|
Azure Static Web Apps: Your stage site is ready! Visit it here: https://polite-field-01aa64f0f-1655.eastus2.azurestaticapps.net |
- A delegate bound to a user type with no instance fields must still count as capturing; require the target type to carry CompilerGeneratedAttribute so only the `<>c` singleton passes - Add that case to WeakHandler_IsNonCapturing - Reword the IL2075 justification: the helper is also called by the runtime test, not only by Debug.Assert Co-Authored-By: Claude Fable 5.1 <[email protected]> Claude-Session: https://claude.ai/code/session_01AG4ybou3qxfwwx9oeYgeRC
|
Azure Static Web Apps: Your stage site is ready! Visit it here: https://polite-field-01aa64f0f-1655.eastus2.azurestaticapps.net |
…t (backport #1655) - Cherry-picked from 9918d84 - Keeps the comment on why Debug is fully qualified: servicing/10.0 still builds native net*-android Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01PZQ75jvGWnf3XhqeenkJ9X
Co-authored-by: Claude Opus 5.5 <[email protected]> Co-authored-by: Xiaotian Gu <[email protected]>


GitHub Issue (If applicable): Regression from the #1602 fix (b2844ea)
PR Type
What is the current behavior?
In Debug builds, every app that loads a
SafeAreacrashes on startup:The assert checks
Delegate.Target is null. But C# compilesstaticlambdas into instance methods on the compiler's cached<>csingleton, soTargetis never null for them. The check fails even though the callers are correct.What is the new behavior?
SafeAreaDetails.IsNonCapturing(Delegate)treats a delegate as non-capturing when it has no target, or when its target has no instance fields.this, still fails the assert as intended.Debug.Assert. The IL2075 trim warning onGetFieldsis suppressed with a justification, following the pattern already used inDependencyObjectExtensions.PR Checklist
SafeAreaTests.WeakHandler_IsNonCapturingOther information
Red/fix/green, run headless on the Material desktop head:
Target is nulllogic,WeakHandler_IsNonCapturingfails withstatic lambda / actual: false.WeakHandler_IsNonCapturingpasses.SafeAreaTestspassed 3 of 3;NestedSafeArea_ApplyCountstays skipped by its existing[Ignore].msbuild Uno.Toolkit.WinUI.csproj -c Releasefor all target frameworks gives 0 warnings and 0 errors.🤖 Generated with Claude Code
https://claude.ai/code/session_01AG4ybou3qxfwwx9oeYgeRC