Skip to content

Add IsClosedTypeAttribute - #128599

Merged
tannergooding merged 4 commits into
mainfrom
copilot/add-closed-attribute
Jun 2, 2026
Merged

tannergooding merged 4 commits into
mainfrom
copilot/add-closed-attribute

Conversation

Copilot AI commented May 26, 2026 •

Copy link
Copy Markdown
Contributor

Exposes the attribute approved in #128161 (comment), emitted by the C# compiler to mark types declared as closed (see the closed-hierarchies proposal). Per @bartonjs's feedback, the name was changed from ClosedAttribute to IsClosedTypeAttribute since it is not intended to be written by users.

namespace System.Runtime.CompilerServices
{
    [EditorBrowsable(EditorBrowsableState.Never)]
    [AttributeUsage(AttributeTargets.Class, Inherited = false)]
    public sealed class IsClosedTypeAttribute : Attribute
    {
        public IsClosedTypeAttribute() { }
    }
}

Changes:

  • New type: src/libraries/System.Private.CoreLib/src/System/Runtime/CompilerServices/IsClosedTypeAttribute.cs, marked [EditorBrowsable(Never)] to match the convention for compiler-only attributes (e.g. RequiresLocationAttribute).
  • Build: include the new file in System.Private.CoreLib.Shared.projitems.
  • Ref assembly: add the corresponding declaration in src/libraries/System.Runtime/ref/System.Runtime.cs.
  • Tests: add a constructor smoke test in AttributesTests.cs next to the existing RequiresLocationAttribute test.
  • Feedback fix: remove explicit AllowMultiple = false from AttributeUsage in both implementation and ref declaration to resolve ApiCompat CP0015 (System.Private.CoreLib ref/lib attribute argument mismatch).

AttributeTargets is intentionally restricted to Class only; broadening to interfaces/enums is deferred until those "closed" forms are designed.

Copilot AI self-assigned this May 26, 2026
Copilot AI review requested due to automatic review settings May 26, 2026 17:27
Copilot AI removed the request for review from Copilot May 26, 2026 17:27
Copilot AI linked an issue May 26, 2026 that may be closed by this pull request
Co-authored-by: tannergooding <[email protected]>
Copilot AI requested review from Copilot and removed request for Copilot May 26, 2026 17:32
Copilot AI changed the title [WIP] Add ClosedAttribute to support closed class hierarchies Add IsClosedTypeAttribute May 26, 2026
Copilot AI requested a review from tannergooding May 26, 2026 17:33
@tannergooding
tannergooding requested a review from RikkiGibson May 26, 2026 17:35
@tannergooding
tannergooding marked this pull request as ready for review May 26, 2026 17:35
Copilot AI review requested due to automatic review settings May 26, 2026 17:35

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

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 a new compiler-reserved attribute, System.Runtime.CompilerServices.IsClosedTypeAttribute, to the BCL surface area so the C# compiler can emit it for closed class hierarchies, with corresponding reference assembly and a basic construction test.

Changes:

  • Added IsClosedTypeAttribute implementation in System.Private.CoreLib with [EditorBrowsable(Never)] and class-only AttributeUsage.
  • Updated the System.Runtime ref assembly to expose the new attribute publicly.
  • Added a small xUnit smoke test to ensure the attribute is constructible.

Reviewed changes

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

File Description
src/libraries/System.Runtime/tests/System.Runtime.Tests/System/Runtime/CompilerServices/AttributesTests.cs Adds a constructor smoke test for IsClosedTypeAttribute.
src/libraries/System.Runtime/ref/System.Runtime.cs Adds the public reference declaration for IsClosedTypeAttribute.
src/libraries/System.Private.CoreLib/src/System/Runtime/CompilerServices/IsClosedTypeAttribute.cs Implements the new compiler-reserved attribute in CoreLib.
src/libraries/System.Private.CoreLib/src/System.Private.CoreLib.Shared.projitems Includes the new source file in the CoreLib shared build items.

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

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

Copilot AI requested a review from tannergooding May 26, 2026 18:30
@tannergooding
tannergooding enabled auto-merge (squash) May 26, 2026 20:15
Copilot AI review requested due to automatic review settings June 2, 2026 01:07

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[API Proposal]: ClosedAttribute

4 participants