Skip to content

Make ProgrammaticSuppressionInfo public #69000

@mavasani

Description

@mavasani

Background and Motivation

We added support for DiagnosticSuppressor API some time back. This work added a new internal type ProgrammaticSuppressionInfo and an internal property Diagnostic.ProgrammaticSuppressionInfo to the compiler layer. This issue proposes making these two APIs public, without any changes or additions to the implementation with one minor change to the existing internal API shape: change the type of ProgrammaticSuppressionInfo.Suppressions property from ImmutableHashSet<(string Id, LocalizableString Justification)> to ImmutableArray<Suppression>. See existing definition of public type Suppression here

Motivation:

  1. Testing support: From @sharwell:

    We need this public API for DiagnosticSuppressor unit testing support in the Microsoft.CodeAnalysis.Testing SDK. We tried two other approaches using public APIs but they caused problems for various edge cases. The library now uses the current property via reflection, which is fragile.

  2. Tooling support: Tools built on top of Microsoft.CodeAnalysis that require to read/operate on the programmatic suppressions from DiagnosticSuppressors would need this public API.

Proposed API

Existing internal type ProgrammaticSuppressionInfo to be made public:

namespace Microsoft.CodeAnalysis.Diagnostics
{
+     public sealed class ProgrammaticSuppressionInfo : IEquatable<ProgrammaticSuppressionInfo?>
+     {
+        // NOTE: The below property is currently implemented in Roslyn with type `ImmutableHashSet<(string Id, LocalizableString Justification)>`, which might not be best suited for a public API.
+        //       Also note that this is proposed as an array and not a single Suppression instance because multiple diagnostic suppressors can suppress the same diagnostic. For most real world cases, the below array will have a single element.
+        public ImmutableArray<Suppression> Suppressions { get; }

+        public bool Equals(ProgrammaticSuppressionInfo? other);
+        public overrides bool Equals(object? other);
+        public overrides int GetHashCode();
+     }
}

Existing internal property Diagnostic.ProgrammaticSuppressionInfo to be made public:

namespace Microsoft.CodeAnalysis
{
      public abstract class Diagnostic
      {
+        public virtual ProgrammaticSuppressionInfo? ProgrammaticSuppressionInfo { get; };
      }
}

NOTE: Diagnostic type has internal abstract methods, and hence cannot be sub-typed outside the compiler layer:

/// <summary>
/// Create a new instance of this diagnostic with the Location property changed.
/// </summary>
internal abstract Diagnostic WithLocation(Location location);
/// <summary>
/// Create a new instance of this diagnostic with the Severity property changed.
/// </summary>
internal abstract Diagnostic WithSeverity(DiagnosticSeverity severity);
/// <summary>
/// Create a new instance of this diagnostic with the suppression info changed.
/// </summary>
internal abstract Diagnostic WithIsSuppressed(bool isSuppressed);

Metadata

Metadata

Assignees

Labels

Area-AnalyzersConcept-APIThis issue involves adding, removing, clarification, or modification of an API.Feature Requestapi-approvedAPI was approved in API review, it can be implemented

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions