diff --git a/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/Analyzers/RuntimeComApiUsageWithSourceGeneratedComAnalyzer.cs b/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/Analyzers/RuntimeComApiUsageWithSourceGeneratedComAnalyzer.cs index 6cbb11e69c1aca..2a039a9a4363fa 100644 --- a/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/Analyzers/RuntimeComApiUsageWithSourceGeneratedComAnalyzer.cs +++ b/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/Analyzers/RuntimeComApiUsageWithSourceGeneratedComAnalyzer.cs @@ -135,6 +135,7 @@ public override void Initialize(AnalysisContext context) context.RegisterOperationAction(context => { var operation = (IConversionOperation)context.Operation; + if (operation.Type is INamedTypeSymbol { IsComImport: true }) { IOperation operand = operation.Operand; @@ -142,6 +143,12 @@ public override void Initialize(AnalysisContext context) { operand = objConversion.Operand; } + if (operand.Type is null) + { + // Some operations like the "null" literal expression don't have a type. + // These expressions definitely aren't a source-generated COM type, so we can skip them. + return; + } foreach (var recognizer in sourceGeneratedComRecognizers) { if (recognizer(operand.Type)) diff --git a/src/libraries/System.Runtime.InteropServices/tests/ComInterfaceGenerator.Unit.Tests/RuntimeComApiUsageWithSourceGeneratedComTests.cs b/src/libraries/System.Runtime.InteropServices/tests/ComInterfaceGenerator.Unit.Tests/RuntimeComApiUsageWithSourceGeneratedComTests.cs index 25246b3d292651..7756af0fa4dce2 100644 --- a/src/libraries/System.Runtime.InteropServices/tests/ComInterfaceGenerator.Unit.Tests/RuntimeComApiUsageWithSourceGeneratedComTests.cs +++ b/src/libraries/System.Runtime.InteropServices/tests/ComInterfaceGenerator.Unit.Tests/RuntimeComApiUsageWithSourceGeneratedComTests.cs @@ -25,7 +25,7 @@ public async Task SetComObjectData() public interface I { } - + [GeneratedComClass] public class C : I { @@ -63,12 +63,12 @@ public async Task GetComObjectData() public interface I { } - + [GeneratedComClass] public class C : I { } - + public static class Program { public static void Foo(I i) @@ -95,13 +95,13 @@ public async Task ReleaseComObject() string source = """ using System.Runtime.InteropServices; using System.Runtime.InteropServices.Marshalling; - + [GeneratedComInterface] [Guid("0B7171CD-04A3-41B6-AD10-FE86D52197DD")] public interface I { } - + [GeneratedComClass] public class C : I { @@ -133,18 +133,18 @@ public async Task FinalReleaseComObject() string source = """ using System.Runtime.InteropServices; using System.Runtime.InteropServices.Marshalling; - + [GeneratedComInterface] [Guid("0B7171CD-04A3-41B6-AD10-FE86D52197DD")] public interface I { } - + [GeneratedComClass] public class C : I { } - + public static class Program { public static void Foo(I i) @@ -171,13 +171,13 @@ public async Task CreateAggregatedObject() string source = """ using System.Runtime.InteropServices; using System.Runtime.InteropServices.Marshalling; - + [GeneratedComInterface] [Guid("0B7171CD-04A3-41B6-AD10-FE86D52197DD")] public interface I { } - + [GeneratedComClass] public class C : I { @@ -269,7 +269,7 @@ public static void Foo(ComObject i) """; await VerifyAnalyzerAsync(source); - } + } [Fact] public async Task GetTypedObjectForIUnknown() @@ -309,18 +309,18 @@ public async Task GetIUnknownForObject() string source = """ using System.Runtime.InteropServices; using System.Runtime.InteropServices.Marshalling; - + [GeneratedComInterface] [Guid("0B7171CD-04A3-41B6-AD10-FE86D52197DD")] public interface I { } - + [GeneratedComClass] public class C : I { } - + public static class Program { public static void Foo(I i) @@ -347,18 +347,18 @@ public async Task GetIDispatchForObject() string source = """ using System.Runtime.InteropServices; using System.Runtime.InteropServices.Marshalling; - + [GeneratedComInterface] [Guid("0B7171CD-04A3-41B6-AD10-FE86D52197DD")] public interface I { } - + [GeneratedComClass] public class C : I { } - + public static class Program { public static void Foo(I i) @@ -385,18 +385,18 @@ public async Task GetComInterfaceForObject() string source = """ using System.Runtime.InteropServices; using System.Runtime.InteropServices.Marshalling; - + [GeneratedComInterface] [Guid("0B7171CD-04A3-41B6-AD10-FE86D52197DD")] public interface I { } - + [GeneratedComClass] public class C : I { } - + public static class Program { public static void Foo(I i) @@ -432,13 +432,13 @@ public async Task CastsBetweenComImportAndGeneratedComTypes() string source = """ using System.Runtime.InteropServices; using System.Runtime.InteropServices.Marshalling; - + [GeneratedComInterface] [Guid("0B7171CD-04A3-41B6-AD10-FE86D52197DD")] public interface I { } - + [GeneratedComClass] public class C : I { @@ -449,7 +449,7 @@ public class C : I public interface J { } - + public static class Program { public static void Foo(I i) @@ -483,18 +483,18 @@ public async Task GetObjectForIUnknown() string source = """ using System.Runtime.InteropServices; using System.Runtime.InteropServices.Marshalling; - + [GeneratedComInterface] [Guid("0B7171CD-04A3-41B6-AD10-FE86D52197DD")] public interface I { } - + [GeneratedComClass] public class C : I { } - + public static class Program { public static void Foo(nint i) @@ -509,6 +509,28 @@ public static void Foo(nint i) await VerifyAnalyzerAsync(source); } + [Fact] + public async Task SetNullToComImportField() + { + string source = """ + using System.Runtime.InteropServices; + using System.Runtime.InteropServices.Marshalling; + + [ComImport] + [Guid("0BADBF92-749A-44DB-9DA0-C8E2EEC783E2")] + public interface J + { + } + + public class X + { + public static J j = null; + } + """; + + await VerifyAnalyzerAsync(source); + } + private Task VerifyAnalyzerAsync(string source) { var test = new VerifyCS.Test