Skip to content

Make an enum nested in a generic type one type per instantiation - #592

Open
jonathanperis wants to merge 2 commits into
mainfrom
fix/nested-generic-enum
Open

jonathanperis wants to merge 2 commits into
mainfrom
fix/nested-generic-enum

Conversation

@jonathanperis

@jonathanperis jonathanperis commented Dec 3, 2026 •

Copy link
Copy Markdown
Owner

Fixes #587.

What was wrong (silent wrong output). Enum method tables were keyed by the enum's definition, so an enum nested in a generic type was one type for all instantiations: typeof(Outer<int>.E) == typeof(Outer<string>.E) was True, a boxed Outer<int>.E was an Outer<string>.E (is, as, patterns, Equals), and reflection showed the open definition.

What changed. The table is keyed by the constructed type (tuple names erased); the formatting code stays shared by the definition. The definition's table is named with its parameters, as other generic definitions'.

Tests. Sample 1851, recorded from .NET: identity, boxing, is/as/casts/unboxing (to another instantiation and to the underlying type), patterns, generic code, arrays, dictionary and set keys, comparers, formatting, parsing, reflection; -O0, -O2, via IL and wasm32. The full local -O2 suite runs on the integration tree of this PR with #582 and the other reflection fixes (commits below, once it has run).


Note

Medium Risk
Changes core runtime type identity for nested generic enums, affecting boxing, casts, and reflection across all compiled programs.

Overview
Fixes incorrect type identity for enums nested inside generic types, where the compiler previously collapsed all instantiations to one enum definition’s method table—breaking typeof, boxing, is/as, casts, and reflection relative to .NET.

Enum method tables are now keyed by the constructed enum type (with tuple names erased via TypeSubstitution.EraseTupleNames) instead of OriginalDefinition, so Outer<int>.Kind and Outer<string>.Kind get distinct runtime tables and boxed instances no longer interoperate across instantiations.

Table metadata names use a new DescribedName helper (CLR name, with generic parameter names for open definitions), shared by enum boxing tables and type-only method tables for typeof.

A new integration sample 1851 records expected behavior for identity, boxing, patterns, collections, formatting, parsing, and reflection across generic nesting depths.

Reviewed by Cursor Bugbot for commit 4d378d2. Bugbot is set up for automated code reviews on this repo. Configure here.

Enum method tables were keyed by the enum's definition, so typeof(Outer<int>.E) == typeof(Outer<string>.E) was true, a boxed
Outer<int>.E was an Outer<string>.E for is, as, patterns and Equals, and reflection showed the open definition (its name,
arguments and IsGenericTypeDefinition). They are keyed by the constructed type now; the definition's table carries its
parameters' names, as other generic definitions' tables do.
@jonathanperis jonathanperis added this to the Drop-in for large apps milestone Dec 3, 2026
@jonathanperis jonathanperis added bug Something isn't working area: codegen Lowering and code generation labels Dec 3, 2026
@jonathanperis jonathanperis self-assigned this Dec 3, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: codegen Lowering and code generation bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Enums nested in generic types lose their type arguments: instantiations are one type

1 participant