Skip to content

Add polyfills for C# 15 union types (UnionAttribute and IUnion)#140

Open
AndiJingzhiTse wants to merge 1 commit into
Sergio0694:mainfrom
AndiJingzhiTse:dev/union-types-polyfill
Open

Add polyfills for C# 15 union types (UnionAttribute and IUnion)#140
AndiJingzhiTse wants to merge 1 commit into
Sergio0694:mainfrom
AndiJingzhiTse:dev/union-types-polyfill

Conversation

@AndiJingzhiTse
Copy link
Copy Markdown

Summary

Closes #139

This PR adds source-only polyfills for the two types required by the C# 15 union types feature, enabling their use on downlevel targets before .NET 11 ships these types in the BCL:

  • [Union] (System.Runtime.CompilerServices.UnionAttribute) — attribute recognized by the C# 15 compiler to identify a type as a union type
  • IUnion (System.Runtime.CompilerServices.IUnion) — interface that union types implement, providing access to the underlying value via object? Value { get; }

Both are placed in the main EmbeddedResources/ folder (language support polyfills, enabled by default) and follow the existing conventions: // <auto-generated/> header, internal accessibility (switchable to public via PolySharpUsePublicAccessibilityForGeneratedTypes), and [ExcludeFromCodeCoverage] on the attribute class.

References

  • C# 15 union types blog post
  • The blog post notes: "in .NET 11 Preview 2, the UnionAttribute and IUnion interface aren't included in the runtime yet. You must declare them in your project."

Test plan

  • Added TestUnion class in tests/PolySharp.Tests/LanguageFeatures.cs that applies [Union] and implements IUnion, exercising both new polyfills
  • Build passes: dotnet build — 0 warnings, 0 errors
  • All tests pass across all target frameworks (net472, net48, net481, netstandard2.0, netstandard2.1, net8.0, net9.0)

🤖 Generated with Claude Code

Adds source-only polyfills for the UnionAttribute and IUnion interface
from System.Runtime.CompilerServices, enabling C# 15 union type features
on downlevel targets before .NET 11 ships these types in the BCL.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@AndiJingzhiTse AndiJingzhiTse marked this pull request as draft May 2, 2026 16:56
@AndiJingzhiTse AndiJingzhiTse marked this pull request as ready for review May 2, 2026 17:17
@AndiJingzhiTse AndiJingzhiTse force-pushed the dev/union-types-polyfill branch from 73fd312 to d27001b Compare May 2, 2026 20:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Polyfill for union types

1 participant