diff --git a/src/coreclr/tools/Common/TypeSystem/Common/MetadataFieldLayoutAlgorithm.cs b/src/coreclr/tools/Common/TypeSystem/Common/MetadataFieldLayoutAlgorithm.cs index 9973be45ff1308..637f1f7538ff88 100644 --- a/src/coreclr/tools/Common/TypeSystem/Common/MetadataFieldLayoutAlgorithm.cs +++ b/src/coreclr/tools/Common/TypeSystem/Common/MetadataFieldLayoutAlgorithm.cs @@ -37,12 +37,8 @@ public override ComputedInstanceFieldLayout ComputeInstanceLayout(DefType defTyp TypeDesc fieldType = field.FieldType; - // ByRef instance fields are not allowed. - if (fieldType.IsByRef) - ThrowHelper.ThrowTypeLoadException(ExceptionStringID.ClassLoadGeneral, type); - - // ByRef-like instance fields on non-byref-like types are not allowed. - if (fieldType.IsByRefLike && !type.IsByRefLike) + // ByRef and byref-like instance fields on non-byref-like types are not allowed. + if ((fieldType.IsByRef || fieldType.IsByRefLike) && !type.IsByRefLike) ThrowHelper.ThrowTypeLoadException(ExceptionStringID.ClassLoadGeneral, type); numInstanceFields++; @@ -481,7 +477,7 @@ protected ComputedInstanceFieldLayout ComputeAutoFieldLayout(MetadataType type, } else { - Debug.Assert(fieldType.IsPrimitive || fieldType.IsPointer || fieldType.IsFunctionPointer || fieldType.IsEnum); + Debug.Assert(fieldType.IsPrimitive || fieldType.IsPointer || fieldType.IsFunctionPointer || fieldType.IsEnum || fieldType.IsByRef); var fieldSizeAndAlignment = ComputeFieldSizeAndAlignment(fieldType, hasLayout, packingSize, out bool _, out bool _); instanceNonGCPointerFieldsCount[CalculateLog2(fieldSizeAndAlignment.Size.AsInt)]++; @@ -815,7 +811,7 @@ private static SizeAndAlignment ComputeFieldSizeAndAlignment(TypeDesc fieldType, } else { - Debug.Assert(fieldType.IsPointer || fieldType.IsFunctionPointer); + Debug.Assert(fieldType.IsPointer || fieldType.IsFunctionPointer || fieldType.IsByRef); result.Size = fieldType.Context.Target.LayoutPointerSize; result.Alignment = fieldType.Context.Target.LayoutPointerSize; } diff --git a/src/tests/Loader/classloader/RefFields/InvalidCSharp.il b/src/tests/Loader/classloader/RefFields/InvalidCSharp.il index b976227547def5..7fc74717dd1029 100644 --- a/src/tests/Loader/classloader/RefFields/InvalidCSharp.il +++ b/src/tests/Loader/classloader/RefFields/InvalidCSharp.il @@ -107,7 +107,7 @@ ldarg.0 ldarga.s 1 mkrefany !T - stfld typedref InvalidCSharp.WithTypedReferenceField`1::Field + stfld typedref valuetype InvalidCSharp.WithTypedReferenceField`1::Field ret } @@ -115,9 +115,9 @@ instance class [System.Runtime]System.Type GetFieldType () cil managed { ldarg.0 - ldfld typedref InvalidCSharp.WithTypedReferenceField`1::Field + 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 } -} \ No newline at end of file +}