[GeneratedComInterface] Add unsafe to derived partial interface when base has unsafe methods#126109
[GeneratedComInterface] Add unsafe to derived partial interface when base has unsafe methods#126109
unsafe to derived partial interface when base has unsafe methods#126109Conversation
|
Tagging subscribers to this area: @vitek-karas, @BrzVlad, @kotlarmilos |
…e has unsafe methods Co-authored-by: jkoritzinsky <1571408+jkoritzinsky@users.noreply.github.com> Agent-Logs-Url: https://github.com/dotnet/runtime/sessions/0c4933d7-3aef-4bef-8952-d134aa2a2767
jkoritzinsky
left a comment
There was a problem hiding this comment.
@copilot can we do this unconditionally instead of limiting it to cases where the methods need the unsafe keyword?
unsafe to derived partial interface when base has unsafe methods
Co-authored-by: jkoritzinsky <1571408+jkoritzinsky@users.noreply.github.com> Agent-Logs-Url: https://github.com/dotnet/runtime/sessions/f6544f27-a7ba-4d9a-8e9d-bd9ad2ce3100
There was a problem hiding this comment.
Pull request overview
This PR fixes a codegen correctness issue in the ComInterfaceGenerator where derived [GeneratedComInterface] interfaces could fail to compile when inheriting unsafe (pointer-typed) base methods, by ensuring the generated “shadowing methods” partial interface is emitted in an unsafe context.
Changes:
- Refactored shadowing-method interface emission into
GenerateShadowingMethodsInterface, and ensured the generated partial interface always includes theunsafemodifier. - Added a new compilation test snippet reproducing the derived-interface + unsafe-base-method scenario.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/ComInterfaceGenerator.cs | Adds GenerateShadowingMethodsInterface and applies unsafe to the generated derived partial interface so pointer-typed shadowing methods compile. |
| src/libraries/System.Runtime.InteropServices/tests/ComInterfaceGenerator.Unit.Tests/CodeSnippets.cs | Adds DerivedComInterfaceTypeWithUnsafeBaseMethod test snippet covering the repro. |
| src/libraries/System.Runtime.InteropServices/tests/ComInterfaceGenerator.Unit.Tests/Compiles.cs | Wires the new snippet into the compilation test data set. |
This potentially needs to be changed to put it on the narrowest scope possible to ensure it remains compatible with the The exact direction the feature lands is TBD, but the general recommendation in either scenario, at least for dotnet/runtime, sounds like its going to land at putting |
When a
[GeneratedComInterface]-attributed derived interface inherits from a base interface containingunsafemethods (e.g., methods with pointer parameters), the generator emits shadowing/forwarding methods into a new partial interface declaration without the requiredunsafemodifier, causing CS0214.Changes
ComInterfaceGenerator.cs: Extracted shadowing-method interface generation into a newGenerateShadowingMethodsInterfacehelper. The helper unconditionally addsunsafeto the generated partial interface's modifiers viaAddToModifiers(no-op if already present), ensuring the emitted declaration is always valid regardless of whether the inherited methods use pointer types.CodeSnippets.cs/Compiles.cs: AddedDerivedComInterfaceTypeWithUnsafeBaseMethodtest snippet covering the exact repro scenario.Example
Previously generated (broken):
Now generates:
Original prompt
📱 Kick off Copilot coding agent tasks wherever you are with GitHub Mobile, available on iOS and Android.