Skip to content

Support C# union types - #476

Open
pixsperdavid wants to merge 3 commits into
Cysharp:mainfrom
seismiq:main
Open

pixsperdavid wants to merge 3 commits into
Cysharp:mainfrom
seismiq:main

Conversation

@pixsperdavid

@pixsperdavid pixsperdavid commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

Adds support for C# 15 union declarations:

[MemoryPackable]
public partial union Pet(Cat, Dog, int?, string);

Value is written as a union header followed by the case value, using the existing union header encoding. An empty union (Value is null) is written as null, and reads back as default.

Tags: by default, a case’s tag is its zero-based position in the declaration, so new cases must be added at the end. Tags can also be set explicitly with [MemoryPackUnion(tag, typeof(Case))]. When any are given, every case needs exactly one (MEMPACK044). To allow this, MemoryPackUnionAttribute can now be applied to structs. Using it on a [MemoryPackable] struct that isn’t a union is an error (MEMPACK045).

Notes:

Layout options: GenerateType and SerializeLayout options have no effect on unions.
Overlapping cases: the first matching case in declaration order is used.
Generic case types: cases like T can’t be tagged explicitly, because C# doesn’t allow typeof(T) in an attribute.
Union tests: the tests target net11.0, but only when the .NET 11 SDK is used to build them. CI will now install 11.0.x.
Diagnostic tests: errors that need union syntax have no generator diagnostic tests yet. The test harness uses Roslyn 4.4.0, which can’t parse it.

pixsperdavid and others added 2 commits November 10, 2026 18:41
[MemoryPackable] partial union declarations serialize Value as a union
header plus the case value. Tags default to the case's declaration
position and can be set with [MemoryPackUnion(tag, typeof(Case))].
Union diagnostics renumbered to MEMPACK044/045 as MEMPACK043 is now used upstream.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@pixsperdavid pixsperdavid mentioned this pull request Nov 11, 2026
@cysharp-github-actions-bot

Copy link
Copy Markdown
Contributor

Only Cysharp organization members and Dependabot are permitted to change .github/ under our security policy. This PR is being closed.

@pixsperdavid

Copy link
Copy Markdown
Contributor Author

The change to .github is necessary to allow the unit tests to function.

@pixsperdavid

Copy link
Copy Markdown
Contributor Author

@neuecc Could I request that this automated decision be reviewed?

@neuecc neuecc reopened this Nov 12, 2026
@neuecc

neuecc commented Nov 12, 2026

Copy link
Copy Markdown
Member

I checked it and it looks fine, so I reopened it.
Changes to Actions are usually not needed, and they can be an entry point for security problems, so we handle them conservatively.
Thank you for the PR.
I will check it later.

@neuecc

neuecc commented Nov 13, 2026

Copy link
Copy Markdown
Member

@pixsperdavid Could you remove the changes under .github/ (build-debug.yaml and build-release.yaml) from this PR?

Our policy bot reacts to any .github/ change coming from outside the organization, and I can't remove them myself because maintainer edits are not available for a fork owned by an organization.

Workflow changes have to be made by Cysharp members, so the .NET 11 SDK setup for CI will be handled separately on our side.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@pixsperdavid

Copy link
Copy Markdown
Contributor Author

I've reverted the workflow changes, everything will still compile however the new functionality will be untested until the tests are compiled and run with .NET 11.

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.

2 participants