Skip to content

Add AssociatedSourceType for conditional unmanaged exports - #128180

Merged
MichalStrehovsky merged 14 commits into
mainfrom
copilot/api-proposal-unmanaged-callers-only
Jun 3, 2026
Merged

MichalStrehovsky merged 14 commits into
mainfrom
copilot/api-proposal-unmanaged-callers-only

Conversation

Copilot AI commented May 14, 2026 •

Copy link
Copy Markdown
Contributor

Resolves #127634

UnmanagedCallersOnlyAttribute exports with EntryPoint were always rooted and emitted. This adds an associated source type so NativeAOT can keep/export generated native entrypoints only when the source type survives trimming.

  • Public API
    • Adds UnmanagedCallersOnlyAttribute.AssociatedSourceType to implementation and ref assemblies.
    • Leaves the lightweight Runtime.Base attribute copy unchanged.
[UnmanagedCallersOnly(
    EntryPoint = "MyObjectProxy_MyMethod",
    AssociatedSourceType = typeof(MyObject))]
static void MyMethod(IntPtr handle) { }
  • NativeAOT rooting

    • Moves unmanaged entrypoint rooting into a per-assembly UnmanagedEntryPointsNode.
    • Roots normal exports unconditionally.
    • Roots associated exports conditionally on runtime-constructable AssociatedSourceType.
    • Skips associated exports when the associated source type cannot be resolved.
    • Handles associated source types that can be dynamically constructed at runtime, including generic source types and array types without array templates.
  • Shared trim-target dependency handling

    • Extracts runtime-constructable trim-target dependency logic into RuntimeConstructableTypeDependencies.
    • Reuses the helper from both unmanaged entrypoint rooting and external type map handling.
  • Export generation

    • Filters generated export files to compiled method bodies so trimmed associated exports are not passed to the native linker.
  • Coverage

    • Adds TrimmingBehaviors coverage for unconditional, kept associated, trimmed associated, unused array associated-source, dynamically loadable generic associated-source, dynamically loadable array associated-source, and array associated-source constructed through a runtime generic array template.

Copilot AI requested review from Copilot and removed request for Copilot May 14, 2026 05:19
Copilot AI linked an issue May 14, 2026 that may be closed by this pull request
Copilot AI requested review from Copilot and removed request for Copilot May 14, 2026 05:52
@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @agocke, @dotnet/ilc-contrib
See info in area-owners.md if you want to be subscribed.

Copilot AI requested review from Copilot and removed request for Copilot May 14, 2026 06:06
Copilot AI requested review from Copilot and removed request for Copilot May 14, 2026 06:12
Copilot AI changed the title [WIP] Add AssociatedSourceType to UnmanagedCallersOnlyAttribute Add AssociatedSourceType for conditional unmanaged exports May 14, 2026
Copilot AI requested a review from MichalStrehovsky May 14, 2026 06:14

@MichalStrehovsky MichalStrehovsky left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Copilot AI requested review from Copilot and removed request for Copilot May 14, 2026 08:08
Copilot AI requested review from Copilot and removed request for Copilot May 14, 2026 08:11

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.

Pull request overview

Copilot reviewed 13 out of 13 changed files in this pull request and generated 2 comments.

Co-authored-by: Copilot Autofix powered by AI <[email protected]>
Copilot AI review requested due to automatic review settings May 29, 2026 07:23

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.

Pull request overview

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

@MichalStrehovsky

Copy link
Copy Markdown
Member

@copilot your suggestion at #128180 (comment) caused a build break.

src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/UnmanagedEntryPointsRootProvider.cs(95,33): error CS1626: (NETCORE_ENGINEERING_TELEMETRY=Build) Cannot yield a value in the body of a try block with a catch clause

Copilot AI commented May 29, 2026 •

Copy link
Copy Markdown
Contributor Author

@copilot your suggestion at #128180 (comment) caused a build break.

...

Fixed in d353ccf by moving the yield return outside the try/catch while preserving the unresolved associated-source skip behavior. I also verified ILCompiler.Compiler.csproj builds successfully.

@copilot your suggestion at #128180 (comment) caused a build break.

...

Fixed in d353ccf by moving the yield return outside the try/catch while preserving the unresolved associated-source skip behavior. I also verified ILCompiler.Compiler.csproj builds successfully.

@jtschuster jtschuster left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM aside from a couple questions.

Co-authored-by: Jackson Schuster <[email protected]>
Co-authored-by: Michal Strehovský <[email protected]>
Copilot AI review requested due to automatic review settings June 3, 2026 05:41

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.

Pull request overview

Copilot reviewed 13 out of 13 changed files in this pull request and generated 2 comments.

@MichalStrehovsky

Copy link
Copy Markdown
Member

/ba-g known issue with MissingShebangInFileBasedProgram'

@MichalStrehovsky
MichalStrehovsky enabled auto-merge (squash) June 3, 2026 10:40
@MichalStrehovsky
MichalStrehovsky merged commit 2165f14 into main Jun 3, 2026
168 of 171 checks passed
@MichalStrehovsky
MichalStrehovsky deleted the copilot/api-proposal-unmanaged-callers-only branch June 3, 2026 10:40
@github-actions github-actions Bot locked and limited conversation to collaborators Jul 4, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[API Proposal]: UnmanagedCallersOnlyAttribute.AssociatedSourceType

5 participants