Skip to content

Commit c897573

Browse files
[release/8.0-staging] Check if AttributeData for InterfaceTypeAttribute is not null in ConvertComImportToGeneratedComInterfaceAnalyzer (#98873)
Co-authored-by: Mario Pistrich <mario@pistrich.com>
1 parent ebb4a50 commit c897573

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/Analyzers/ConvertComImportToGeneratedComInterfaceAnalyzer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public override void Initialize(AnalysisContext context)
5353
INamedTypeSymbol type = (INamedTypeSymbol)context.Symbol;
5454
AttributeData? interfaceTypeAttributeData = type.GetAttributes().FirstOrDefault(a => a.AttributeClass.Equals(interfaceTypeAttribute, SymbolEqualityComparer.Default));
5555
if (type is not { TypeKind: TypeKind.Interface, IsComImport: true }
56-
|| interfaceTypeAttributeData.ConstructorArguments.Length == 1 && (int)interfaceTypeAttributeData.ConstructorArguments[0].Value != (int)ComInterfaceType.InterfaceIsIUnknown)
56+
|| interfaceTypeAttributeData is not { ConstructorArguments: [{ Value: (int)ComInterfaceType.InterfaceIsIUnknown }] })
5757
{
5858
return;
5959
}

src/libraries/System.Runtime.InteropServices/tests/ComInterfaceGenerator.Unit.Tests/ConvertToGeneratedComInterfaceTests.cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,5 +370,43 @@ public struct HResult
370370

371371
await VerifyCS.VerifyCodeFixAsync(source, fixedSource);
372372
}
373+
374+
[Fact]
375+
public async Task UnsupportedInterfaceTypes_DoesNotReportDiagnostic()
376+
{
377+
// This also tests the case where InterfaceType is missing (defaulting to ComInterfaceType.InterfaceIsDual).
378+
string source = """
379+
using System.Runtime.InteropServices;
380+
381+
[ComImport]
382+
[Guid("73EB4AF8-BE9C-4b49-B3A4-24F4FF657B26")]
383+
public interface IInterfaceIsDualMissingAttribute
384+
{
385+
}
386+
387+
[ComImport]
388+
[Guid("5DA39CDF-DCAD-447A-836E-EA80DB34D81B")]
389+
[InterfaceType(ComInterfaceType.InterfaceIsDual)]
390+
public interface IInterfaceIsDual
391+
{
392+
}
393+
394+
[ComImport]
395+
[Guid("F59AB2FE-523D-4B28-911C-21363808C51E")]
396+
[InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
397+
public interface IInterfaceIsIDispatch
398+
{
399+
}
400+
401+
[ComImport]
402+
[Guid("DC1C5A9C-E88A-4dde-A5A1-60F82A20AEF7")]
403+
[InterfaceType(ComInterfaceType.InterfaceIsIInspectable)]
404+
public interface IInterfaceIsIInspectable
405+
{
406+
}
407+
""";
408+
409+
await VerifyCS.VerifyCodeFixAsync(source, source);
410+
}
373411
}
374412
}

0 commit comments

Comments
 (0)