Skip to content

[bgen] Propagate nullability information for generic type arguments. Fixes #16860#25541

Draft
rolfbjarne wants to merge 8 commits into
mainfrom
dev/rolf/bgen-generic-actions
Draft

[bgen] Propagate nullability information for generic type arguments. Fixes #16860#25541
rolfbjarne wants to merge 8 commits into
mainfrom
dev/rolf/bgen-generic-actions

Conversation

@rolfbjarne

Copy link
Copy Markdown
Member

Summary

When properties have generic delegate types (like Action<NSObject?, NSError?>), bgen now correctly propagates the ? nullability annotations on the generic type arguments in the generated C# code.

Previously, bgen only read the first byte of the [NullableAttribute(byte[])] array (for the outer type's nullability), ignoring the remaining bytes that encode nullability for generic type arguments. This caused properties like Action<UIViewController?, NSError?> to be rendered as Action<UIViewController, NSError> — losing the nullability annotations.

Changes

  • src/bgen/AttributeManager.cs: Added GetNullabilityBytes() method that extracts the full byte array from [NullableAttribute].
  • src/bgen/TypeManager.cs: Added FormatType(Type?, Type?, byte[]?) overload and a recursive FormatTypeUsedIn helper that consumes nullability bytes in depth-first traversal order, correctly skipping value types (which don't have bytes in the array).
  • src/bgen/Generator.cs: Updated property rendering to pass nullability bytes. The "wrap" path applies nullability for all delegate types. The "non-wrap" path only applies for void-returning delegates (Action<>) to avoid Func<> covariance issues with trampoline signatures.
  • tests/bgen/BGenTests.cs: Added GenericTypeNullability test with 10 assertions.
  • tests/bgen/tests/generic-type-nullability.cs: Test input covering nullable args, non-nullable args, value types, many args, mixed patterns, and alternating nullability.

Key design decisions

  1. Value types skip byte consumption: The C# compiler does NOT emit bytes for value types in the NullableAttribute array. The fix correctly identifies value types and skips them during traversal.
  2. Func<> covariance guard: Trampolines use non-nullable generic args. This is fine for Action<in T> (contravariant) but breaks for Func<out T> (covariant). The non-wrap path only applies nullability for void-returning delegates.
  3. Wrap path is unrestricted: The wrap path generates self-contained code without trampoline interaction, so it can safely apply nullability for any delegate type.

Fixes #16860

🤖 Pull request created by Copilot

rolfbjarne and others added 3 commits May 27, 2026 14:16
…ixes #16860.

When a property has a generic type like Action<NSObject?, NSError?>, the
C# compiler emits a [NullableAttribute(byte[])] where each byte encodes
the nullability for the outer type and each generic argument in depth-first
order. Previously, bgen only looked at the first byte (the outer type) and
ignored nullability for generic type arguments.

This change:
- Adds GetNullabilityBytes() to AttributeManager to extract the full byte array
- Adds a FormatType overload in TypeManager that processes the byte array
  to annotate each generic type argument with ? where appropriate
- Uses the new method when rendering property type declarations

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
…onsumption.

- Use braces for all if/else/for blocks in new code.
- Add complex test samples with many generic args, value types, and
  alternating nullability patterns.
- Fix bug where value types incorrectly consumed nullability bytes.
- Only apply nullability for void-returning delegates (Action<>) in the
  non-wrap path to avoid Func<> covariance issues with trampolines.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
…tead.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@rolfbjarne

Copy link
Copy Markdown
Member Author

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 3 pipeline(s).

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@rolfbjarne

Copy link
Copy Markdown
Member Author

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 3 pipeline(s).

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

Copy link
Copy Markdown
Collaborator

✅ [PR Build #c0a4994] Build passed (Build macOS tests) ✅

Pipeline on Agent
Hash: c0a499487d3350fbfcad063f2e0c6f3450b4d45b [PR build]

@vs-mobiletools-engineering-service2

This comment has been minimized.

@rolfbjarne

Copy link
Copy Markdown
Member Author

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 3 pipeline(s).

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@rolfbjarne

Copy link
Copy Markdown
Member Author

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 3 pipeline(s).

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

Copy link
Copy Markdown
Collaborator

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

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

Pipeline on Agent
Hash: c698bc843f73e71832e2dd2df3f34a22417e2574 [PR build]

@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: c698bc843f73e71832e2dd2df3f34a22417e2574 [PR build]

@vs-mobiletools-engineering-service2

Copy link
Copy Markdown
Collaborator

🔥 [CI Build #c698bc8] Test results 🔥

Test results

❌ Tests failed on VSTS: test results

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

Failures

❌ 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 Monterey (12) tests

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

Html Report (VSDrops) Download

❌ Tests on macOS Ventura (13) tests

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

Html Report (VSDrops) Download

❌ Tests on macOS Sonoma (14) tests

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

Html Report (VSDrops) Download

❌ Tests on macOS Sequoia (15) tests

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

Html Report (VSDrops) Download

❌ Tests on macOS Tahoe (26) tests

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

Html Report (VSDrops) Download

Successes

macOS tests

Linux Build Verification

Linux build failed

Pipeline on Agent
Hash: c698bc843f73e71832e2dd2df3f34a22417e2574 [PR build]

@vs-mobiletools-engineering-service2

Copy link
Copy Markdown
Collaborator

🔥 [PR Build #c698bc8] Build failed (Build packages) 🔥

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

Pipeline on Agent
Hash: c698bc843f73e71832e2dd2df3f34a22417e2574 [PR build]

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.

[generator] Add support for nullable annotations in api definitions

2 participants