Repository navigation
Answer generic variance and parameter attributes in reflection as .NET does - #593
jonathanperis wants to merge 4 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.
…T does Type.GenericParameterAttributes was 0 for every type parameter; it is computed as the compiler writes it in metadata (variance, class/struct/new() constraints, allows ref struct). Type.IsAssignableFrom and its relatives ignored the variance of interfaces and delegates nested in a type, and of type parameters past the 16 whose 2-bit fields a csrt_TypeInfo holds: nested types record theirs after their declaring types' arguments, and the runtime asks the definition's parameters about arguments past 16.
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes using high effort and found 1 potential issue.
❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.
Reviewed by Cursor Bugbot for commit 6f19ea6. Configure here.
| static uint32_t parameter_variance(const csrt_TypeInfo* info, uint32_t k) { | ||
| const csrt_TypeInfo* definition = info->definition ? lookup_info(info->definition) : NULL; | ||
| const csrt_TypeInfo* parameter = definition && k < definition->generic_count ? lookup_info(definition->generic_args[k]) : NULL; | ||
| return parameter && (parameter->flags & CSRT_TI_GENERIC_PARAMETER) ? parameter->variance & 3 : 0; |
There was a problem hiding this comment.
Enclosing variance flips after sixteen
Medium Severity
VarianceBits packs only a nested type's own parameters and leaves enclosing arguments invariant. Past the 16th argument, parameter_variance instead uses that chain slot's real variance, including an enclosing in or out parameter. IsAssignableFrom then treats the same enclosing parameter differently on either side of the boundary.
Additional Locations (2)
Reviewed by Cursor Bugbot for commit 6f19ea6. Configure here.


Fixes #579. Fixes #580.
What was wrong (silent wrong output).
Type.GenericParameterAttributesanswered 0 for every type parameter, source and framework.IsAssignableFrom,IsAssignableToandIsInstanceOfTypeignored the variance of interfaces and delegates nested in a type (also in a generic type), and of type parameters past the 16th.What changed. A generic parameter's entry records the attributes the compiler writes in metadata (variance,
class/struct/new(),allows ref struct). Nested types record their variance after their declaring types' arguments; past the 16 arguments whose 2-bit fields fit acsrt_TypeInfo, the runtime asks the definition's own parameters. No table grows: both answers use fields the entries have.GetGenericParameterConstraints()stays the explicit "not supported" diagnostic it was, and a generic method definition's type parameters stay undescribed (an explicitNotSupportedException).Tests. Sample 1850, recorded from .NET: top-level, nested, in a generic class, 16, 17 and 18 parameters, framework interfaces, nested variance, value-type arguments, delegates, definitions, constructed types that appear only in
typeof; parameter attributes and positions of 14 definitions; -O0, -O2, via IL and wasm32. A differential audit of 940 reflection answers against NativeAOT found no other difference in relations, generic parameters or positions (pointer assignability is in #589). 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).Stack. This branch contains #592 (merge that one first; then this PR's merge gives the tree CI ran on).
Note
Medium Risk
Changes core reflection assignability and enum/type identity in metadata and runtime; broad behavior surface but aligned with CLR rules and covered by new samples.
Overview
Fixes reflection and runtime type relations so they match .NET for generic variance,
GenericParameterAttributes, and enums nested in generic types.Compiler / metadata:
csrt_TypeInfonow records variance with an offset for nested generic interfaces (delegates/interfaces inside types), capped at 16 two-bit slots via newVarianceBits. Type parameters get fullGenericParameterAttributes(covariance/contravariance, class/struct/new(), ref struct). Enum boxing tables key on instantiated nested enums (TypeSubstitution.EraseTupleNames) and use sharedDescribedNamefor CLR-style type names on method tables.Runtime (
type.c):variant_compatibleconsults embedded variance for arguments 0–15 andparameter_varianceon the generic definition for argument 16+ soIsAssignableFrom/IsInstanceOfTypework for long parameter lists and nested variance.Tests: Sample 1850 (variance + parameter attributes vs .NET) and 1851 (per-instantiation nested enum identity, boxing, casts, reflection).
Reviewed by Cursor Bugbot for commit 9eacdcc. Bugbot is set up for automated code reviews on this repo. Configure here.