Repository navigation
Add UnionAttribute and IUnion interface to System.Runtime.CompilerServices #127001
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
eiriktsarpalis
merged 11 commits into
main
from
copilot/add-union-attribute-and-iuunion-interface
Apr 17, 2026
+73
−0
Merged
Changes from 6 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
2ce8283
Initial plan
Copilot 14dfeba
Add UnionAttribute and IUnion interface to System.Runtime.CompilerSer…
Copilot 68ee32d
Apply suggestion from @eiriktsarpalis
eiriktsarpalis a037949
Add comprehensive XML documentation for UnionAttribute and IUnion
Copilot 1b5fa60
Document Value property per language spec; seal UnionAttribute in ref…
Copilot c2d4e07
Update src/libraries/System.Private.CoreLib/src/System/Runtime/Compil…
eiriktsarpalis 6d1d2bc
Remove proposal links and simplify XML docs to avoid naming specific …
Copilot 44d9a8f
Manual cleanup of XML comments
eiriktsarpalis ed25811
Remove IUnionTests and TestUnion test class per review feedback
Copilot 798d7ab
Fix IUnion XML docs: implementing interface is not required for union…
Copilot 041b73a
Change UnionAttribute Inherited to false — derived types aren't union…
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
src/libraries/System.Private.CoreLib/src/System/Runtime/CompilerServices/IUnion.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| namespace System.Runtime.CompilerServices | ||
| { | ||
| /// <summary> | ||
| /// Provides a common interface for accessing the contents of a union type at runtime. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// <para> | ||
| /// The C# compiler automatically implements this interface on types generated by union declarations. | ||
| /// User-defined types annotated with <see cref="UnionAttribute" /> may also implement this interface | ||
| /// to participate in union behaviors such as implicit union conversions, union pattern matching, | ||
| /// and switch exhaustiveness checking. | ||
| /// </para> | ||
| /// <para> | ||
| /// The <see cref="Value" /> property is expected to return <see langword="null" /> or a value of | ||
| /// one of the union's case types. A union's case types are determined by its single-parameter | ||
| /// constructors (or static <c>Create</c> factory methods when a union member provider is used). | ||
| /// </para> | ||
| /// <para> | ||
| /// For more information, see the | ||
| /// <see href="https://github.com/dotnet/csharplang/blob/main/proposals/unions.md">C# unions proposal</see>. | ||
|
eiriktsarpalis marked this conversation as resolved.
Resolved
eiriktsarpalis marked this conversation as resolved.
Resolved
|
||
| /// </para> | ||
| /// </remarks> | ||
| /// <seealso cref="UnionAttribute" /> | ||
| public interface IUnion | ||
| { | ||
| /// <summary> | ||
| /// Gets the value contained in the union, or <see langword="null" /> if the union has no value. | ||
| /// </summary> | ||
| /// <value> | ||
| /// The current value of the union as one of its case types, or <see langword="null" />. | ||
| /// </value> | ||
| /// <remarks> | ||
| /// <para> | ||
| /// The <see cref="Value" /> property provides access to the contents of a union regardless of the | ||
| /// case type. The compiler uses this property when performing union pattern matching: patterns applied | ||
| /// to a union value are implicitly applied to its <see cref="Value" /> property rather than to the | ||
| /// union wrapper itself. | ||
| /// </para> | ||
| /// <para> | ||
| /// The property is expected to satisfy the following well-formedness invariants: | ||
| /// </para> | ||
| /// <list type="bullet"> | ||
| /// <item><description> | ||
| /// Soundness: <see cref="Value" /> always evaluates to <see langword="null" /> or to an instance of | ||
| /// one of the union's case types, including for the default value of the union type. | ||
| /// </description></item> | ||
| /// <item><description> | ||
| /// Stability: if the union is created from a case type value, <see cref="Value" /> returns that value | ||
| /// (or <see langword="null" />). If the union is created from <see langword="null" />, | ||
| /// <see cref="Value" /> returns <see langword="null" />. | ||
| /// </description></item> | ||
| /// </list> | ||
| /// </remarks> | ||
| object? Value { get; } | ||
| } | ||
| } | ||
61 changes: 61 additions & 0 deletions
61
src/libraries/System.Private.CoreLib/src/System/Runtime/CompilerServices/UnionAttribute.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| namespace System.Runtime.CompilerServices | ||
| { | ||
| /// <summary> | ||
| /// Indicates that a class or struct is a union type, enabling compiler support for union behaviors. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// <para> | ||
| /// Any class or struct annotated with this attribute is recognized by the C# compiler as a union type. | ||
| /// Union types support the following behaviors: | ||
| /// </para> | ||
| /// <list type="bullet"> | ||
| /// <item><description> | ||
| /// Implicit conversions from each case type to the union type (union conversions). | ||
| /// </description></item> | ||
| /// <item><description> | ||
| /// Pattern matching that automatically unwraps the union's contents, applying patterns to | ||
| /// the underlying value rather than to the union wrapper itself (union matching). | ||
| /// </description></item> | ||
| /// <item><description> | ||
| /// Switch exhaustiveness checking that considers a switch complete when all case types | ||
| /// have been matched, without requiring a fallback case (union exhaustiveness). | ||
| /// </description></item> | ||
| /// </list> | ||
| /// <para> | ||
| /// A union type must follow the basic union pattern: it must have at least one single-parameter | ||
| /// public constructor (or static <c>Create</c> factory method when using a union member provider), | ||
| /// and a public <c>Value</c> property of type <see cref="object" />. The parameter types of these | ||
|
eiriktsarpalis marked this conversation as resolved.
Resolved
|
||
| /// constructors or factory methods determine the union's case types. Implementing | ||
| /// <see cref="IUnion" /> is optional but recommended, as it provides runtime access to | ||
| /// the union's value and is automatically implemented by compiler-generated union declarations. | ||
| /// </para> | ||
| /// <para> | ||
| /// The C# <c>union</c> declaration syntax automatically generates a struct annotated with this | ||
| /// attribute and implementing <see cref="IUnion" />. | ||
| /// </para> | ||
| /// <para> | ||
| /// For more information, see the | ||
| /// <see href="https://github.com/dotnet/csharplang/blob/main/proposals/unions.md">C# unions proposal</see>. | ||
| /// </para> | ||
| /// </remarks> | ||
|
eiriktsarpalis marked this conversation as resolved.
Resolved
|
||
| /// <example> | ||
| /// The following example shows a manually declared union type: | ||
| /// <code language="csharp"> | ||
| /// [Union] | ||
| /// public struct Pet : IUnion | ||
| /// { | ||
| /// public Pet(Cat value) => Value = value; | ||
| /// public Pet(Dog value) => Value = value; | ||
| /// public object? Value { get; } | ||
| /// } | ||
| /// </code> | ||
| /// </example> | ||
| /// <seealso cref="IUnion" /> | ||
| [AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct, AllowMultiple = false, Inherited = true)] | ||
|
eiriktsarpalis marked this conversation as resolved.
Resolved
|
||
| public class UnionAttribute : Attribute | ||
|
eiriktsarpalis marked this conversation as resolved.
Resolved
eiriktsarpalis marked this conversation as resolved.
Resolved
|
||
| { | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.