Skip to content

Constraint attributes for C# 15 unions and closed classes #209

Open

Description

@CaffeinatedCoder

What you're trying to do

Constrain a type argument to the kinds C# 15 adds. You can't write where T : union or where T : closed. The analyzer's constraint attributes (TypeArgumentConstraints.cs, checked by CMTK0010) cover abstract, sealed, record, concrete, interface, value-object, closed-set, flags-enum and readonly-struct, but nothing for a union or a closed hierarchy.

What you'd like

public static string Describe<[UnionType] T>(T value) where T : notnull { ... }
public static IReadOnlyList<Type> Cases<[ClosedHierarchy] T>() { ... }
  • Two new attributes. One admits only a union, compiler-made or custom. The other admits only a closed class.
    • Names: decided in the PR. [Union] collides with System.Runtime.CompilerServices.UnionAttribute, which the compiler puts on a union. [Closed] reads like [ClosedSet], and closed is now a contextual keyword.
  • How the analyzer recognises them. The newest Microsoft.CodeAnalysis.CSharp on nuget.org is 5.9.0, without ITypeSymbol.IsUnion, which is still in Roslyn's PublicAPI.Unshipped.txt. So the analyzer resolves the BCL types by metadata name, as it already resolves System.Func1andSystem.Linq.IQueryable1: UnionAttribute and IUnion for a union. To measure: how closed is emitted in metadata, an attribute or a flag.
  • The existing attributes' answers for the new kinds. Add union and closed rows to TypeArgumentKinds (test/CodoMetis.TypeKit.Analyzers.Tests/Variants/) and decide each attribute's answer:
    • A closed class is implicitly abstract: [Abstract] admits it and [Concrete] refuses it.
    • From another assembly, a closed class can't be derived from. Decide what [Unsealed] says about it, in line with the 3.0.0 roadmap item on abstract classes nothing can derive from.
    • A union is a struct: decide [Sealed], [ReadOnlyStruct], [ValueObject] and [Record] from the emitted shape.
    • Decision 34 applies. Before it ships, measure what CMTK0010 answers today for the new kinds, because a change that reports more under an Error waits for 3.0.0.
  • Targets. The attributes go on both targets. A net10.0 library's generic method is often called from a net11.0 application that passes a union.
  • Reach. 2.4.0 moves to Metalama 2027.0 (decision 129), so a woven net11.0 project declares and matches unions at C# 15 (KB-060). Test the attribute in a woven project.

Does it change something that compiles or works today?

No, it only adds. The one exception would be a changed answer for an existing attribute (above), which is measured first.

Activity

  1. added this to the 2.4.0 milestone on Oct 30, 2026
  2. added
    enhancement
    package/core
    package/analyzers
    on Oct 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementpackage/analyzerspackage/core

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions