Skip to content

Warning for partial ComImport types with members in more than one type part #59013

Open

Description

@jnm2

The same reasoning applies as the reasoning for why partial enums wouldn't make sense. The meaning of a member in a ComImport type changes based on its order within the type.

There is currently no warning for a pair of source files like this, but there should be:

[ComImport]
partial interface ISomeInterface { void Foo(); }
partial interface ISomeInterface { void Bar(); }

There should be no warning for a case like this:

[ComImport]
partial interface ISomeInterface { void Foo(); }
[SomeInterface]
partial interface ISomeInterface
{
    public struct DoesNotAffectVTable { }
}

Why not warn for partial ComImport interfaces in the first place?

The source generator https://github.com/microsoft/CsWin32 is a new official vehicle for consuming Windows platform APIs. It generates most types as partial so that you can add customizations, but it doesn't do this for ComImport interfaces. @AArnott cited the danger described at the top. The use case for a partial ComImport interface was to add attributes:

namespace Windows.Win32.UI.Shell
{
    [CoClass(typeof(ShellLink))]
    internal partial interface IShellLinkW { }
}

This is safe, but adding a member would not be safe. Therefore the warning should only apply if more than one type part has members.

Activity

  1. AArnott commented on Jul 21, 2021

    @AArnott
    Contributor

    The same reasoning applies as the reasoning for why partial enums wouldn't make sense.

    partial enums are fine if the first enum value declared in each partial explicitly prescribes the value. For example, there's no problem here:

    partial enum Foo
    {
        A = 1,
        B,
    }
    
    partial enum Foo
    {
        C = 3,
        D,
    }
  2. jnm2 commented on Jul 21, 2021

    @jnm2
    ContributorAuthor

    That will give "CS0267: The 'partial' modifier can only appear immediately before 'class', 'record', 'struct', 'interface', or a method return type." (dotnet/csharplang#2669) Even if partial enums with explicit ordering were allowed in the future, you'd want errors or warnings if the order wasn't explicitly given. That error/warning would be analogous to what we're asking for in ComImport types.

  3. jaredpar commented on Aug 2, 2021

    @jaredpar
    Member

    Moving to roslyn-analyzers as this feels more like an analyzer issue vs. a core compiler one. The reasoning being that [ComImport] isn't really a concept that is understood by the compiler. It exists outside of it. That's very much in analyzer territory.

  4. transferred this issue fromdotnet/roslynon Aug 2, 2021
  5. mavasani commented on Sep 13, 2021

    @mavasani

    Needs to be triaged/approved by dotnet/runtime.

  6. ghost added
    untriaged
    on Sep 13, 2021
  7. added
    code-analyzer
    on Sep 13, 2021
  8. removed
    untriaged
    on Sep 13, 2021
  9. added this to the 7.0.0 milestone on Sep 13, 2021
  10. modified the milestones: 7.0.0, 8.0.0 on Jul 6, 2022
  11. elachlan commented on Aug 17, 2022

    @elachlan
    Contributor

    This is a potential blocker for winforms as we attempt to migrate interop code to CsWin32.

    CC: @JeremyKuhne, @lonitra

  12. elachlan commented on Aug 18, 2022

    @elachlan
    Contributor

    @danmoseley can you please see if you can get this unstuck?

  13. AArnott commented on Aug 19, 2022

    @AArnott
    Contributor

    @elachlan Can you elaborate on how lack of an analyzer would be a blocker for WinForms? At best it seems to me it would just help you write correct code. But when would WinForms define partial COM interfaces? CsWin32 is being used in WinForms without marshaling, so interfaces aren't even being generated.

  14. added
    api-suggestion
    on Nov 9, 2022
  15. modified the milestones: 8.0.0, 9.0.0 on Jul 10, 2023
  16. modified the milestones: 9.0.0, 10.0.0 on Jun 20, 2024
  17. modified the milestones: 10.0.0, 11.0.0 on Aug 18, 2025
  18. dotnet-policy-service commented on Apr 13, 2026

    @dotnet-policy-service
    Contributor

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

  19. modified the milestones: 11.0.0, Future on Apr 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    api-suggestionarea-Interop-coreclrcode-analyzer

    Type

    No type

    Projects

    • Status
      No status

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions