Skip to content

Answer generic variance and parameter attributes in reflection as .NET does - #593

Open
jonathanperis wants to merge 4 commits into
mainfrom
fix/reflection-variance
Open

jonathanperis wants to merge 4 commits into
mainfrom
fix/reflection-variance

Conversation

@jonathanperis

@jonathanperis jonathanperis commented Dec 3, 2026 •

Copy link
Copy Markdown
Owner

Fixes #579. Fixes #580.

What was wrong (silent wrong output).

  • Type.GenericParameterAttributes answered 0 for every type parameter, source and framework.
  • IsAssignableFrom, IsAssignableTo and IsInstanceOfType ignored 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 a csrt_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 explicit NotSupportedException).

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_TypeInfo now records variance with an offset for nested generic interfaces (delegates/interfaces inside types), capped at 16 two-bit slots via new VarianceBits. Type parameters get full GenericParameterAttributes (covariance/contravariance, class/struct/new(), ref struct). Enum boxing tables key on instantiated nested enums (TypeSubstitution.EraseTupleNames) and use shared DescribedName for CLR-style type names on method tables.

Runtime (type.c): variant_compatible consults embedded variance for arguments 0–15 and parameter_variance on the generic definition for argument 16+ so IsAssignableFrom / IsInstanceOfType work 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.

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.
@jonathanperis jonathanperis added this to the Drop-in for large apps milestone Dec 3, 2026
@jonathanperis jonathanperis added bug Something isn't working area: bcl Library surface (corelib) area: runtime csrt: collector, threads, exceptions, OS boundary labels Dec 3, 2026
@jonathanperis jonathanperis self-assigned this Dec 3, 2026

@cursor cursor Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes using high effort and found 1 potential issue.

Fix All in Cursor

❌ 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.

Comment thread src/runtime/type.c
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;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)
Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit 6f19ea6. Configure here.

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: bcl Library surface (corelib) area: runtime csrt: collector, threads, exceptions, OS boundary bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Type.IsAssignableFrom ignores variance of some interfaces declared by the program (nested ones) GenericParameterAttributes is always None

1 participant