Strip partial modifier from generated COM interface method stubs#126719
Strip partial modifier from generated COM interface method stubs#126719
Conversation
…d VtableIndexStubGenerator The ComInterfaceGenerator and VtableIndexStubGenerator were copying the partial modifier from user-declared interface methods to the generated stub code, producing invalid C#. This change strips the partial keyword from method modifiers when creating the method syntax template, similar to how the new keyword and accessibility modifiers are already stripped. Agent-Logs-Url: https://github.com/dotnet/runtime/sessions/de757761-5c54-412d-9875-7403edd00714 Co-authored-by: jtschuster <36744439+jtschuster@users.noreply.github.com>
src/libraries/System.Runtime.InteropServices/tests/ComInterfaceGenerator.Unit.Tests/Compiles.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Runtime.InteropServices/tests/ComInterfaceGenerator.Unit.Tests/Compiles.cs
Outdated
Show resolved
Hide resolved
Co-authored-by: Jackson Schuster <36744439+jtschuster@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
This PR updates the COM interface source generators to avoid emitting invalid C# when user-declared COM interface methods include the partial modifier, by stripping partial from copied method modifiers and adding a regression compilation test.
Changes:
- Strip
SyntaxKind.PartialKeywordfrom copied method modifiers inComInterfaceGeneratorandVtableIndexStubGenerator. - Add a regression test ensuring partial method modifiers on
[GeneratedComInterface]methods don’t break generated code compilation.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| src/libraries/System.Runtime.InteropServices/tests/ComInterfaceGenerator.Unit.Tests/Compiles.cs | Adds a new compilation regression test for partial method modifiers on COM interface methods. |
| src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/VtableIndexStubGenerator.cs | Filters out partial from method modifiers when constructing the syntax template for generated stubs. |
| src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/ComInterfaceGenerator.cs | Filters out partial (alongside existing new filtering) from method modifiers used in generated stubs. |
| internal virtual partial interface IComInterface | ||
| { | ||
| void Method(); | ||
| public partial void PartialMethod(); | ||
| } | ||
| internal virtual partial interface IComInterface |
There was a problem hiding this comment.
virtual is not a valid modifier on an interface declaration, so this test source will fail to compile before the generator runs. Remove virtual from both IComInterface declarations (keep partial).
| internal virtual partial interface IComInterface | |
| { | |
| void Method(); | |
| public partial void PartialMethod(); | |
| } | |
| internal virtual partial interface IComInterface | |
| internal partial interface IComInterface | |
| { | |
| void Method(); | |
| public partial void PartialMethod(); | |
| } | |
| internal partial interface IComInterface |
| } | ||
| """; | ||
|
|
||
| // CS0539 is expected because the partial method's default implementation |
There was a problem hiding this comment.
The comment about expecting CS0539 is incomplete and no expected diagnostic is provided to the verifier. Either remove this comment or add an explicit expected diagnostic if the test intentionally includes a compiler error.
| // CS0539 is expected because the partial method's default implementation |
🤖 Copilot Code Review — PR #126719Note This review was generated by Copilot. Holistic AssessmentMotivation: The problem is real and clearly motivated — when a user declares a Approach: The fix is the right approach. Stripping Summary: Detailed Findings✅ Correctness — Generator fix is correctBoth The sibling generators (
|
Description
ComInterfaceGeneratorandVtableIndexStubGeneratorcopy method modifiers from user-declared interface methods into generated stub code. Thenewkeyword and accessibility modifiers were already stripped, butpartialwas not — producing invalid C# when the user declares partial methods on a[GeneratedComInterface]interface.Changes:
ComInterfaceGenerator.cs: FilterSyntaxKind.PartialKeywordalongside existingNewKeywordfilter inCalculateStubInformationVtableIndexStubGenerator.cs: AddPartialKeywordfilter to modifier stripping inCalculateStubInformationCompiles.cs: Add regression test confirming partial method modifiers don't produce invalid generated codeNote:
LibraryImportGeneratorandDownlevelLibraryImportGeneratorare intentionally unchanged — they generate partial method implementations wherepartialmust be preserved.