Skip to content

[bgen] Optimize generated object marshaling. Fixes #16675 - #27296

Open
rolfbjarne wants to merge 3 commits into
mainfrom
dev/rolf/optimize-generated-code
Open

rolfbjarne wants to merge 3 commits into
mainfrom
dev/rolf/optimize-generated-code

Conversation

@rolfbjarne

@rolfbjarne rolfbjarne commented Nov 11, 2026 •

Copy link
Copy Markdown
Member

Agent assessment

Self-reported assessment, not an independent review.

Assessed revision: 7db9f1afa19912aa7b1c805178fb117bc36e0b64

Dimension Assessment Rationale
Change confidence 8/10 ↑ Targeted generator regressions pass; the new assertion fails with the old ownership guard.
↑ Earlier two-model review findings were addressed.
↓ Latest tests use installed .NET 10 Apple references; full current-platform validation was not run.
Diagnosis confidence 9.5/10 ↑ The handle-equality guard skipped the runtime call that releases transferred ownership when reusing a wrapper.
Evidence fit 9/10 ↑ The reported review points directly to the owned-result guard now removed for ownership transfers.
↓ No native reference-count reproduction was run.

When the generated code knows the object is an NSObject, use GetNSObject and the NSObject-specific retain/autorelease helpers. Replace repeated null checks around native handles with GetHandle().

For callbacks, don't read the incoming native value of out parameters, and honor [ForcedType] ownership flags for ref parameters. Other native objects and protocol interfaces still use the generic marshaling code.

Fixes #16675

🤖 Pull request created by Copilot

rolfbjarne and others added 2 commits November 11, 2026 10:40
Use GetNSObject for non-forced NSObject ref/out block parameters, and use the NSObject-specific retain/autorelease helper for statically known NSObject parameters and callback results. Keep generic marshaling for native objects, protocol interfaces, and forced-type lookups while preserving ownership and null-pointer handling.

Replace repeated null/Handle conditionals in ref/out marshaling with GetHandle. Add regression coverage for NSError ref/out parameters, bound NSObject types, native objects, protocols, and forced types across all four Apple platforms.

Fixes #16675

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Avoid reading incoming object pointers for out callbacks. Honor forced-type and ownership flags for ref callbacks, including types defined in the binding. Keep protocol interfaces on the generic marshaling path and check both callback directions in the regression test.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot AI balanced review requested due to automatic review settings November 11, 2026 13:32

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

The handle-equality guard skips ownership processing for unchanged owned references, leaking native references that the runtime helper would otherwise release.

1 open issue
What changed in this PR

This PR optimizes bgen’s generated object marshaling for .NET Apple bindings, addressing #16675 while preserving generic handling for other native objects and protocol interfaces.

Changes:

  • Uses NSObject-specific lookup and retain/autorelease helpers where appropriate.
  • Simplifies native-handle access with GetHandle() and updates callback handling for out parameters and forced ownership.
  • Adds regression coverage for object marshaling and generated helper selection.
File Description
tests/​bgen/​tests/​block-object-marshalling.cs Adds callback binding fixtures covering object types and ownership flags.
tests/​bgen/​BGenTests.cs Verifies marshaling helpers, ownership arguments, and null-safe handle access.
src/​bgen/​Generator.cs Optimizes generated marshaling and updates ref/out parameter handling.

Review effort: Balanced

Comment thread src/bgen/Generator.cs Resolved
@vs-mobiletools-engineering-service2

Copy link
Copy Markdown
Collaborator

🔥 [PR Build #96719d3] Build failed (Detect API changes) 🔥

Build failed for the job 'Detect API changes' (with job status 'Failed')

Pipeline on Agent
Hash: 96719d388864ed5f40128503e1aac9c7899bff87 [PR build]

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

Copy link
Copy Markdown
Collaborator

🔥 Unable to find the contents for the comment: D:\a\1\s\change-detection\results\gh-comment.md does not exist :fire

Pipeline on Agent
Hash: 96719d388864ed5f40128503e1aac9c7899bff87 [PR build]

@vs-mobiletools-engineering-service2

Copy link
Copy Markdown
Collaborator

🔥 [CI Build #96719d3] Test results 🔥

Test results

❌ Tests failed on VSTS: test results

29 tests crashed, 0 tests failed, 0 tests passed.

Failures

❌ assembly-processing tests

🔥 Failed catastrophically on VSTS: test results - assembly-processing (no summary found).

Html Report (VSDrops) Download

❌ cecil tests

🔥 Failed catastrophically on VSTS: test results - cecil (no summary found).

Html Report (VSDrops) Download

❌ dotnettests tests (iOS)

🔥 Failed catastrophically on VSTS: test results - dotnettests_ios (no summary found).

Html Report (VSDrops) Download

❌ dotnettests tests (MacCatalyst)

🔥 Failed catastrophically on VSTS: test results - dotnettests_maccatalyst (no summary found).

Html Report (VSDrops) Download

❌ dotnettests tests (macOS)

🔥 Failed catastrophically on VSTS: test results - dotnettests_macos (no summary found).

Html Report (VSDrops) Download

❌ dotnettests tests (Multiple platforms)

🔥 Failed catastrophically on VSTS: test results - dotnettests_multiple (no summary found).

Html Report (VSDrops) Download

❌ dotnettests tests (tvOS)

🔥 Failed catastrophically on VSTS: test results - dotnettests_tvos (no summary found).

Html Report (VSDrops) Download

❌ framework tests

🔥 Failed catastrophically on VSTS: test results - framework (no summary found).

Html Report (VSDrops) Download

❌ fsharp tests

🔥 Failed catastrophically on VSTS: test results - fsharp (no summary found).

Html Report (VSDrops) Download

❌ generator tests

🔥 Failed catastrophically on VSTS: test results - generator (no summary found).

Html Report (VSDrops) Download

❌ interdependent-binding-projects tests

🔥 Failed catastrophically on VSTS: test results - interdependent-binding-projects (no summary found).

Html Report (VSDrops) Download

❌ introspection tests

🔥 Failed catastrophically on VSTS: test results - introspection (no summary found).

Html Report (VSDrops) Download

❌ linker tests (iOS)

🔥 Failed catastrophically on VSTS: test results - linker_ios (no summary found).

Html Report (VSDrops) Download

❌ linker tests (MacCatalyst)

🔥 Failed catastrophically on VSTS: test results - linker_maccatalyst (no summary found).

Html Report (VSDrops) Download

❌ linker tests (macOS)

🔥 Failed catastrophically on VSTS: test results - linker_macos (no summary found).

Html Report (VSDrops) Download

❌ linker tests (tvOS)

🔥 Failed catastrophically on VSTS: test results - linker_tvos (no summary found).

Html Report (VSDrops) Download

❌ monotouch tests (iOS)

🔥 Failed catastrophically on VSTS: test results - monotouch_ios (no summary found).

Html Report (VSDrops) Download

❌ monotouch tests (MacCatalyst)

🔥 Failed catastrophically on VSTS: test results - monotouch_maccatalyst (no summary found).

Html Report (VSDrops) Download

❌ monotouch tests (macOS)

🔥 Failed catastrophically on VSTS: test results - monotouch_macos (no summary found).

Html Report (VSDrops) Download

❌ monotouch tests (tvOS)

🔥 Failed catastrophically on VSTS: test results - monotouch_tvos (no summary found).

Html Report (VSDrops) Download

❌ msbuild tests

🔥 Failed catastrophically on VSTS: test results - msbuild (no summary found).

Html Report (VSDrops) Download

❌ sharpie tests

🔥 Failed catastrophically on VSTS: test results - sharpie (no summary found).

Html Report (VSDrops) Download

❌ windows tests

🔥 Failed catastrophically on VSTS: test results - windows (no summary found).

Html Report (VSDrops) Download

❌ xcframework tests

🔥 Failed catastrophically on VSTS: test results - xcframework (no summary found).

Html Report (VSDrops) Download

❌ xtro tests

🔥 Failed catastrophically on VSTS: test results - xtro (no summary found).

Html Report (VSDrops) Download

❌ Tests on macOS Sonoma (14) tests

⚠️ Tests did not run because the Build macOS tests job failed.

Html Report (VSDrops) Download

❌ Tests on macOS Sequoia (15) tests

⚠️ Tests did not run because the Build macOS tests job failed.

Html Report (VSDrops) Download

❌ Tests on macOS Tahoe (26) tests

⚠️ Tests did not run because the Build macOS tests job failed.

Html Report (VSDrops) Download

❌ Tests on macOS Golden Gate (27) tests

⚠️ Tests did not run because the Build macOS tests job failed.

Html Report (VSDrops) Download

Successes

macOS tests

Linux Build Verification

❌ Linux build failed

Pipeline on Agent
Hash: 96719d388864ed5f40128503e1aac9c7899bff87 [PR build]

@vs-mobiletools-engineering-service2

Copy link
Copy Markdown
Collaborator

🔥 [PR Build #96719d3] Build failed (Build packages) 🔥

Build failed for the job 'Build packages' (with job status 'Failed')

Pipeline on Agent
Hash: 96719d388864ed5f40128503e1aac9c7899bff87 [PR build]

Always pass owned forced-type results through GetINativeObject so it can release the transferred reference when reusing an existing wrapper. Keep the handle comparison for unowned results and check both paths in the block marshaling test.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot AI balanced review requested due to automatic review settings November 30, 2026 11:55

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🔵 Needs a closer look

The cross-binding callback ownership changes still need native Apple-platform lifetime and retain/release validation, particularly for owned results that reuse wrappers.

No open issues 🎉

1 resolved since last review

Review effort: Balanced

@MauiBot

MauiBot commented Nov 30, 2026

Copy link
Copy Markdown
Collaborator

🔥 [PR Build #7db9f1a] Build failed (Build packages) 🔥

Build failed for the job 'Build packages' (with job status 'Failed')

Pipeline on Agent
Hash: 7db9f1afa19912aa7b1c805178fb117bc36e0b64 [PR build]

@MauiBot

MauiBot commented Nov 30, 2026

Copy link
Copy Markdown
Collaborator

🔥 [PR Build #7db9f1a] Build failed (Detect API changes) 🔥

Build failed for the job 'Detect API changes' (with job status 'Failed')

Pipeline on Agent
Hash: 7db9f1afa19912aa7b1c805178fb117bc36e0b64 [PR build]

@MauiBot

MauiBot commented Nov 30, 2026

Copy link
Copy Markdown
Collaborator

🔥 [CI Build #7db9f1a] Test results 🔥

Test results

❌ Tests failed on VSTS: test results

29 tests crashed, 0 tests failed, 0 tests passed.

Failures

❌ assembly-processing tests

🔥 Failed catastrophically on VSTS: test results - assembly-processing (no summary found).

Html Report (VSDrops) Download

❌ cecil tests

🔥 Failed catastrophically on VSTS: test results - cecil (no summary found).

Html Report (VSDrops) Download

❌ dotnettests tests (iOS)

🔥 Failed catastrophically on VSTS: test results - dotnettests_ios (no summary found).

Html Report (VSDrops) Download

❌ dotnettests tests (MacCatalyst)

🔥 Failed catastrophically on VSTS: test results - dotnettests_maccatalyst (no summary found).

Html Report (VSDrops) Download

❌ dotnettests tests (macOS)

🔥 Failed catastrophically on VSTS: test results - dotnettests_macos (no summary found).

Html Report (VSDrops) Download

❌ dotnettests tests (Multiple platforms)

🔥 Failed catastrophically on VSTS: test results - dotnettests_multiple (no summary found).

Html Report (VSDrops) Download

❌ dotnettests tests (tvOS)

🔥 Failed catastrophically on VSTS: test results - dotnettests_tvos (no summary found).

Html Report (VSDrops) Download

❌ framework tests

🔥 Failed catastrophically on VSTS: test results - framework (no summary found).

Html Report (VSDrops) Download

❌ fsharp tests

🔥 Failed catastrophically on VSTS: test results - fsharp (no summary found).

Html Report (VSDrops) Download

❌ generator tests

🔥 Failed catastrophically on VSTS: test results - generator (no summary found).

Html Report (VSDrops) Download

❌ interdependent-binding-projects tests

🔥 Failed catastrophically on VSTS: test results - interdependent-binding-projects (no summary found).

Html Report (VSDrops) Download

❌ introspection tests

🔥 Failed catastrophically on VSTS: test results - introspection (no summary found).

Html Report (VSDrops) Download

❌ linker tests (iOS)

🔥 Failed catastrophically on VSTS: test results - linker_ios (no summary found).

Html Report (VSDrops) Download

❌ linker tests (MacCatalyst)

🔥 Failed catastrophically on VSTS: test results - linker_maccatalyst (no summary found).

Html Report (VSDrops) Download

❌ linker tests (macOS)

🔥 Failed catastrophically on VSTS: test results - linker_macos (no summary found).

Html Report (VSDrops) Download

❌ linker tests (tvOS)

🔥 Failed catastrophically on VSTS: test results - linker_tvos (no summary found).

Html Report (VSDrops) Download

❌ monotouch tests (iOS)

🔥 Failed catastrophically on VSTS: test results - monotouch_ios (no summary found).

Html Report (VSDrops) Download

❌ monotouch tests (MacCatalyst)

🔥 Failed catastrophically on VSTS: test results - monotouch_maccatalyst (no summary found).

Html Report (VSDrops) Download

❌ monotouch tests (macOS)

🔥 Failed catastrophically on VSTS: test results - monotouch_macos (no summary found).

Html Report (VSDrops) Download

❌ monotouch tests (tvOS)

🔥 Failed catastrophically on VSTS: test results - monotouch_tvos (no summary found).

Html Report (VSDrops) Download

❌ msbuild tests

🔥 Failed catastrophically on VSTS: test results - msbuild (no summary found).

Html Report (VSDrops) Download

❌ sharpie tests

🔥 Failed catastrophically on VSTS: test results - sharpie (no summary found).

Html Report (VSDrops) Download

❌ windows tests

🔥 Failed catastrophically on VSTS: test results - windows (no summary found).

Html Report (VSDrops) Download

❌ xcframework tests

🔥 Failed catastrophically on VSTS: test results - xcframework (no summary found).

Html Report (VSDrops) Download

❌ xtro tests

🔥 Failed catastrophically on VSTS: test results - xtro (no summary found).

Html Report (VSDrops) Download

❌ Tests on macOS Sonoma (14) tests

⚠️ Tests did not run because the Build macOS tests job failed.

Html Report (VSDrops) Download

❌ Tests on macOS Sequoia (15) tests

⚠️ Tests did not run because the Build macOS tests job failed.

Html Report (VSDrops) Download

❌ Tests on macOS Tahoe (26) tests

⚠️ Tests did not run because the Build macOS tests job failed.

Html Report (VSDrops) Download

❌ Tests on macOS Golden Gate (27) tests

⚠️ Tests did not run because the Build macOS tests job failed.

Html Report (VSDrops) Download

Successes

macOS tests

Linux Build Verification

❌ Linux build failed

Pipeline on Agent
Hash: 7db9f1afa19912aa7b1c805178fb117bc36e0b64 [PR build]

@MauiBot

MauiBot commented Nov 30, 2026

Copy link
Copy Markdown
Collaborator

🔥 Unable to find the contents for the comment: D:\a\1\s\change-detection\results\gh-comment.md does not exist :fire

Pipeline on Agent
Hash: 7db9f1afa19912aa7b1c805178fb117bc36e0b64 [PR build]

@MauiBot

MauiBot commented Nov 30, 2026

Copy link
Copy Markdown
Collaborator

🔥 [PR Build #7db9f1a] Build failed (Build packages) 🔥

Build failed for the job 'Build packages' (with job status 'Failed')

Pipeline on Agent
Hash: 7db9f1afa19912aa7b1c805178fb117bc36e0b64 [PR build]

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Optimize generated code

4 participants