Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Jan 27, 2026

Description

Interfaces with default implementation members (C# 8+) can contain executable code that appears in code coverage reports. This PR adds AttributeTargets.Interface to both attributes as approved in API review.

Changes

  • Implementation: Added AttributeTargets.Interface to ExcludeFromCodeCoverageAttribute and StackTraceHiddenAttribute in System.Private.CoreLib
  • NativeAOT: Updated StackTraceHiddenAttribute in Test.CoreLib
  • Reference assembly: Updated System.Runtime.cs
  • API compat: Added suppressions in ApiCompatBaseline.NetCoreAppLatestStable.xml and ApiCompatBaseline.netstandard2.1.xml
  • Tests: Added tests verifying attribute application to interfaces

API Diff

namespace System.Diagnostics.CodeAnalysis
{
-    [AttributeUsage(AttributeTargets.Assembly | AttributeTargets.Class | ... | AttributeTargets.Event, ...)]
+    [AttributeUsage(AttributeTargets.Assembly | AttributeTargets.Class | ... | AttributeTargets.Event | AttributeTargets.Interface, ...)]
    public sealed class ExcludeFromCodeCoverageAttribute : Attribute;
}

namespace System.Diagnostics
{
-    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Constructor | AttributeTargets.Method, ...)]
+    [AttributeUsage(AttributeTargets.Class | ... | AttributeTargets.Method | AttributeTargets.Interface, ...)]
    public sealed class StackTraceHiddenAttribute : Attribute;
}

Example

[ExcludeFromCodeCoverage]
public interface ILogger
{
    void Log(string message);

    // Default interface method now excluded from coverage
    void LogWarning(string message) => Log($"[WARN] {message}");
}
Original prompt

This section details on the original issue you should resolve

<issue_title>ExcludeFromCodeCoverageAttribute can't be applied to interfaces</issue_title>
<issue_description>### Description

With interfaces now having the ability to have default implementations members, they can now contain code, which can show up on a code coverage report. The AttributeUsage attribute on ExcludeFromCodeCoverageAttribute does not specify AttributeTargets.Interface so applying the attribute to an interface will cause a compiler error.

Reproduction Steps

Try to compile the following code:

[ExcludeFromCodeCoverage]
public interface ILogger
{
    void Log(string message);

    // Default interface method (C# 8+)
    void LogWarning(string message)
        => Log($"[WARN] {message}");
}

Expected behavior

The provided sample code to be compiled without error.

Actual behavior

You will get a compilation error as ExcludeFromCodeCoverageAttribute can't be applied to an interface.

Regression?

No regression

Known Workarounds

I would expect applying the attribute to the individual method implementations would work.

Configuration

All versions of .NET, any OS, any architecture.

Other information

Missing AttributeTargets.Interface on the AttributeUsage attribute of ExcludeFromCodeCoverageAttribute.</issue_description>

<agent_instructions>Implement the approved APIs, updating the ref assemblies as well</agent_instructions>

Comments on the Issue (you are @copilot in this section)

@bartonjs [Video](https://www.youtube.com/watch?v=qkCNatTijWg&t=1h38m59s)

Looks good as proposed.

Actually, we went ahead and pre-approved StackTraceHidden, too.

namespace System.Diagnostics.CodeAnalysis
{
-    [AttributeUsage(AttributeTargets.Assembly | AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Constructor | AttributeTargets.Method | AttributeTargets.Property | AttributeTargets.Event, Inherited = false, AllowMultiple = false)]
+    [AttributeUsage(AttributeTargets.Assembly | AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Constructor | AttributeTargets.Method | AttributeTargets.Property | AttributeTargets.Event | AttributeTargets.Interface, Inherited = false, AllowMultiple = false)]
    public sealed class ExcludeFromCodeCoverageAttribute : Attribute;
}

namespace System.Diagnostics
{
-    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Constructor | AttributeTargets.Method, Inherited = false)]
+    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Constructor | AttributeTargets.Method | AttributeTargets.Interface, Inherited = false)]
    public sealed class StackTraceHiddenAttribute : Attribute;
}
```</body></comment_new>
</comments>

✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI and others added 2 commits January 27, 2026 20:38
…d StackTraceHiddenAttribute

Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
…assembly

Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix ExcludeFromCodeCoverageAttribute for interfaces Add AttributeTargets.Interface to ExcludeFromCodeCoverageAttribute and StackTraceHiddenAttribute Jan 27, 2026
Copilot AI requested a review from stephentoub January 27, 2026 20:42
…/Diagnostics/StackTraceHiddenAttributeTests.cs
@stephentoub stephentoub marked this pull request as ready for review January 27, 2026 20:47
Copilot AI review requested due to automatic review settings January 27, 2026 20:47
@dotnet-policy-service
Copy link
Contributor

Tagging subscribers to this area: @steveisok, @dotnet/area-system-diagnostics
See info in area-owners.md if you want to be subscribed.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds AttributeTargets.Interface to both ExcludeFromCodeCoverageAttribute and StackTraceHiddenAttribute to support interfaces with default implementation members (C# 8+) that contain executable code.

Changes:

  • Added AttributeTargets.Interface to ExcludeFromCodeCoverageAttribute and StackTraceHiddenAttribute in implementation and reference assemblies
  • Added API compatibility suppressions for the attribute usage changes
  • Added tests verifying the attributes can be applied to interfaces

Reviewed changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated no comments.

Show a summary per file
File Description
src/libraries/System.Private.CoreLib/src/System/Diagnostics/CodeAnalysis/ExcludeFromCodeCoverageAttribute.cs Added AttributeTargets.Interface to the attribute usage declaration in alphabetical order
src/libraries/System.Private.CoreLib/src/System/Diagnostics/StackTraceHiddenAttribute.cs Added AttributeTargets.Interface to the attribute usage declaration in alphabetical order
src/coreclr/nativeaot/Test.CoreLib/src/System/Diagnostics/StackTraceHiddenAttribute.cs Added AttributeTargets.Interface to maintain consistency with the main implementation
src/libraries/System.Runtime/ref/System.Runtime.cs Updated reference assembly to reflect the attribute usage changes for both attributes
src/libraries/apicompat/ApiCompatBaseline.NetCoreAppLatestStable.xml Added suppressions for CP0015 diagnostic due to attribute usage changes across multiple assemblies
src/libraries/apicompat/ApiCompatBaseline.netstandard2.1.xml Added suppression for CP0015 diagnostic for netstandard compatibility
src/libraries/System.Runtime/tests/System.Diagnostics.Tools.Tests/System/Diagnostics/CodeAnalysis/ExcludeFromCodeCoverageAttributeTests.cs Added test verifying ExcludeFromCodeCoverageAttribute can be applied to interfaces
src/libraries/System.Runtime/tests/System.Runtime.Tests/System/Diagnostics/StackTraceHiddenAttributeTests.cs Added test verifying StackTraceHiddenAttribute can be applied to interfaces

</Suppression>
<Suppression>
<DiagnosticId>CP0015</DiagnosticId>
<Target>T:System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverageAttribute:[T:System.AttributeUsageAttribute]</Target>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is ExcludeFromCodeCoverageAttribute listed 4 times in this file?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ExcludeFromCodeCoverageAttribute can't be applied to interfaces

3 participants