diff --git a/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/ComInterfaceGenerator.cs b/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/ComInterfaceGenerator.cs index 26eae7fd73c406..e10a2ddd2f67dc 100644 --- a/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/ComInterfaceGenerator.cs +++ b/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/ComInterfaceGenerator.cs @@ -204,7 +204,7 @@ public void Initialize(IncrementalGeneratorInitializationContext context) using StringWriter source = new(); source.WriteLine("// "); - source.WriteLine("#pragma warning disable CS0612, CS0618, CS0649"); // Suppress warnings about [Obsolete] and "lack of assignment" in generated code. + source.WriteLine("#pragma warning disable CS0612, CS0618, CS0649, CS1591"); // Suppress warnings about [Obsolete], "lack of assignment", and missing XML documentation in generated code. // If the user has specified 'ManagedObjectWrapper', it means that the COM interface will never be used to marshal a native // object as an RCW (eg. the IDIC vtable will also not be generated, nor any additional supporting code). To reduce binary diff --git a/src/libraries/System.Runtime.InteropServices/tests/ComInterfaceGenerator.Unit.Tests/Compiles.cs b/src/libraries/System.Runtime.InteropServices/tests/ComInterfaceGenerator.Unit.Tests/Compiles.cs index 3f5a90d3c1a2c0..247bd7a84f56e0 100644 --- a/src/libraries/System.Runtime.InteropServices/tests/ComInterfaceGenerator.Unit.Tests/Compiles.cs +++ b/src/libraries/System.Runtime.InteropServices/tests/ComInterfaceGenerator.Unit.Tests/Compiles.cs @@ -4,8 +4,12 @@ using System; using System.Collections.Generic; using System.Diagnostics; +using System.Linq; using System.Runtime.CompilerServices; using System.Threading.Tasks; +using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.CSharp; +using Microsoft.CodeAnalysis.Testing; using Microsoft.Interop.UnitTests; using Xunit; using VerifyComInterfaceGenerator = Microsoft.Interop.UnitTests.Verifiers.CSharpSourceGeneratorVerifier; @@ -369,5 +373,80 @@ public async Task ValidateComInterfaceSnippets(string id, string source) await VerifyComInterfaceGenerator.VerifySourceGeneratorAsync(source); } + + [Fact] + public async Task DocumentedComInterfaceDoesNotProduceCS1591Warnings() + { + string source = """ + using System.Runtime.InteropServices; + using System.Runtime.InteropServices.Marshalling; + + namespace Test + { + /// + /// This is my interface. + /// + [GeneratedComInterface, Guid("27dd3a3d-4c16-485a-a123-7cd8f39c6ef2")] + public partial interface IMyInterface + { + /// + /// This does something. + /// + void DoSomething(); + } + + /// + /// This is my other interface. + /// + [GeneratedComInterface, Guid("1b681178-368a-4d13-8893-66b4673d2ff9")] + public partial interface MyOtherInterface : IMyInterface + { + /// + /// This does something else. + /// + void DoSomethingElse(); + } + } + """; + + var test = new VerifyCompilationTest(false) + { + TestCode = source, + TestBehaviors = TestBehaviors.SkipGeneratedSourcesCheck | TestBehaviors.SkipGeneratedCodeCheck, + CompilationVerifier = compilation => + { + // Verify that no CS1591 warnings are produced in the generated code + var diagnostics = compilation.GetDiagnostics(); + var cs1591Diagnostics = diagnostics.Where(d => d.Id == "CS1591").ToList(); + + Assert.Empty(cs1591Diagnostics); + } + }; + + // Enable XML documentation warnings to ensure CS1591 would be raised if not suppressed + test.SolutionTransforms.Add((solution, projectId) => + { + var project = solution.GetProject(projectId); + if (project is null) return solution; + + // Set parse options to enable documentation mode which is required for CS1591 validation + var parseOptions = (CSharpParseOptions?)project.ParseOptions; + if (parseOptions is not null) + { + parseOptions = parseOptions.WithDocumentationMode(DocumentationMode.Diagnose); + solution = solution.WithProjectParseOptions(projectId, parseOptions); + project = solution.GetProject(projectId)!; + } + + var compilationOptions = project.CompilationOptions! + .WithSpecificDiagnosticOptions(new Dictionary + { + ["CS1591"] = ReportDiagnostic.Warn + }); + return solution.WithProjectCompilationOptions(projectId, compilationOptions); + }); + + await test.RunAsync(); + } } }