dotnet / runtime

.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps.
https://docs.microsoft.com/dotnet/core/
MIT License
15.15k stars 4.71k forks source link

[GeneratedComInterface] in CCW-only mode produces CA2256 warning #90922

Open Sergio0694 opened 1 year ago

Sergio0694 commented 1 year ago

Description

When using [GeneratedComInterface] with ComInterfaceOptions.ManagedObjectWrapper, the generator emits an empty interface declaration with [DynamicInterfaceCastableImplementation], which then produces a warning:

Reproduction Steps

[Guid("7448E99B-0904-4B6A-9CC2-51DD9A71032A")]
[GeneratedComInterface(Options = ComInterfaceOptions.ManagedObjectWrapper)]
public partial interface IFoo
{
    void Foo();
}

Expected behavior

No build warnings or errors.

Actual behavior

"Microsoft.Interop.ComInterfaceGenerator\IFoo.cs(12,31,12,54): warning CA2256: Type 'InterfaceImplementation' has the 'DynamicInterfaceCastableImplementationAttribute' applied to it but does not provide an implementation of all interface members defined in inherited interfaces (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2256)"

Regression?

No, it's a new feature.

Configuration

ghost commented 1 year ago

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

Issue Details
### Description When using `[GeneratedComInterface]` with `ComInterfaceOptions.ManagedObjectWrapper`, the generator emits an empty interface declaration with `[DynamicInterfaceCastableImplementation]`, which then produces a warning: ![](https://github.com/dotnet/runtime/assets/10199417/b1009396-bad6-4cc1-a5a2-096960b61fb5) ### Reproduction Steps ```csharp [Guid("7448E99B-0904-4B6A-9CC2-51DD9A71032A")] [GeneratedComInterface(Options = ComInterfaceOptions.ManagedObjectWrapper)] public partial interface IFoo { void Foo(); } ``` ### Expected behavior No build warnings or errors. ### Actual behavior > "Microsoft.Interop.ComInterfaceGenerator\IFoo.cs(12,31,12,54): warning CA2256: Type 'InterfaceImplementation' has the 'DynamicInterfaceCastableImplementationAttribute' applied to it but does not provide an implementation of all interface members defined in inherited interfaces (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2256)" ### Regression? No, it's a new feature. ### Configuration - VS 17.8 Preview 1 - .NET 8.0.100-rc.2.23421.16 - Roslyn 4.8.0-1.23377.4 (692d17e9)
Author: Sergio0694
Assignees: -
Labels: `area-System.Runtime.InteropServices`, `untriaged`
Milestone: -
smaillet commented 2 months ago

It's been almost a year, and this issue is still present in latest releases. What's the status? Can someone who actually understands the generated code and attribute explain if this is at least safe to ignore?

smaillet commented 2 months ago

So, I found a workaround at least, for anyone else seeing this. In a GlobalSupporession.cs file add the following assembly level suppression. This will, fortunately silence all of these warnings in generated code where the type InterfaceImplemantation is declared with the file modifier in the global namespace.

using System.Diagnostics.CodeAnalysis;

// Until https://github.com/dotnet/runtime/issues/90922 is resolved, this is the only way
// to suppress the warning in the generated code. The analyzer is ignoring that it is
// generated, and partial (where the declarations live in a different partial class
// [in the same "file"])
[assembly: SuppressMessage(
    "Usage",
    "CA2256:All members declared in parent interfaces must have an implementation in a DynamicInterfaceCastableImplementation-attributed interface",
    Justification = "Generated Code and analyzer ignores that it is generated AND partial",
    Scope = "type",
    Target = "~T:InterfaceImplementation")
]

Since the generated code uses the file modifier on a symbol in the global namespace, this single suppression applies to them all.