Skip to content

LibraryImportGenerator: prep for unsafe-v2 - #131041

Merged
EgorBo merged 3 commits into
dotnet:mainfrom
EgorBo:libimport-gen-unsafe-block
Jul 21, 2026
Merged

EgorBo merged 3 commits into
dotnet:mainfrom
EgorBo:libimport-gen-unsafe-block

Conversation

@EgorBo

@EgorBo EgorBo commented Jul 19, 2026 •

Copy link
Copy Markdown
Member

Prep for unsafe-v2 where unsafe on types is illegal and unsafe on methods no longer opens an unsafe context in the body. Emit code valid under both old and new rules:

  • Don't add unsafe to the generated stub's containing type (user's own modifiers are still copied as-is).
  • Wrap wrapper-stub bodies in an unsafe { } block instead.
  • Forwarder stubs (bodyless extern) are unchanged.

The exact behavior of LIG in unsafe-v2 context is currently blocked by dotnet/roslyn#84555 decision.

…containing type

The LibraryImport source generator (and its downlevel variant) previously
added an `unsafe` modifier to the generated partial method's containing type
to provide an unsafe context for both the stub signature and body.

In preparation for the C# "unsafe evolution" feature
(https://github.com/dotnet/csharplang/blob/main/proposals/unsafe-evolution.md),
where `unsafe` on a type becomes illegal and `unsafe` on a method no longer
opens an unsafe context in its body, change the generator to emit code that is
valid under both the current and the updated memory-safety rules:

- Stop adding an `unsafe` modifier to the containing type. The user's own type
  modifiers are still copied verbatim, so a user-authored `unsafe partial class`
  is preserved.
- Wrap the body of generated wrapper stubs in an explicit `unsafe { }` block.
  Forwarder stubs (bodyless `extern` declarations) get no unsafe block and rely
  on the copied user modifiers.

`WrapMemberInContainingSyntaxWithUnsafeModifier` is renamed to
`WrapMemberInContainingSyntax` accordingly.

This is preparatory only; emitting different code specifically for the updated
rules (e.g. `unsafe`/`safe` on `extern` forwarders) is left for follow-up work
that depends on dotnet/roslyn#84555.

Co-authored-by: Copilot <[email protected]>
Copilot-Session: a6c2ca1e-3757-422c-ab22-9d9d7ea3ad10
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 3 pipeline(s).
13 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

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

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

This PR updates the LibraryImportGenerator (and downlevel variant) to stop emitting unsafe on containing type declarations and instead emit an explicit unsafe { ... } block inside wrapper stub bodies, keeping the generated code shape compatible with evolving C# unsafe semantics. It also adds/adjusts unit tests to lock in the new emitted structure and updates an existing diagnostic expectation accordingly.

Changes:

  • Wrap generated wrapper stub bodies in a single unsafe { } statement block (instead of relying on unsafe on the containing type).
  • Update ContainingSyntaxContext to wrap members in containing syntax without injecting an unsafe type modifier; rename the helper accordingly.
  • Add UnsafeCodeGeneration structural tests and update CompileFails.ValidateRequireAllowUnsafeBlocksDiagnostic expectations.

Reviewed changes

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

Show a summary per file
File Description
src/libraries/System.Runtime.InteropServices/tests/LibraryImportGenerator.UnitTests/UnsafeCodeGeneration.cs Adds structural unit tests verifying wrapper stubs use a body unsafe block and that the generator does not inject unsafe onto containing types (while preserving user-authored unsafe).
src/libraries/System.Runtime.InteropServices/tests/LibraryImportGenerator.UnitTests/CompileFails.cs Updates expectations/comments for AllowUnsafeBlocks=false now that trivial forwarder stubs don’t trigger CS0227 via generated type-level unsafe.
src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/ContainingSyntaxContext.cs Renames/remodels member wrapping to avoid adding unsafe to containing types when emitting syntax-wrapped members.
src/libraries/System.Runtime.InteropServices/gen/LibraryImportGenerator/LibraryImportGenerator.cs Wraps wrapper stub bodies in unsafe { } and switches to the new containing-syntax wrapper API.
src/libraries/System.Runtime.InteropServices/gen/DownlevelLibraryImportGenerator/DownlevelLibraryImportGenerator.cs Mirrors the wrapper-stub unsafe { } body emission and containing-syntax wrapper API change for downlevel generation.

@EgorBo EgorBo changed the title Emit an unsafe block in LibraryImport stubs instead of unsafe on the containing type LibraryImportGenerator: prep for unsafe-v2 Jul 19, 2026
@github-actions

This comment was marked as spam.

github-actions[bot]

This comment was marked as spam.

@EgorBo

EgorBo commented Jul 20, 2026

Copy link
Copy Markdown
Member Author

PTAL @AaronRobinsonMSFT (assuming @jkoritzinsky is OOO)

@EgorBo
EgorBo requested a review from AaronRobinsonMSFT July 20, 2026 14:56

@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.

Could we also add test cases for forwarding stubs and marshalling stubs with 'unsafe' on the method declaration?

public static unsafe partial void Method();

public static unsafe partial void Method2(string s, int* i);

Co-authored-by: Jackson Schuster <[email protected]>
Copilot AI review requested due to automatic review settings July 21, 2026 08:50

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 5 out of 5 changed files in this pull request and generated 1 comment.

Covers user-declared 'unsafe' on the method declaration for both a bodyless forwarder stub and a marshalling (wrapper) stub with a pointer parameter, verifying the modifier is copied verbatim and the containing type is not marked unsafe.

Co-authored-by: Copilot <[email protected]>

Copilot-Session: 7cec108-b256-42c4-be29-a95b75550247
Copilot AI review requested due to automatic review settings July 21, 2026 09:00

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 5 out of 5 changed files in this pull request and generated no new comments.

@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.

Thank you!

@EgorBo

EgorBo commented Jul 21, 2026

Copy link
Copy Markdown
Member Author

/ba-g deadletter

@EgorBo
EgorBo enabled auto-merge (squash) July 21, 2026 19:52
@EgorBo
EgorBo merged commit 8435557 into dotnet:main Jul 21, 2026
91 of 95 checks passed
@EgorBo
EgorBo deleted the libimport-gen-unsafe-block branch July 21, 2026 19:52
@github-actions github-actions Bot locked and limited conversation to collaborators Aug 23, 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.

4 participants