Skip to content

Describe by-ref types in reflection - #594

Open
jonathanperis wants to merge 7 commits into
mainfrom
fix/reflection-byref-types
Open

jonathanperis wants to merge 7 commits into
mainfrom
fix/reflection-byref-types

Conversation

@jonathanperis

@jonathanperis jonathanperis commented Dec 4, 2026 •

Copy link
Copy Markdown
Owner

Fixes #588.

What was wrong (silent wrong output). By-ref parameter, return and property types were described by their element type: ParameterType, ReturnType and PropertyType answered System.Int32 for int&, IsByRef was always false, MethodInfo.ToString() printed Void Ref(Int32), and MakeByRefType() was an explicit error, so an overload could not be selected by it.

What changed. The runtime makes a by-ref type per element the first time it is asked for (type.c, as synthesized arrays), so programs carry no static data for them. Method and property records say when a return or a property is by-ref (free bits of their flags; parameters did already). Type.GetType("System.Int32&") finds them too, and NullabilityInfoContext.Create of a by-ref parameter no longer throws.

Invoke through a by-ref parameter or return stays the explicit NotSupportedException it was.

Tests. Sample 1852, recorded from the PublishAot build (NativeAOT differs from the JIT in two answers, see the .aot file): ref, out, in, ref readonly and by-ref returns/properties/indexers of primitives, classes, structs, enums, arrays and generic types, a delegate's Invoke, generic classes, framework methods, texts, identity, assignability, overload selection; -O0, -O2, via IL. The full local -O2 suite runs on the integration tree (the top of this stack).

Stack. This branch contains #592 and #593.


Note

Medium Risk
Changes span compiler metadata, C runtime type synthesis, and reflection APIs used by binding and serializers; incorrect by-ref or variance handling could misreport types or break overload resolution at runtime.

Overview
Reflection now exposes by-ref types correctly instead of silently using element types. The runtime synthesizes a Type per element on first use (csrt_type_make_byref, CSRT_MT_BYREF / CSRT_TI_BYREF), so the compiler only records by-ref flags on method returns (CSRT_METHOD_BYREF_RETURN), properties (CSRT_PROPERTY_BYREF), and parameters (existing bit). Corelib maps those records through SignatureType / WithoutByRef, implements Type.MakeByRefType() and Type.GetType("…&"), wires Type.IsByRef, fixes MethodInfo.ToString() for ByRef parameters, and allows NullabilityInfoContext.Create on ref parameters. Type.IsAssignableFrom treats matching by-ref pairs like the CLR.

On the same branch: generic variance metadata is fixed via VarianceBits (nested and >16 parameters fall back to definition generic parameters in type.c), and GenericParameterAttributes are emitted for reflection. Enums nested in generic types get distinct boxed method tables per instantiation (DescribedName, tuple-erased keys) instead of collapsing to the enum definition.

New sample tests cover by-ref reflection (1852), variance (1850), and generic nested enums (1851).

Reviewed by Cursor Bugbot for commit 76a2214. 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.
ref, out, in and ref readonly parameters, by-ref returns and properties were described by their element type: ParameterType,
ReturnType and PropertyType answered System.Int32 for int&, IsByRef was always false, MethodInfo.ToString() printed Int32, and
MakeByRefType() was not supported, so overloads could not be selected by it. The runtime makes a by-ref type per element the
first time it is asked for (no static data), and the records say which returns and properties are by-ref (parameters did already).
Invoking through a by-ref parameter stays the explicit NotSupportedException it was.
@jonathanperis jonathanperis added this to the Drop-in for large apps milestone Dec 4, 2026
@jonathanperis jonathanperis added bug Something isn't working area: bcl Library surface (corelib) area: runtime csrt: collector, threads, exceptions, OS boundary labels Dec 4, 2026
@jonathanperis jonathanperis self-assigned this Dec 4, 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 2 potential issues.

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 76a2214. Configure here.

Comment thread src/runtime/type.c
if (to->flags & CSRT_TI_GENERIC_DEFINITION) return false; // nothing derives from an open definition
if (from->flags & CSRT_TI_POINTER) return false;
if ((to->flags | from->flags) & CSRT_TI_BYREF)
return (to->flags & from->flags & CSRT_TI_BYREF) && element_compatible(to->element, from->element);

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.

By-ref assignability uses array rules

Medium Severity

By-ref IsAssignableFrom goes through element_compatible, the array-covariance helper. A by-ref of int is therefore treated as assignable from a by-ref of uint, and an enum by-ref from its underlying type, because they share an element storage class. Reference by-refs such as object& and string& happen to agree with element assignability, so the sample does not catch this.

Additional Locations (1)
Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit 76a2214. Configure here.

var variance = 0;
for (var k = 0; k < definition.TypeParameters.Length && offset + k < 16; k++)
variance |= definition.TypeParameters[k].Variance switch { VarianceKind.Out => 1, VarianceKind.In => 2, _ => 0 } << (2 * (offset + k));
return variance;

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.

Nested variance drops outer parameters

Medium Severity

VarianceBits only packs definition.TypeParameters, which are the nested type's own parameters. The offset skips enclosing arguments in the chain, but those slots stay zero, so a variant parameter on an enclosing interface or delegate is treated as invariant. IsAssignableFrom then rejects conversions that those outer parameters allow.

Additional Locations (1)
Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit 76a2214. 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.

Reflection loses by-ref types of parameters, returns and properties

1 participant