Skip to content

Add support for byref fields in NativeAOT/crossgen2 #64366

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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++;
Expand Down Expand Up @@ -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)]++;
Expand Down Expand Up @@ -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;
}
Expand Down
6 changes: 3 additions & 3 deletions src/tests/Loader/classloader/RefFields/InvalidCSharp.il
Original file line number Diff line number Diff line change
Expand Up @@ -107,17 +107,17 @@
ldarg.0
ldarga.s 1
mkrefany !T
stfld typedref InvalidCSharp.WithTypedReferenceField`1::Field
stfld typedref valuetype InvalidCSharp.WithTypedReferenceField`1<!T>::Field
ret
}

.method public hidebysig
instance class [System.Runtime]System.Type GetFieldType () cil managed
{
ldarg.0
ldfld typedref InvalidCSharp.WithTypedReferenceField`1::Field
ldfld typedref valuetype InvalidCSharp.WithTypedReferenceField`1<!T>::Field
refanytype
call class [System.Runtime]System.Type [System.Runtime]System.Type::GetTypeFromHandle(valuetype [System.Runtime]System.RuntimeTypeHandle )
ret
}
}
}