Repository navigation
Make an enum nested in a generic type one type per instantiation - #592
Open
jonathanperis wants to merge 2 commits into
Open
jonathanperis wants to merge 2 commits into
jonathanperis wants to merge 2 commits into
Conversation
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.
This was referenced Dec 4, 2026
This branch has not been deployed
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
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)wasTrue, a boxedOuter<int>.Ewas anOuter<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 ofOriginalDefinition, soOuter<int>.KindandOuter<string>.Kindget distinct runtime tables and boxed instances no longer interoperate across instantiations.Table metadata names use a new
DescribedNamehelper (CLR name, with generic parameter names for open definitions), shared by enum boxing tables and type-only method tables fortypeof.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.