Repository navigation
Add polyfills for C# 15 union types (UnionAttribute and IUnion) #140
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
Sergio0694
merged 5 commits into
Sergio0694:main
from
AndiJingzhiTse:dev/union-types-polyfill
Oct 26, 2026
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
a50c811
Add polyfills for C# 15 union types (UnionAttribute and IUnion)
AndiJingzhiTse 97a397d
Update src/PolySharp.SourceGenerators/EmbeddedResources/System.Runtim…
AndiJingzhiTse 25c4da4
Update README.md
AndiJingzhiTse 8fd413e
Fix union type polyfills
AndiJingzhiTse 8191892
Improve union polyfill documentation and test coverage
Sergio0694 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
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
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
31 changes: 31 additions & 0 deletions
31
...rceGenerators/EmbeddedResources/LanguageSupport/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,31 @@ | ||
| // <auto-generated/> | ||
| #pragma warning disable | ||
| #nullable enable annotations | ||
|
|
||
| // 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> | ||
| /// The C# compiler automatically implements this interface on types generated by union declarations. | ||
| /// User-defined types annotated with <see cref="global::System.Runtime.CompilerServices.UnionAttribute"/> may also implement this interface | ||
| /// to provide runtime access to the union's value. Implementing this interface is not required | ||
| /// for union behaviors provided by the compiler. | ||
| /// </remarks> | ||
| /// <seealso cref="global::System.Runtime.CompilerServices.UnionAttribute"/> | ||
| [global::Microsoft.CodeAnalysis.Embedded] | ||
| internal 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> | ||
| object? Value { get; } | ||
| } | ||
| } |
29 changes: 29 additions & 0 deletions
29
...ators/EmbeddedResources/LanguageSupport/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,29 @@ | ||
| // <auto-generated/> | ||
| #pragma warning disable | ||
| #nullable enable annotations | ||
|
|
||
| // 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> | ||
| /// Any class or struct annotated with this attribute is recognized by the C# compiler as a union type. | ||
| /// Union types may support behaviors such as implicit conversions from case types, pattern matching | ||
| /// that unwraps the union's contents, and switch exhaustiveness checking. | ||
| /// </remarks> | ||
| /// <seealso cref="global::System.Runtime.CompilerServices.IUnion"/> | ||
| [global::System.AttributeUsage( | ||
| global::System.AttributeTargets.Class | | ||
| global::System.AttributeTargets.Struct, | ||
| AllowMultiple = false, | ||
| Inherited = false)] | ||
| [global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage] | ||
| [global::Microsoft.CodeAnalysis.Embedded] | ||
| internal sealed class UnionAttribute : global::System.Attribute | ||
| { | ||
| } | ||
| } | ||
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.