Repository navigation
Describe by-ref types in reflection - #594
jonathanperis wants to merge 7 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.
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.
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes using high effort and found 2 potential issues.
❌ 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.
| 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); |
There was a problem hiding this comment.
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)
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; |
There was a problem hiding this comment.
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)
Reviewed by Cursor Bugbot for commit 76a2214. Configure here.


Fixes #588.
What was wrong (silent wrong output). By-ref parameter, return and property types were described by their element type:
ParameterType,ReturnTypeandPropertyTypeansweredSystem.Int32forint&,IsByRefwas always false,MethodInfo.ToString()printedVoid Ref(Int32), andMakeByRefType()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, andNullabilityInfoContext.Createof a by-ref parameter no longer throws.Invokethrough a by-ref parameter or return stays the explicitNotSupportedExceptionit was.Tests. Sample 1852, recorded from the PublishAot build (NativeAOT differs from the JIT in two answers, see the
.aotfile): 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
Typeper 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 throughSignatureType/WithoutByRef, implementsType.MakeByRefType()andType.GetType("…&"), wiresType.IsByRef, fixesMethodInfo.ToString()forByRefparameters, and allowsNullabilityInfoContext.Createon ref parameters.Type.IsAssignableFromtreats 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 intype.c), andGenericParameterAttributesare 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.