From 4d9056e7f8207da56bbd82366a34bde52d906edc Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Wed, 28 Jan 2026 23:23:36 +0000 Subject: [PATCH 01/10] Initial plan From cae838cd21d4cd64ab6b44d6cae65a3512bf4104 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Wed, 28 Jan 2026 23:31:16 +0000 Subject: [PATCH 02/10] Migrate WithRefField, WithRefStructField, and WithTypedReferenceField to C# Co-authored-by: jkoritzinsky <1571408+jkoritzinsky@users.noreply.github.com> --- .../classloader/RefFields/InvalidCSharp.il | 110 ------------------ .../Loader/classloader/RefFields/Validate.cs | 53 +++++++++ 2 files changed, 53 insertions(+), 110 deletions(-) diff --git a/src/tests/Loader/classloader/RefFields/InvalidCSharp.il b/src/tests/Loader/classloader/RefFields/InvalidCSharp.il index eceebcde61f829..507e6abcc4fc33 100644 --- a/src/tests/Loader/classloader/RefFields/InvalidCSharp.il +++ b/src/tests/Loader/classloader/RefFields/InvalidCSharp.il @@ -74,113 +74,3 @@ // This field's valid Type would illegally overlap this type's first field using a precise GC. .field [0] public valuetype InvalidCSharp.InnerValidByRef Invalid } - -.class public sequential ansi sealed beforefieldinit InvalidCSharp.WithRefField - extends [System.Runtime]System.ValueType -{ - .custom instance void [System.Runtime]System.Runtime.CompilerServices.IsByRefLikeAttribute::.ctor() = ( - 01 00 00 00 - ) - .field public string& Str - - .method public hidebysig specialname rtspecialname - instance void .ctor ( - string& - ) cil managed - { - ldarg.0 - ldarg.1 - stfld string& InvalidCSharp.WithRefField::Str - ret - } - - .method public hidebysig - instance bool ConfirmFieldInstance ( - string - ) cil managed - { - ldarg.0 - ldfld string& InvalidCSharp.WithRefField::Str - ldind.ref - ldarg.1 - ceq - ret - } -} - -.class public sequential ansi sealed beforefieldinit InvalidCSharp.WithRefStructField - extends [System.Runtime]System.ValueType -{ - .custom instance void [System.Runtime]System.Runtime.CompilerServices.IsByRefLikeAttribute::.ctor() = ( - 01 00 00 00 - ) - .field public valuetype InvalidCSharp.WithRefField& Field - - .method public hidebysig specialname rtspecialname - instance void .ctor ( - valuetype InvalidCSharp.WithRefField& - ) cil managed - { - ldarg.0 - ldarg.1 - stfld valuetype InvalidCSharp.WithRefField& InvalidCSharp.WithRefStructField::Field - ret - } - - .method public hidebysig - instance bool ConfirmFieldInstance ( - valuetype InvalidCSharp.WithRefField& - ) cil managed - { - ldarg.0 - ldfld valuetype InvalidCSharp.WithRefField& InvalidCSharp.WithRefStructField::Field - ldarg.1 - ceq - ret - } -} - -.class public sequential ansi sealed beforefieldinit InvalidCSharp.WithTypedReferenceField`1 - extends [System.Runtime]System.ValueType -{ - .custom instance void [System.Runtime]System.Runtime.CompilerServices.IsByRefLikeAttribute::.ctor() = ( - 01 00 00 00 - ) - .field public typedref Field - - .method public hidebysig specialname rtspecialname - instance void .ctor ( - !T& - ) cil managed - { - ldarg.0 - ldarg.1 - mkrefany !T - stfld typedref valuetype InvalidCSharp.WithTypedReferenceField`1::Field - ret - } - - .method public hidebysig - instance class [System.Runtime]System.Type GetFieldType () cil managed - { - ldarg.0 - ldfld typedref valuetype InvalidCSharp.WithTypedReferenceField`1::Field - refanytype - call class [System.Runtime]System.Type [System.Runtime]System.Type::GetTypeFromHandle(valuetype [System.Runtime]System.RuntimeTypeHandle ) - ret - } - - .method public hidebysig - instance bool ConfirmFieldInstance ( - !T - ) cil managed - { - ldarg.0 - ldfld typedref valuetype InvalidCSharp.WithTypedReferenceField`1::Field - refanyval !T - ldind.ref - ldarg.1 - ceq - ret - } -} diff --git a/src/tests/Loader/classloader/RefFields/Validate.cs b/src/tests/Loader/classloader/RefFields/Validate.cs index 65d9d36f8df742..32f4305f5f6976 100644 --- a/src/tests/Loader/classloader/RefFields/Validate.cs +++ b/src/tests/Loader/classloader/RefFields/Validate.cs @@ -129,4 +129,57 @@ public static void Validate_Create_TypedReferenceRefField() Validate v = new(); Create_TypedReferenceRefField_Worker(v, 1); } +} + +namespace InvalidCSharp +{ + public ref struct WithRefField + { + public ref string Str; + + public WithRefField(ref string str) + { + Str = ref str; + } + + public bool ConfirmFieldInstance(string value) + { + return object.ReferenceEquals(Str, value); + } + } + + public ref struct WithRefStructField + { + public ref WithRefField Field; + + public WithRefStructField(ref WithRefField field) + { + Field = ref field; + } + + public bool ConfirmFieldInstance(ref WithRefField field) + { + return Unsafe.AreSame(ref Field, ref field); + } + } + + public ref struct WithTypedReferenceField + { + public TypedReference Field; + + public WithTypedReferenceField(ref T value) + { + Field = __makeref(value); + } + + public Type GetFieldType() + { + return __reftype(Field); + } + + public bool ConfirmFieldInstance(T value) + { + return object.ReferenceEquals(__refvalue(Field, T), value); + } + } } \ No newline at end of file From 152986803cf0348adc1bef5c678eda28dfcc5f06 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Wed, 28 Jan 2026 23:31:51 +0000 Subject: [PATCH 03/10] Migrate SpanArr ref struct inline array to C# Co-authored-by: jkoritzinsky <1571408+jkoritzinsky@users.noreply.github.com> --- .../InlineArray/InlineArrayValid.cs | 18 +++++++ .../InlineArray/InvalidCSharpInlineArray.il | 47 ------------------- 2 files changed, 18 insertions(+), 47 deletions(-) diff --git a/src/tests/Loader/classloader/InlineArray/InlineArrayValid.cs b/src/tests/Loader/classloader/InlineArray/InlineArrayValid.cs index d21e0bd9c0078f..e47395d0e9bbbf 100644 --- a/src/tests/Loader/classloader/InlineArray/InlineArrayValid.cs +++ b/src/tests/Loader/classloader/InlineArray/InlineArrayValid.cs @@ -448,3 +448,21 @@ public static void InlineArrayEqualsGetHashCode_Fails() }); } } + +[InlineArray(LengthConst)] +public ref struct SpanArr +{ + private const int LengthConst = 100; + public Span element; + + public int Length => LengthConst; + + [UnscopedRef] + public unsafe Span* At(int index) + { + fixed (Span* ptr = &element) + { + return ptr + index; + } + } +} diff --git a/src/tests/Loader/classloader/InlineArray/InvalidCSharpInlineArray.il b/src/tests/Loader/classloader/InlineArray/InvalidCSharpInlineArray.il index ca71640f41fc37..ebb3ab4571334a 100644 --- a/src/tests/Loader/classloader/InlineArray/InvalidCSharpInlineArray.il +++ b/src/tests/Loader/classloader/InlineArray/InvalidCSharpInlineArray.il @@ -85,50 +85,3 @@ .field private int32 a .field private int32 b } - -.class public sequential ansi sealed beforefieldinit SpanArr - extends [System.Runtime]System.ValueType -{ - .custom instance void [System.Runtime]System.Runtime.CompilerServices.IsByRefLikeAttribute::.ctor() = ( - 01 00 00 00 - ) - .custom instance void [System.Runtime]System.Runtime.CompilerServices.InlineArrayAttribute::.ctor(int32) = ( - 01 00 64 00 00 00 00 00 - ) - - .field private static literal int32 LengthConst = int32(100) - .field public valuetype [System.Runtime]System.Span`1 element - - .method public hidebysig - instance valuetype [System.Runtime]System.Span`1* At ( - int32 - ) cil managed - { - .locals init ( - [0] valuetype [System.Runtime]System.Span`1& pinned - ) - ldarg.0 - ldflda valuetype [System.Runtime]System.Span`1 SpanArr::element - stloc.0 - ldloc.0 - conv.u - ldarg.1 - conv.i - sizeof valuetype [System.Runtime]System.Span`1 - mul - add - ret - } - - .method public hidebysig specialname - instance int32 get_Length () cil managed - { - ldc.i4.s 100 - ret - } - - .property instance int32 Length() - { - .get instance int32 SpanArr::get_Length() - } -} From 359a1bb230b0ada558e09aa2b98fd6b56effa321 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Wed, 28 Jan 2026 23:34:46 +0000 Subject: [PATCH 04/10] Migrate ByRefLike helper types and generic structs to C# Co-authored-by: jkoritzinsky <1571408+jkoritzinsky@users.noreply.github.com> --- .../generics/ByRefLike/InvalidCSharp.il | 88 ------- .../generics/ByRefLike/Validate.cs | 219 ++++++++++++++++++ 2 files changed, 219 insertions(+), 88 deletions(-) diff --git a/src/tests/Loader/classloader/generics/ByRefLike/InvalidCSharp.il b/src/tests/Loader/classloader/generics/ByRefLike/InvalidCSharp.il index 1bfb367c6d0006..c29c27abe665ac 100644 --- a/src/tests/Loader/classloader/generics/ByRefLike/InvalidCSharp.il +++ b/src/tests/Loader/classloader/generics/ByRefLike/InvalidCSharp.il @@ -435,94 +435,6 @@ } } -.class public sequential ansi sealed beforefieldinit ByRefLikeType - extends [System.Runtime]System.ValueType -{ - .custom instance void [System.Runtime]System.Runtime.CompilerServices.IsByRefLikeAttribute::.ctor() = ( - 01 00 00 00 - ) -} - -.class public sequential ansi sealed beforefieldinit ByRefLikeType2 - extends [System.Runtime]System.ValueType -{ - .custom instance void [System.Runtime]System.Runtime.CompilerServices.IsByRefLikeAttribute::.ctor() = ( - 01 00 00 00 - ) -} - -.class public sequential ansi sealed beforefieldinit ByRefLikeType`1 - extends [System.Runtime]System.ValueType -{ - .custom instance void [System.Runtime]System.Runtime.CompilerServices.IsByRefLikeAttribute::.ctor() = ( - 01 00 00 00 - ) -} - -.class interface public auto ansi abstract InvalidCSharp.SimpleInterface -{ - .method public hidebysig newslot abstract virtual - instance int32 Method() cil managed - { - } -} - -.class public sequential ansi sealed beforefieldinit ByRefLikeTypeWithInterface - extends [System.Runtime]System.ValueType - implements InvalidCSharp.SimpleInterface -{ - .custom instance void [System.Runtime]System.Runtime.CompilerServices.IsByRefLikeAttribute::.ctor() = ( - 01 00 00 00 - ) - - .method public final hidebysig newslot virtual - instance int32 Method() cil managed - { - ldc.i4.1 - ret - } -} - -.class interface public auto ansi abstract InvalidCSharp.DefaultInterface -{ - .method public hidebysig newslot virtual - instance int32 Method() cil managed - { - ldc.i4.0 - ret - } -} - -.class public sequential ansi sealed beforefieldinit RS_DI1 - extends [System.Runtime]System.ValueType - implements InvalidCSharp.DefaultInterface -{ - .custom instance void [System.Runtime]System.Runtime.CompilerServices.IsByRefLikeAttribute::.ctor() = ( - 01 00 00 00 - ) -} - -.class public sequential ansi sealed beforefieldinit RS_DI2 - extends [System.Runtime]System.ValueType - implements InvalidCSharp.DefaultInterface -{ - .custom instance void [System.Runtime]System.Runtime.CompilerServices.IsByRefLikeAttribute::.ctor() = ( - 01 00 00 00 - ) - - .method public hidebysig newslot virtual - instance int32 Method() cil managed - { - ldc.i4.1 - ret - } -} - -.class public sequential ansi sealed beforefieldinit RegularValueType - extends [System.Runtime]System.ValueType -{ -} - .class public auto ansi beforefieldinit ClassWithInterface extends [System.Runtime]System.Object implements InvalidCSharp.SimpleInterface diff --git a/src/tests/Loader/classloader/generics/ByRefLike/Validate.cs b/src/tests/Loader/classloader/generics/ByRefLike/Validate.cs index ced2fda3c95dbb..02f60f7e59ea1b 100644 --- a/src/tests/Loader/classloader/generics/ByRefLike/Validate.cs +++ b/src/tests/Loader/classloader/generics/ByRefLike/Validate.cs @@ -185,3 +185,222 @@ public static void Validate_MemberDiscoveryViaReflection_ForSpanReadOnlySpan() // } } } + +namespace InvalidCSharp +{ + // Simple ref struct types used for testing + public ref struct ByRefLikeType + { + } + + public ref struct ByRefLikeType2 + { + } + + public ref struct ByRefLikeType + { + } + + public interface SimpleInterface + { + int Method(); + } + + public ref struct ByRefLikeTypeWithInterface : SimpleInterface + { + public int Method() => 1; + } + + public interface DefaultInterface + { + int Method() => 0; + } + + public ref struct RS_DI1 : DefaultInterface + { + } + + public ref struct RS_DI2 : DefaultInterface + { + public int Method() => 1; + } + + public struct RegularValueType + { + } + + // Generic value type with allows ref struct constraint + public struct GenericValueType_Over where T : allows ref struct + { + } + + // Generic ref struct with allows ref struct constraint + public ref struct GenericByRefLike_Over where T : allows ref struct + { + public T Field; + + public T CreateDefaultInstance() + { + return default(T); + } + + public Type GetGenericType() + { + return typeof(T); + } + + public bool BoxUnboxAny(T value) + { + object? boxed = value; + T unboxed = (T)boxed!; + return true; + } + + public bool BoxBranch(T value) + { + object? boxed1 = value; + if (boxed1 == null) { } + + object? boxed2 = value; + if (boxed2 == null) { } + + object? boxed3 = value; + if (boxed3 != null) { } + + object? boxed4 = value; + if (boxed4 != null) { } + + return true; + } + + public bool BoxBranchToOther(T value) where U : allows ref struct + { + // This method boxes U, not T - it's testing generic method constraints + U defaultU = default(U); + object? boxed1 = defaultU; + if (boxed1 == null) { } + + object? boxed2 = defaultU; + if (boxed2 == null) { } + + object? boxed3 = defaultU; + if (boxed3 != null) { } + + object? boxed4 = defaultU; + if (boxed4 != null) { } + + return true; + } + + public bool BoxBranch_WithSideEffects(ref T value) + { + T copy1 = value; + object? boxed1 = copy1; + if (boxed1 == null) { } + + T copy2 = value; + object? boxed2 = copy2; + if (boxed2 == null) { } + + T copy3 = value; + object? boxed3 = copy3; + if (boxed3 != null) { } + + T copy4 = value; + object? boxed4 = copy4; + if (boxed4 != null) { } + + return true; + } + + public bool BoxIsinstUnboxAny(T value) + { + object? boxed = value; + if (boxed is ByRefLikeType) + { + ByRefLikeType unboxed = (ByRefLikeType)boxed; + } + return true; + } + + public bool BoxIsinstUnboxAny_Mismatch(T value) + { + object? boxed = value; + if (boxed is ByRefLikeType2) + { + ByRefLikeType2 unboxed = (ByRefLikeType2)boxed; + } + return false; + } + + public bool BoxIsinstBranch(T value) + { + object? boxed1 = value; + if (boxed1 is not ByRefLikeType) { } + + object? boxed2 = value; + if (boxed2 is not ByRefLikeType) { } + + object? boxed3 = value; + if (boxed3 is ByRefLikeType) { } + + object? boxed4 = value; + if (boxed4 is ByRefLikeType) { } + + return true; + } + + public bool BoxIsinstBranch_WithSideEffects(ref T value) + { + T copy1 = value; + object? boxed1 = copy1; + if (boxed1 is not ByRefLikeType) { } + + T copy2 = value; + object? boxed2 = copy2; + if (boxed2 is not ByRefLikeType) { } + + T copy3 = value; + object? boxed3 = copy3; + if (boxed3 is ByRefLikeType) { } + + T copy4 = value; + object? boxed4 = copy4; + if (boxed4 is ByRefLikeType) { } + + return true; + } + + public bool BoxIsinstBranch_CheckForSimpleInterface(U value) where U : allows ref struct + { + if (value is not SimpleInterface) + { + throw new Exception("All types should implement SimpleInterface"); + } + + SimpleInterface iface = (SimpleInterface)(object)value!; + return iface.Method() != 0; + } + + public bool AllocArrayOfT() + { + T[] array = new T[1]; + return array != null; + } + + public bool InstanceOfT(object o) + { + return o is T; + } + + public void CastToT(object o) + { + T value = (T)o; + } + + public void UnboxToT(object o) + { + T value = (T)o; + } + } +} From 3940ebef26e05cbd08825c95ba838685face4b98 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Wed, 28 Jan 2026 23:35:52 +0000 Subject: [PATCH 05/10] Complete removal of migrated generic types from IL Co-authored-by: jkoritzinsky <1571408+jkoritzinsky@users.noreply.github.com> --- .../generics/ByRefLike/InvalidCSharp.il | 336 ------------------ 1 file changed, 336 deletions(-) diff --git a/src/tests/Loader/classloader/generics/ByRefLike/InvalidCSharp.il b/src/tests/Loader/classloader/generics/ByRefLike/InvalidCSharp.il index c29c27abe665ac..d2f0d5878a8000 100644 --- a/src/tests/Loader/classloader/generics/ByRefLike/InvalidCSharp.il +++ b/src/tests/Loader/classloader/generics/ByRefLike/InvalidCSharp.il @@ -59,342 +59,6 @@ { } -.class public sequential ansi sealed beforefieldinit InvalidCSharp.GenericValueType_Over`1 - extends [System.Runtime]System.ValueType -{ -} - -.class public sequential ansi sealed beforefieldinit InvalidCSharp.GenericByRefLike_Over`1 - extends [System.Runtime]System.ValueType -{ - .custom instance void [System.Runtime]System.Runtime.CompilerServices.IsByRefLikeAttribute::.ctor() = ( - 01 00 00 00 - ) - - .field public !T Field - - .method public hidebysig - instance !T CreateDefaultInstance() cil managed - { - .locals init ( - [0] !T - ) - ldloca.s 0 - initobj !T - ldloc.0 - ret - } - - .method public hidebysig - instance class [System.Runtime]System.Type GetGenericType() cil managed - { - ldtoken !T - call class [System.Runtime]System.Type [System.Runtime]System.Type::GetTypeFromHandle(valuetype [System.Runtime]System.RuntimeTypeHandle) - ret - } - - .method public hidebysig - instance bool BoxUnboxAny(!T) cil managed - { - ldarg.1 - // Begin sequence - box !T - unbox.any !T - // End sequence - pop - ldc.i4.1 - ret - } - - .method public hidebysig - instance bool BoxBranch(!T) cil managed - { - ldarg.1 - // Begin sequence - box !T - brfalse.s NEXT_1 - // End sequence - NEXT_1: - - ldarg.1 - // Begin sequence - box !T - brfalse NEXT_2 - // End sequence - NEXT_2: - - ldarg.1 - // Begin sequence - box !T - brtrue.s NEXT_3 - // End sequence - NEXT_3: - - ldarg.1 - // Begin sequence - box !T - brtrue NEXT_4 - // End sequence - NEXT_4: - - ldc.i4.1 - ret - } - - .method public hidebysig - instance bool BoxBranchToOther(!T) cil managed - { - ldarg.1 - // Begin sequence - box !!U - brfalse.s NEXT_1 - // End sequence - NEXT_1: - - ldarg.1 - // Begin sequence - box !!U - brfalse NEXT_2 - // End sequence - NEXT_2: - - ldarg.1 - // Begin sequence - box !!U - brtrue.s NEXT_3 - // End sequence - NEXT_3: - - ldarg.1 - // Begin sequence - box !!U - brtrue NEXT_4 - // End sequence - NEXT_4: - - ldc.i4.1 - ret - } - - .method public hidebysig - instance bool BoxBranch_WithSideEffects(!T&) cil managed - { - ldarg.1 - // Begin sequence - ldobj !T // Side-effect - box !T - brfalse.s NEXT_1 - // End sequence - NEXT_1: - - ldarg.1 - // Begin sequence - ldobj !T // Side-effect - box !T - brfalse NEXT_2 - // End sequence - NEXT_2: - - ldarg.1 - // Begin sequence - ldobj !T // Side-effect - box !T - brtrue.s NEXT_3 - // End sequence - NEXT_3: - - ldarg.1 - // Begin sequence - ldobj !T // Side-effect - box !T - brtrue NEXT_4 - // End sequence - NEXT_4: - - ldc.i4.1 - ret - } - - .method public hidebysig - instance bool BoxIsinstUnboxAny(!T) cil managed - { - ldarg.1 - // Begin sequence - box !T - isinst ByRefLikeType - unbox.any ByRefLikeType - // End sequence - pop - ldc.i4.1 - ret - } - - .method public hidebysig - instance bool BoxIsinstUnboxAny_Mismatch(!T) cil managed - { - ldarg.1 - // Begin sequence - box !T - isinst ByRefLikeType2 - unbox.any ByRefLikeType2 - // End sequence - pop - ldc.i4.0 - ret - } - - .method public hidebysig - instance bool BoxIsinstBranch(!T) cil managed - { - ldarg.1 - // Begin sequence - box !T - isinst ByRefLikeType - brfalse.s NEXT_1 - // End sequence - NEXT_1: - - ldarg.1 - // Begin sequence - box !T - isinst ByRefLikeType - brfalse NEXT_2 - // End sequence - NEXT_2: - - ldarg.1 - // Begin sequence - box !T - isinst ByRefLikeType - brtrue.s NEXT_3 - // End sequence - NEXT_3: - - ldarg.1 - // Begin sequence - box !T - isinst ByRefLikeType - brtrue NEXT_4 - // End sequence - NEXT_4: - - ldc.i4.1 - ret - } - - .method public hidebysig - instance bool BoxIsinstBranch_WithSideEffects(!T&) cil managed - { - ldarg.1 - // Begin sequence - ldobj !T // Side-effect - box !T - isinst ByRefLikeType - brfalse.s NEXT_1 - // End sequence - NEXT_1: - - ldarg.1 - // Begin sequence - ldobj !T // Side-effect - box !T - isinst ByRefLikeType - brfalse NEXT_2 - // End sequence - NEXT_2: - - ldarg.1 - // Begin sequence - ldobj !T // Side-effect - box !T - isinst ByRefLikeType - brtrue.s NEXT_3 - // End sequence - NEXT_3: - - ldarg.1 - // Begin sequence - ldobj !T // Side-effect - box !T - isinst ByRefLikeType - brtrue NEXT_4 - // End sequence - NEXT_4: - - ldc.i4.1 - ret - } - - .method public hidebysig - instance bool BoxIsinstBranch_CheckForSimpleInterface(!!U) cil managed - { - .locals init ( - [0] !!U - ) - - ldarg.1 - box !!U - isinst InvalidCSharp.SimpleInterface - brtrue.s IS_SIMPLEINTERFACE - - ldstr "All types should implement SimpleInterface" - newobj instance void [System.Runtime]System.Exception::.ctor(string) - throw - - IS_SIMPLEINTERFACE: - ldarg.1 - stloc.0 - ldloca.s 0 - constrained. !!U - callvirt instance int32 InvalidCSharp.SimpleInterface::Method() - ret - } - - .method public hidebysig - instance bool AllocArrayOfT() cil managed aggressiveinlining - { - ldc.i4.1 - newarr !T - ldnull - cgt.un - ret - } - - .method public hidebysig - instance bool InstanceOfT( - object o - ) cil managed - { - ldarg.1 - isinst !T - ldnull - cgt.un - ret - } - - .method public hidebysig - instance void CastToT( - object o - ) cil managed - { - ldarg.1 - castclass !T - pop - ret - } - - .method public hidebysig - instance void UnboxToT( - object o - ) cil managed - { - ldarg.1 - unbox.any !T - pop - ret - } -} - .class public sequential ansi sealed beforefieldinit InvalidCSharp.GenericClass_WithStaticField`1 extends [System.Runtime]System.Object { From ab1d3f10e27c828b43ee3fbb704f94c21c3c0a2b Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Thu, 29 Jan 2026 00:05:05 +0000 Subject: [PATCH 06/10] Move migrated types out of InvalidCSharp namespace Co-authored-by: jkoritzinsky <1571408+jkoritzinsky@users.noreply.github.com> --- .../Loader/classloader/RefFields/Validate.cs | 71 ++-- .../generics/ByRefLike/Validate.cs | 353 +++++++++--------- 2 files changed, 209 insertions(+), 215 deletions(-) diff --git a/src/tests/Loader/classloader/RefFields/Validate.cs b/src/tests/Loader/classloader/RefFields/Validate.cs index 32f4305f5f6976..b7b82dd972f43a 100644 --- a/src/tests/Loader/classloader/RefFields/Validate.cs +++ b/src/tests/Loader/classloader/RefFields/Validate.cs @@ -131,55 +131,52 @@ public static void Validate_Create_TypedReferenceRefField() } } -namespace InvalidCSharp +public ref struct WithRefField { - public ref struct WithRefField - { - public ref string Str; - - public WithRefField(ref string str) - { - Str = ref str; - } + public ref string Str; - public bool ConfirmFieldInstance(string value) - { - return object.ReferenceEquals(Str, value); - } + public WithRefField(ref string str) + { + Str = ref str; } - public ref struct WithRefStructField + public bool ConfirmFieldInstance(string value) { - public ref WithRefField Field; + return object.ReferenceEquals(Str, value); + } +} - public WithRefStructField(ref WithRefField field) - { - Field = ref field; - } +public ref struct WithRefStructField +{ + public ref WithRefField Field; - public bool ConfirmFieldInstance(ref WithRefField field) - { - return Unsafe.AreSame(ref Field, ref field); - } + public WithRefStructField(ref WithRefField field) + { + Field = ref field; } - public ref struct WithTypedReferenceField + public bool ConfirmFieldInstance(ref WithRefField field) { - public TypedReference Field; + return Unsafe.AreSame(ref Field, ref field); + } +} - public WithTypedReferenceField(ref T value) - { - Field = __makeref(value); - } +public ref struct WithTypedReferenceField +{ + public TypedReference Field; - public Type GetFieldType() - { - return __reftype(Field); - } + public WithTypedReferenceField(ref T value) + { + Field = __makeref(value); + } - public bool ConfirmFieldInstance(T value) - { - return object.ReferenceEquals(__refvalue(Field, T), value); - } + public Type GetFieldType() + { + return __reftype(Field); + } + + public bool ConfirmFieldInstance(T value) + { + return object.ReferenceEquals(__refvalue(Field, T), value); } } \ No newline at end of file diff --git a/src/tests/Loader/classloader/generics/ByRefLike/Validate.cs b/src/tests/Loader/classloader/generics/ByRefLike/Validate.cs index 02f60f7e59ea1b..2f42b9ecc40c7b 100644 --- a/src/tests/Loader/classloader/generics/ByRefLike/Validate.cs +++ b/src/tests/Loader/classloader/generics/ByRefLike/Validate.cs @@ -186,221 +186,218 @@ public static void Validate_MemberDiscoveryViaReflection_ForSpanReadOnlySpan() } } -namespace InvalidCSharp +// Simple ref struct types used for testing +public ref struct ByRefLikeType { - // Simple ref struct types used for testing - public ref struct ByRefLikeType - { - } +} - public ref struct ByRefLikeType2 - { - } +public ref struct ByRefLikeType2 +{ +} - public ref struct ByRefLikeType - { - } +public ref struct ByRefLikeType +{ +} - public interface SimpleInterface - { - int Method(); - } +public interface SimpleInterface +{ + int Method(); +} + +public ref struct ByRefLikeTypeWithInterface : SimpleInterface +{ + public int Method() => 1; +} + +public interface DefaultInterface +{ + int Method() => 0; +} - public ref struct ByRefLikeTypeWithInterface : SimpleInterface +public ref struct RS_DI1 : DefaultInterface +{ +} + +public ref struct RS_DI2 : DefaultInterface +{ + public int Method() => 1; +} + +public struct RegularValueType +{ +} + +// Generic value type with allows ref struct constraint +public struct GenericValueType_Over where T : allows ref struct +{ +} + +// Generic ref struct with allows ref struct constraint +public ref struct GenericByRefLike_Over where T : allows ref struct +{ + public T Field; + + public T CreateDefaultInstance() { - public int Method() => 1; + return default(T); } - public interface DefaultInterface + public Type GetGenericType() { - int Method() => 0; + return typeof(T); } - public ref struct RS_DI1 : DefaultInterface + public bool BoxUnboxAny(T value) { + object? boxed = value; + T unboxed = (T)boxed!; + return true; } - public ref struct RS_DI2 : DefaultInterface + public bool BoxBranch(T value) { - public int Method() => 1; + object? boxed1 = value; + if (boxed1 == null) { } + + object? boxed2 = value; + if (boxed2 == null) { } + + object? boxed3 = value; + if (boxed3 != null) { } + + object? boxed4 = value; + if (boxed4 != null) { } + + return true; } - public struct RegularValueType + public bool BoxBranchToOther(T value) where U : allows ref struct { + // This method boxes U, not T - it's testing generic method constraints + U defaultU = default(U); + object? boxed1 = defaultU; + if (boxed1 == null) { } + + object? boxed2 = defaultU; + if (boxed2 == null) { } + + object? boxed3 = defaultU; + if (boxed3 != null) { } + + object? boxed4 = defaultU; + if (boxed4 != null) { } + + return true; } - // Generic value type with allows ref struct constraint - public struct GenericValueType_Over where T : allows ref struct + public bool BoxBranch_WithSideEffects(ref T value) { + T copy1 = value; + object? boxed1 = copy1; + if (boxed1 == null) { } + + T copy2 = value; + object? boxed2 = copy2; + if (boxed2 == null) { } + + T copy3 = value; + object? boxed3 = copy3; + if (boxed3 != null) { } + + T copy4 = value; + object? boxed4 = copy4; + if (boxed4 != null) { } + + return true; } - // Generic ref struct with allows ref struct constraint - public ref struct GenericByRefLike_Over where T : allows ref struct + public bool BoxIsinstUnboxAny(T value) { - public T Field; - - public T CreateDefaultInstance() - { - return default(T); - } - - public Type GetGenericType() - { - return typeof(T); - } - - public bool BoxUnboxAny(T value) - { - object? boxed = value; - T unboxed = (T)boxed!; - return true; - } - - public bool BoxBranch(T value) + object? boxed = value; + if (boxed is ByRefLikeType) { - object? boxed1 = value; - if (boxed1 == null) { } - - object? boxed2 = value; - if (boxed2 == null) { } - - object? boxed3 = value; - if (boxed3 != null) { } - - object? boxed4 = value; - if (boxed4 != null) { } - - return true; - } - - public bool BoxBranchToOther(T value) where U : allows ref struct - { - // This method boxes U, not T - it's testing generic method constraints - U defaultU = default(U); - object? boxed1 = defaultU; - if (boxed1 == null) { } - - object? boxed2 = defaultU; - if (boxed2 == null) { } - - object? boxed3 = defaultU; - if (boxed3 != null) { } - - object? boxed4 = defaultU; - if (boxed4 != null) { } - - return true; - } - - public bool BoxBranch_WithSideEffects(ref T value) - { - T copy1 = value; - object? boxed1 = copy1; - if (boxed1 == null) { } - - T copy2 = value; - object? boxed2 = copy2; - if (boxed2 == null) { } - - T copy3 = value; - object? boxed3 = copy3; - if (boxed3 != null) { } - - T copy4 = value; - object? boxed4 = copy4; - if (boxed4 != null) { } - - return true; - } - - public bool BoxIsinstUnboxAny(T value) - { - object? boxed = value; - if (boxed is ByRefLikeType) - { - ByRefLikeType unboxed = (ByRefLikeType)boxed; - } - return true; + ByRefLikeType unboxed = (ByRefLikeType)boxed; } + return true; + } - public bool BoxIsinstUnboxAny_Mismatch(T value) + public bool BoxIsinstUnboxAny_Mismatch(T value) + { + object? boxed = value; + if (boxed is ByRefLikeType2) { - object? boxed = value; - if (boxed is ByRefLikeType2) - { - ByRefLikeType2 unboxed = (ByRefLikeType2)boxed; - } - return false; + ByRefLikeType2 unboxed = (ByRefLikeType2)boxed; } + return false; + } - public bool BoxIsinstBranch(T value) - { - object? boxed1 = value; - if (boxed1 is not ByRefLikeType) { } - - object? boxed2 = value; - if (boxed2 is not ByRefLikeType) { } - - object? boxed3 = value; - if (boxed3 is ByRefLikeType) { } - - object? boxed4 = value; - if (boxed4 is ByRefLikeType) { } - - return true; - } + public bool BoxIsinstBranch(T value) + { + object? boxed1 = value; + if (boxed1 is not ByRefLikeType) { } + + object? boxed2 = value; + if (boxed2 is not ByRefLikeType) { } + + object? boxed3 = value; + if (boxed3 is ByRefLikeType) { } + + object? boxed4 = value; + if (boxed4 is ByRefLikeType) { } + + return true; + } - public bool BoxIsinstBranch_WithSideEffects(ref T value) - { - T copy1 = value; - object? boxed1 = copy1; - if (boxed1 is not ByRefLikeType) { } - - T copy2 = value; - object? boxed2 = copy2; - if (boxed2 is not ByRefLikeType) { } - - T copy3 = value; - object? boxed3 = copy3; - if (boxed3 is ByRefLikeType) { } - - T copy4 = value; - object? boxed4 = copy4; - if (boxed4 is ByRefLikeType) { } - - return true; - } + public bool BoxIsinstBranch_WithSideEffects(ref T value) + { + T copy1 = value; + object? boxed1 = copy1; + if (boxed1 is not ByRefLikeType) { } + + T copy2 = value; + object? boxed2 = copy2; + if (boxed2 is not ByRefLikeType) { } + + T copy3 = value; + object? boxed3 = copy3; + if (boxed3 is ByRefLikeType) { } + + T copy4 = value; + object? boxed4 = copy4; + if (boxed4 is ByRefLikeType) { } + + return true; + } - public bool BoxIsinstBranch_CheckForSimpleInterface(U value) where U : allows ref struct + public bool BoxIsinstBranch_CheckForSimpleInterface(U value) where U : allows ref struct + { + if (value is not SimpleInterface) { - if (value is not SimpleInterface) - { - throw new Exception("All types should implement SimpleInterface"); - } - - SimpleInterface iface = (SimpleInterface)(object)value!; - return iface.Method() != 0; + throw new Exception("All types should implement SimpleInterface"); } + + SimpleInterface iface = (SimpleInterface)(object)value!; + return iface.Method() != 0; + } - public bool AllocArrayOfT() - { - T[] array = new T[1]; - return array != null; - } + public bool AllocArrayOfT() + { + T[] array = new T[1]; + return array != null; + } - public bool InstanceOfT(object o) - { - return o is T; - } + public bool InstanceOfT(object o) + { + return o is T; + } - public void CastToT(object o) - { - T value = (T)o; - } + public void CastToT(object o) + { + T value = (T)o; + } - public void UnboxToT(object o) - { - T value = (T)o; - } + public void UnboxToT(object o) + { + T value = (T)o; } } From 93d4d2e1b9dacc7a85a83104e4e6998ba980f37b Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Tue, 17 Feb 2026 20:13:32 +0000 Subject: [PATCH 07/10] Move GenericValueType_Over, GenericByRefLike_Over, and interfaces back to IL Co-authored-by: jkoritzinsky <1571408+jkoritzinsky@users.noreply.github.com> --- .../generics/ByRefLike/InvalidCSharp.il | 354 ++++++++++++++++++ .../generics/ByRefLike/Validate.cs | 191 +--------- 2 files changed, 357 insertions(+), 188 deletions(-) diff --git a/src/tests/Loader/classloader/generics/ByRefLike/InvalidCSharp.il b/src/tests/Loader/classloader/generics/ByRefLike/InvalidCSharp.il index d2f0d5878a8000..a06ea91e9d5cda 100644 --- a/src/tests/Loader/classloader/generics/ByRefLike/InvalidCSharp.il +++ b/src/tests/Loader/classloader/generics/ByRefLike/InvalidCSharp.il @@ -59,6 +59,360 @@ { } +.class public sequential ansi sealed beforefieldinit InvalidCSharp.GenericValueType_Over`1 + extends [System.Runtime]System.ValueType +{ +} + +.class public sequential ansi sealed beforefieldinit InvalidCSharp.GenericByRefLike_Over`1 + extends [System.Runtime]System.ValueType +{ + .custom instance void [System.Runtime]System.Runtime.CompilerServices.IsByRefLikeAttribute::.ctor() = ( + 01 00 00 00 + ) + + .field public !T Field + + .method public hidebysig + instance !T CreateDefaultInstance() cil managed + { + .locals init ( + [0] !T + ) + ldloca.s 0 + initobj !T + ldloc.0 + ret + } + + .method public hidebysig + instance class [System.Runtime]System.Type GetGenericType() cil managed + { + ldtoken !T + call class [System.Runtime]System.Type [System.Runtime]System.Type::GetTypeFromHandle(valuetype [System.Runtime]System.RuntimeTypeHandle) + ret + } + + .method public hidebysig + instance bool BoxUnboxAny(!T) cil managed + { + ldarg.1 + // Begin sequence + box !T + unbox.any !T + // End sequence + pop + ldc.i4.1 + ret + } + + .method public hidebysig + instance bool BoxBranch(!T) cil managed + { + ldarg.1 + // Begin sequence + box !T + brfalse.s NEXT_1 + // End sequence + NEXT_1: + + ldarg.1 + // Begin sequence + box !T + brfalse NEXT_2 + // End sequence + NEXT_2: + + ldarg.1 + // Begin sequence + box !T + brtrue.s NEXT_3 + // End sequence + NEXT_3: + + ldarg.1 + // Begin sequence + box !T + brtrue NEXT_4 + // End sequence + NEXT_4: + + ldc.i4.1 + ret + } + + .method public hidebysig + instance bool BoxBranchToOther(!T) cil managed + { + ldarg.1 + // Begin sequence + box !!U + brfalse.s NEXT_1 + // End sequence + NEXT_1: + + ldarg.1 + // Begin sequence + box !!U + brfalse NEXT_2 + // End sequence + NEXT_2: + + ldarg.1 + // Begin sequence + box !!U + brtrue.s NEXT_3 + // End sequence + NEXT_3: + + ldarg.1 + // Begin sequence + box !!U + brtrue NEXT_4 + // End sequence + NEXT_4: + + ldc.i4.1 + ret + } + + .method public hidebysig + instance bool BoxBranch_WithSideEffects(!T&) cil managed + { + ldarg.1 + // Begin sequence + ldobj !T // Side-effect + box !T + brfalse.s NEXT_1 + // End sequence + NEXT_1: + + ldarg.1 + // Begin sequence + ldobj !T // Side-effect + box !T + brfalse NEXT_2 + // End sequence + NEXT_2: + + ldarg.1 + // Begin sequence + ldobj !T // Side-effect + box !T + brtrue.s NEXT_3 + // End sequence + NEXT_3: + + ldarg.1 + // Begin sequence + ldobj !T // Side-effect + box !T + brtrue NEXT_4 + // End sequence + NEXT_4: + + ldc.i4.1 + ret + } + + .method public hidebysig + instance bool BoxIsinstUnboxAny(!T) cil managed + { + ldarg.1 + // Begin sequence + box !T + isinst ByRefLikeType + unbox.any ByRefLikeType + // End sequence + pop + ldc.i4.1 + ret + } + + .method public hidebysig + instance bool BoxIsinstUnboxAny_Mismatch(!T) cil managed + { + ldarg.1 + // Begin sequence + box !T + isinst ByRefLikeType2 + unbox.any ByRefLikeType2 + // End sequence + pop + ldc.i4.0 + ret + } + + .method public hidebysig + instance bool BoxIsinstBranch(!T) cil managed + { + ldarg.1 + // Begin sequence + box !T + isinst ByRefLikeType + brfalse.s NEXT_1 + // End sequence + NEXT_1: + + ldarg.1 + // Begin sequence + box !T + isinst ByRefLikeType + brfalse NEXT_2 + // End sequence + NEXT_2: + + ldarg.1 + // Begin sequence + box !T + isinst ByRefLikeType + brtrue.s NEXT_3 + // End sequence + NEXT_3: + + ldarg.1 + // Begin sequence + box !T + isinst ByRefLikeType + brtrue NEXT_4 + // End sequence + NEXT_4: + + ldc.i4.1 + ret + } + + .method public hidebysig + instance bool BoxIsinstBranch_WithSideEffects(!T&) cil managed + { + ldarg.1 + // Begin sequence + ldobj !T // Side-effect + box !T + isinst ByRefLikeType + brfalse.s NEXT_1 + // End sequence + NEXT_1: + + ldarg.1 + // Begin sequence + ldobj !T // Side-effect + box !T + isinst ByRefLikeType + brfalse NEXT_2 + // End sequence + NEXT_2: + + ldarg.1 + // Begin sequence + ldobj !T // Side-effect + box !T + isinst ByRefLikeType + brtrue.s NEXT_3 + // End sequence + NEXT_3: + + ldarg.1 + // Begin sequence + ldobj !T // Side-effect + box !T + isinst ByRefLikeType + brtrue NEXT_4 + // End sequence + NEXT_4: + + ldc.i4.1 + ret + } + + .method public hidebysig + instance bool BoxIsinstBranch_CheckForSimpleInterface(!!U) cil managed + { + .locals init ( + [0] !!U + ) + + ldarg.1 + box !!U + isinst InvalidCSharp.SimpleInterface + brtrue.s IS_SIMPLEINTERFACE + + ldstr "All types should implement SimpleInterface" + newobj instance void [System.Runtime]System.Exception::.ctor(string) + throw + + IS_SIMPLEINTERFACE: + ldarg.1 + stloc.0 + ldloca.s 0 + constrained. !!U + callvirt instance int32 InvalidCSharp.SimpleInterface::Method() + ret + } + + .method public hidebysig + instance bool AllocArrayOfT() cil managed aggressiveinlining + { + ldc.i4.1 + newarr !T + ldnull + cgt.un + ret + } + + .method public hidebysig + instance bool InstanceOfT( + object o + ) cil managed + { + ldarg.1 + isinst !T + ldnull + cgt.un + ret + } + + .method public hidebysig + instance void CastToT( + object o + ) cil managed + { + ldarg.1 + castclass !T + pop + ret + } + + .method public hidebysig + instance void UnboxToT( + object o + ) cil managed + { + ldarg.1 + unbox.any !T + pop + ret + } +} + +.class interface public auto ansi abstract InvalidCSharp.SimpleInterface +{ + .method public hidebysig newslot abstract virtual + instance int32 Method() cil managed + { + } +} + +.class interface public auto ansi abstract InvalidCSharp.DefaultInterface +{ + .method public hidebysig newslot virtual + instance int32 Method() cil managed + { + ldc.i4.0 + ret + } +} + .class public sequential ansi sealed beforefieldinit InvalidCSharp.GenericClass_WithStaticField`1 extends [System.Runtime]System.Object { diff --git a/src/tests/Loader/classloader/generics/ByRefLike/Validate.cs b/src/tests/Loader/classloader/generics/ByRefLike/Validate.cs index 2f42b9ecc40c7b..f2c1a29a65e465 100644 --- a/src/tests/Loader/classloader/generics/ByRefLike/Validate.cs +++ b/src/tests/Loader/classloader/generics/ByRefLike/Validate.cs @@ -199,26 +199,16 @@ public ref struct ByRefLikeType { } -public interface SimpleInterface -{ - int Method(); -} - -public ref struct ByRefLikeTypeWithInterface : SimpleInterface +public ref struct ByRefLikeTypeWithInterface : InvalidCSharp.SimpleInterface { public int Method() => 1; } -public interface DefaultInterface -{ - int Method() => 0; -} - -public ref struct RS_DI1 : DefaultInterface +public ref struct RS_DI1 : InvalidCSharp.DefaultInterface { } -public ref struct RS_DI2 : DefaultInterface +public ref struct RS_DI2 : InvalidCSharp.DefaultInterface { public int Method() => 1; } @@ -226,178 +216,3 @@ public interface DefaultInterface public struct RegularValueType { } - -// Generic value type with allows ref struct constraint -public struct GenericValueType_Over where T : allows ref struct -{ -} - -// Generic ref struct with allows ref struct constraint -public ref struct GenericByRefLike_Over where T : allows ref struct -{ - public T Field; - - public T CreateDefaultInstance() - { - return default(T); - } - - public Type GetGenericType() - { - return typeof(T); - } - - public bool BoxUnboxAny(T value) - { - object? boxed = value; - T unboxed = (T)boxed!; - return true; - } - - public bool BoxBranch(T value) - { - object? boxed1 = value; - if (boxed1 == null) { } - - object? boxed2 = value; - if (boxed2 == null) { } - - object? boxed3 = value; - if (boxed3 != null) { } - - object? boxed4 = value; - if (boxed4 != null) { } - - return true; - } - - public bool BoxBranchToOther(T value) where U : allows ref struct - { - // This method boxes U, not T - it's testing generic method constraints - U defaultU = default(U); - object? boxed1 = defaultU; - if (boxed1 == null) { } - - object? boxed2 = defaultU; - if (boxed2 == null) { } - - object? boxed3 = defaultU; - if (boxed3 != null) { } - - object? boxed4 = defaultU; - if (boxed4 != null) { } - - return true; - } - - public bool BoxBranch_WithSideEffects(ref T value) - { - T copy1 = value; - object? boxed1 = copy1; - if (boxed1 == null) { } - - T copy2 = value; - object? boxed2 = copy2; - if (boxed2 == null) { } - - T copy3 = value; - object? boxed3 = copy3; - if (boxed3 != null) { } - - T copy4 = value; - object? boxed4 = copy4; - if (boxed4 != null) { } - - return true; - } - - public bool BoxIsinstUnboxAny(T value) - { - object? boxed = value; - if (boxed is ByRefLikeType) - { - ByRefLikeType unboxed = (ByRefLikeType)boxed; - } - return true; - } - - public bool BoxIsinstUnboxAny_Mismatch(T value) - { - object? boxed = value; - if (boxed is ByRefLikeType2) - { - ByRefLikeType2 unboxed = (ByRefLikeType2)boxed; - } - return false; - } - - public bool BoxIsinstBranch(T value) - { - object? boxed1 = value; - if (boxed1 is not ByRefLikeType) { } - - object? boxed2 = value; - if (boxed2 is not ByRefLikeType) { } - - object? boxed3 = value; - if (boxed3 is ByRefLikeType) { } - - object? boxed4 = value; - if (boxed4 is ByRefLikeType) { } - - return true; - } - - public bool BoxIsinstBranch_WithSideEffects(ref T value) - { - T copy1 = value; - object? boxed1 = copy1; - if (boxed1 is not ByRefLikeType) { } - - T copy2 = value; - object? boxed2 = copy2; - if (boxed2 is not ByRefLikeType) { } - - T copy3 = value; - object? boxed3 = copy3; - if (boxed3 is ByRefLikeType) { } - - T copy4 = value; - object? boxed4 = copy4; - if (boxed4 is ByRefLikeType) { } - - return true; - } - - public bool BoxIsinstBranch_CheckForSimpleInterface(U value) where U : allows ref struct - { - if (value is not SimpleInterface) - { - throw new Exception("All types should implement SimpleInterface"); - } - - SimpleInterface iface = (SimpleInterface)(object)value!; - return iface.Method() != 0; - } - - public bool AllocArrayOfT() - { - T[] array = new T[1]; - return array != null; - } - - public bool InstanceOfT(object o) - { - return o is T; - } - - public void CastToT(object o) - { - T value = (T)o; - } - - public void UnboxToT(object o) - { - T value = (T)o; - } -} From 67bb6f52156a61f084a3de97dd03a5bba995f0a4 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Fri, 20 Feb 2026 20:03:38 +0000 Subject: [PATCH 08/10] Move helper types back to IL to fix build errors Co-authored-by: jkoritzinsky <1571408+jkoritzinsky@users.noreply.github.com> --- .../generics/ByRefLike/InvalidCSharp.il | 62 +++++++++++++++++++ .../generics/ByRefLike/Validate.cs | 31 ---------- 2 files changed, 62 insertions(+), 31 deletions(-) diff --git a/src/tests/Loader/classloader/generics/ByRefLike/InvalidCSharp.il b/src/tests/Loader/classloader/generics/ByRefLike/InvalidCSharp.il index a06ea91e9d5cda..f5ca7973396e66 100644 --- a/src/tests/Loader/classloader/generics/ByRefLike/InvalidCSharp.il +++ b/src/tests/Loader/classloader/generics/ByRefLike/InvalidCSharp.il @@ -413,6 +413,68 @@ } } +.class public sequential ansi sealed beforefieldinit ByRefLikeType + extends [System.Runtime]System.ValueType +{ + .custom instance void [System.Runtime]System.Runtime.CompilerServices.IsByRefLikeAttribute::.ctor() = ( + 01 00 00 00 + ) +} + +.class public sequential ansi sealed beforefieldinit ByRefLikeType2 + extends [System.Runtime]System.ValueType +{ + .custom instance void [System.Runtime]System.Runtime.CompilerServices.IsByRefLikeAttribute::.ctor() = ( + 01 00 00 00 + ) +} + +.class public sequential ansi sealed beforefieldinit ByRefLikeTypeWithInterface + extends [System.Runtime]System.ValueType + implements InvalidCSharp.SimpleInterface +{ + .custom instance void [System.Runtime]System.Runtime.CompilerServices.IsByRefLikeAttribute::.ctor() = ( + 01 00 00 00 + ) + + .method public final hidebysig newslot virtual + instance int32 Method() cil managed + { + ldc.i4.1 + ret + } +} + +.class public sequential ansi sealed beforefieldinit RegularValueType + extends [System.Runtime]System.ValueType +{ +} + +.class public sequential ansi sealed beforefieldinit RS_DI1 + extends [System.Runtime]System.ValueType + implements InvalidCSharp.DefaultInterface +{ + .custom instance void [System.Runtime]System.Runtime.CompilerServices.IsByRefLikeAttribute::.ctor() = ( + 01 00 00 00 + ) +} + +.class public sequential ansi sealed beforefieldinit RS_DI2 + extends [System.Runtime]System.ValueType + implements InvalidCSharp.DefaultInterface +{ + .custom instance void [System.Runtime]System.Runtime.CompilerServices.IsByRefLikeAttribute::.ctor() = ( + 01 00 00 00 + ) + + .method public hidebysig newslot virtual + instance int32 Method() cil managed + { + ldc.i4.1 + ret + } +} + .class public sequential ansi sealed beforefieldinit InvalidCSharp.GenericClass_WithStaticField`1 extends [System.Runtime]System.Object { diff --git a/src/tests/Loader/classloader/generics/ByRefLike/Validate.cs b/src/tests/Loader/classloader/generics/ByRefLike/Validate.cs index f2c1a29a65e465..ced2fda3c95dbb 100644 --- a/src/tests/Loader/classloader/generics/ByRefLike/Validate.cs +++ b/src/tests/Loader/classloader/generics/ByRefLike/Validate.cs @@ -185,34 +185,3 @@ public static void Validate_MemberDiscoveryViaReflection_ForSpanReadOnlySpan() // } } } - -// Simple ref struct types used for testing -public ref struct ByRefLikeType -{ -} - -public ref struct ByRefLikeType2 -{ -} - -public ref struct ByRefLikeType -{ -} - -public ref struct ByRefLikeTypeWithInterface : InvalidCSharp.SimpleInterface -{ - public int Method() => 1; -} - -public ref struct RS_DI1 : InvalidCSharp.DefaultInterface -{ -} - -public ref struct RS_DI2 : InvalidCSharp.DefaultInterface -{ - public int Method() => 1; -} - -public struct RegularValueType -{ -} From 1d70f6b229c7b15e48d3db8082d31df86f7d18f6 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Fri, 20 Feb 2026 23:50:44 +0000 Subject: [PATCH 09/10] Move RefFields types back to IL - ref to ref struct and TypedReference not valid in C# Co-authored-by: jkoritzinsky <1571408+jkoritzinsky@users.noreply.github.com> --- .../classloader/RefFields/InvalidCSharp.il | 110 ++++++++++++++++++ .../Loader/classloader/RefFields/Validate.cs | 50 -------- 2 files changed, 110 insertions(+), 50 deletions(-) diff --git a/src/tests/Loader/classloader/RefFields/InvalidCSharp.il b/src/tests/Loader/classloader/RefFields/InvalidCSharp.il index 507e6abcc4fc33..eceebcde61f829 100644 --- a/src/tests/Loader/classloader/RefFields/InvalidCSharp.il +++ b/src/tests/Loader/classloader/RefFields/InvalidCSharp.il @@ -74,3 +74,113 @@ // This field's valid Type would illegally overlap this type's first field using a precise GC. .field [0] public valuetype InvalidCSharp.InnerValidByRef Invalid } + +.class public sequential ansi sealed beforefieldinit InvalidCSharp.WithRefField + extends [System.Runtime]System.ValueType +{ + .custom instance void [System.Runtime]System.Runtime.CompilerServices.IsByRefLikeAttribute::.ctor() = ( + 01 00 00 00 + ) + .field public string& Str + + .method public hidebysig specialname rtspecialname + instance void .ctor ( + string& + ) cil managed + { + ldarg.0 + ldarg.1 + stfld string& InvalidCSharp.WithRefField::Str + ret + } + + .method public hidebysig + instance bool ConfirmFieldInstance ( + string + ) cil managed + { + ldarg.0 + ldfld string& InvalidCSharp.WithRefField::Str + ldind.ref + ldarg.1 + ceq + ret + } +} + +.class public sequential ansi sealed beforefieldinit InvalidCSharp.WithRefStructField + extends [System.Runtime]System.ValueType +{ + .custom instance void [System.Runtime]System.Runtime.CompilerServices.IsByRefLikeAttribute::.ctor() = ( + 01 00 00 00 + ) + .field public valuetype InvalidCSharp.WithRefField& Field + + .method public hidebysig specialname rtspecialname + instance void .ctor ( + valuetype InvalidCSharp.WithRefField& + ) cil managed + { + ldarg.0 + ldarg.1 + stfld valuetype InvalidCSharp.WithRefField& InvalidCSharp.WithRefStructField::Field + ret + } + + .method public hidebysig + instance bool ConfirmFieldInstance ( + valuetype InvalidCSharp.WithRefField& + ) cil managed + { + ldarg.0 + ldfld valuetype InvalidCSharp.WithRefField& InvalidCSharp.WithRefStructField::Field + ldarg.1 + ceq + ret + } +} + +.class public sequential ansi sealed beforefieldinit InvalidCSharp.WithTypedReferenceField`1 + extends [System.Runtime]System.ValueType +{ + .custom instance void [System.Runtime]System.Runtime.CompilerServices.IsByRefLikeAttribute::.ctor() = ( + 01 00 00 00 + ) + .field public typedref Field + + .method public hidebysig specialname rtspecialname + instance void .ctor ( + !T& + ) cil managed + { + ldarg.0 + ldarg.1 + mkrefany !T + stfld typedref valuetype InvalidCSharp.WithTypedReferenceField`1::Field + ret + } + + .method public hidebysig + instance class [System.Runtime]System.Type GetFieldType () cil managed + { + ldarg.0 + ldfld typedref valuetype InvalidCSharp.WithTypedReferenceField`1::Field + refanytype + call class [System.Runtime]System.Type [System.Runtime]System.Type::GetTypeFromHandle(valuetype [System.Runtime]System.RuntimeTypeHandle ) + ret + } + + .method public hidebysig + instance bool ConfirmFieldInstance ( + !T + ) cil managed + { + ldarg.0 + ldfld typedref valuetype InvalidCSharp.WithTypedReferenceField`1::Field + refanyval !T + ldind.ref + ldarg.1 + ceq + ret + } +} diff --git a/src/tests/Loader/classloader/RefFields/Validate.cs b/src/tests/Loader/classloader/RefFields/Validate.cs index b7b82dd972f43a..65d9d36f8df742 100644 --- a/src/tests/Loader/classloader/RefFields/Validate.cs +++ b/src/tests/Loader/classloader/RefFields/Validate.cs @@ -129,54 +129,4 @@ public static void Validate_Create_TypedReferenceRefField() Validate v = new(); Create_TypedReferenceRefField_Worker(v, 1); } -} - -public ref struct WithRefField -{ - public ref string Str; - - public WithRefField(ref string str) - { - Str = ref str; - } - - public bool ConfirmFieldInstance(string value) - { - return object.ReferenceEquals(Str, value); - } -} - -public ref struct WithRefStructField -{ - public ref WithRefField Field; - - public WithRefStructField(ref WithRefField field) - { - Field = ref field; - } - - public bool ConfirmFieldInstance(ref WithRefField field) - { - return Unsafe.AreSame(ref Field, ref field); - } -} - -public ref struct WithTypedReferenceField -{ - public TypedReference Field; - - public WithTypedReferenceField(ref T value) - { - Field = __makeref(value); - } - - public Type GetFieldType() - { - return __reftype(Field); - } - - public bool ConfirmFieldInstance(T value) - { - return object.ReferenceEquals(__refvalue(Field, T), value); - } } \ No newline at end of file From 102fe72ec7c920dcb15efeaf80072f5450b8bf4a Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sat, 21 Feb 2026 00:34:04 +0000 Subject: [PATCH 10/10] Revert InvalidCSharp.il (ByRefLike) to original state Co-authored-by: jkoritzinsky <1571408+jkoritzinsky@users.noreply.github.com> --- .../generics/ByRefLike/InvalidCSharp.il | 100 ++++++++++-------- 1 file changed, 54 insertions(+), 46 deletions(-) diff --git a/src/tests/Loader/classloader/generics/ByRefLike/InvalidCSharp.il b/src/tests/Loader/classloader/generics/ByRefLike/InvalidCSharp.il index f5ca7973396e66..1bfb367c6d0006 100644 --- a/src/tests/Loader/classloader/generics/ByRefLike/InvalidCSharp.il +++ b/src/tests/Loader/classloader/generics/ByRefLike/InvalidCSharp.il @@ -395,20 +395,42 @@ } } -.class interface public auto ansi abstract InvalidCSharp.SimpleInterface +.class public sequential ansi sealed beforefieldinit InvalidCSharp.GenericClass_WithStaticField`1 + extends [System.Runtime]System.Object { - .method public hidebysig newslot abstract virtual - instance int32 Method() cil managed + .method public hidebysig specialname rtspecialname + instance void .ctor () cil managed { + .locals init ( + [0] !T, + [1] !T + ) + + ldarg.0 + call instance void [System.Runtime]System.Object::.ctor() + + ldloca.s 0 + initobj !T + + ldloc.0 + stsfld !0 class InvalidCSharp.GenericClass_WithStaticField`1::StaticField + ldsfld !0 class InvalidCSharp.GenericClass_WithStaticField`1::StaticField + stloc.1 + ret } + + .field public static class InvalidCSharp.GenericClass_Over`1 StaticField1 + .field public static !T StaticField } -.class interface public auto ansi abstract InvalidCSharp.DefaultInterface +.class public sequential ansi sealed beforefieldinit InvalidCSharp.GenericClass_IndependentConstraints`2 + extends [System.Runtime]System.Object { - .method public hidebysig newslot virtual - instance int32 Method() cil managed + .method public hidebysig specialname rtspecialname + instance void .ctor () cil managed { - ldc.i4.0 + ldarg.0 + call instance void [System.Runtime]System.Object::.ctor() ret } } @@ -429,6 +451,22 @@ ) } +.class public sequential ansi sealed beforefieldinit ByRefLikeType`1 + extends [System.Runtime]System.ValueType +{ + .custom instance void [System.Runtime]System.Runtime.CompilerServices.IsByRefLikeAttribute::.ctor() = ( + 01 00 00 00 + ) +} + +.class interface public auto ansi abstract InvalidCSharp.SimpleInterface +{ + .method public hidebysig newslot abstract virtual + instance int32 Method() cil managed + { + } +} + .class public sequential ansi sealed beforefieldinit ByRefLikeTypeWithInterface extends [System.Runtime]System.ValueType implements InvalidCSharp.SimpleInterface @@ -445,9 +483,14 @@ } } -.class public sequential ansi sealed beforefieldinit RegularValueType - extends [System.Runtime]System.ValueType +.class interface public auto ansi abstract InvalidCSharp.DefaultInterface { + .method public hidebysig newslot virtual + instance int32 Method() cil managed + { + ldc.i4.0 + ret + } } .class public sequential ansi sealed beforefieldinit RS_DI1 @@ -475,44 +518,9 @@ } } -.class public sequential ansi sealed beforefieldinit InvalidCSharp.GenericClass_WithStaticField`1 - extends [System.Runtime]System.Object -{ - .method public hidebysig specialname rtspecialname - instance void .ctor () cil managed - { - .locals init ( - [0] !T, - [1] !T - ) - - ldarg.0 - call instance void [System.Runtime]System.Object::.ctor() - - ldloca.s 0 - initobj !T - - ldloc.0 - stsfld !0 class InvalidCSharp.GenericClass_WithStaticField`1::StaticField - ldsfld !0 class InvalidCSharp.GenericClass_WithStaticField`1::StaticField - stloc.1 - ret - } - - .field public static class InvalidCSharp.GenericClass_Over`1 StaticField1 - .field public static !T StaticField -} - -.class public sequential ansi sealed beforefieldinit InvalidCSharp.GenericClass_IndependentConstraints`2 - extends [System.Runtime]System.Object +.class public sequential ansi sealed beforefieldinit RegularValueType + extends [System.Runtime]System.ValueType { - .method public hidebysig specialname rtspecialname - instance void .ctor () cil managed - { - ldarg.0 - call instance void [System.Runtime]System.Object::.ctor() - ret - } } .class public auto ansi beforefieldinit ClassWithInterface