Skip to content
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

[mono] Add basic ref struct support for generic parameter #99081

Merged
merged 19 commits into from
Mar 13, 2024
Merged
Show file tree
Hide file tree
Changes from 6 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
2 changes: 1 addition & 1 deletion src/mono/mono/metadata/class-init.c
Original file line number Diff line number Diff line change
Expand Up @@ -2953,7 +2953,7 @@ mono_class_init_internal (MonoClass *klass)
if (klass->inited || mono_class_has_failure (klass))
return !mono_class_has_failure (klass);

/*g_print ("Init class %s\n", mono_type_get_full_name (klass));*/
// g_print ("Init class %s\n", mono_type_get_full_name (klass));

/*
* This function can recursively call itself.
Expand Down
3 changes: 0 additions & 3 deletions src/mono/mono/metadata/class.c
Original file line number Diff line number Diff line change
Expand Up @@ -640,10 +640,7 @@ mono_type_is_valid_generic_argument (MonoType *type)
{
switch (type->type) {
case MONO_TYPE_VOID:
case MONO_TYPE_TYPEDBYREF:
return FALSE;
case MONO_TYPE_VALUETYPE:
return !m_class_is_byreflike (type->data.klass);
default:
return TRUE;
}
Expand Down
21 changes: 11 additions & 10 deletions src/mono/mono/metadata/object-internals.h
Original file line number Diff line number Diff line change
Expand Up @@ -1439,16 +1439,17 @@ typedef struct {

/* Keep in sync with System.GenericParameterAttributes */
typedef enum {
GENERIC_PARAMETER_ATTRIBUTE_NON_VARIANT = 0,
GENERIC_PARAMETER_ATTRIBUTE_COVARIANT = 1,
GENERIC_PARAMETER_ATTRIBUTE_CONTRAVARIANT = 2,
GENERIC_PARAMETER_ATTRIBUTE_VARIANCE_MASK = 3,

GENERIC_PARAMETER_ATTRIBUTE_NO_SPECIAL_CONSTRAINT = 0,
GENERIC_PARAMETER_ATTRIBUTE_REFERENCE_TYPE_CONSTRAINT = 4,
GENERIC_PARAMETER_ATTRIBUTE_VALUE_TYPE_CONSTRAINT = 8,
GENERIC_PARAMETER_ATTRIBUTE_CONSTRUCTOR_CONSTRAINT = 16,
GENERIC_PARAMETER_ATTRIBUTE_SPECIAL_CONSTRAINTS_MASK = 28
GENERIC_PARAMETER_ATTRIBUTE_NON_VARIANT = 0x0000,
GENERIC_PARAMETER_ATTRIBUTE_COVARIANT = 0x0001,
GENERIC_PARAMETER_ATTRIBUTE_CONTRAVARIANT = 0x0002,
GENERIC_PARAMETER_ATTRIBUTE_VARIANCE_MASK = 0x0003,

GENERIC_PARAMETER_ATTRIBUTE_NO_SPECIAL_CONSTRAINT = 0x0000,
GENERIC_PARAMETER_ATTRIBUTE_REFERENCE_TYPE_CONSTRAINT = 0x0004,
GENERIC_PARAMETER_ATTRIBUTE_VALUE_TYPE_CONSTRAINT = 0x0008,
GENERIC_PARAMETER_ATTRIBUTE_CONSTRUCTOR_CONSTRAINT = 0x0010,
GENERIC_PARAMETER_ATTRIBUTE_ACCEPT_BYREFLIKE_CONSTRAINTS = 0x0020, // type argument can be ByRefLike
GENERIC_PARAMETER_ATTRIBUTE_SPECIAL_CONSTRAINTS_MASK = 0x003c
} GenericParameterAttributes;

typedef struct {
Expand Down
3 changes: 3 additions & 0 deletions src/mono/mono/metadata/verify.c
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ is_valid_generic_instantiation (MonoGenericContainer *gc, MonoGenericContext *co
return FALSE;
}

if (m_class_is_byreflike (paramClass) && (param_info->flags & GENERIC_PARAMETER_ATTRIBUTE_ACCEPT_BYREFLIKE_CONSTRAINTS) == 0)
return FALSE;

if (!param_info->constraints && !(param_info->flags & GENERIC_PARAMETER_ATTRIBUTE_SPECIAL_CONSTRAINTS_MASK))
continue;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
public class GenericTypeSubstitution
{
[Fact]
[SkipOnMono("Mono does not support ByRefLike generics yet")]
public static void AllowByRefLike_Substituted_For_AllowByRefLike()
{
Console.WriteLine($"{nameof(AllowByRefLike_Substituted_For_AllowByRefLike)}...");
Expand All @@ -23,7 +22,6 @@ public static void AllowByRefLike_Substituted_For_AllowByRefLike()
}

[Fact]
[SkipOnMono("Mono does not support ByRefLike generics yet")]
public static void NonByRefLike_Substituted_For_AllowByRefLike()
{
Console.WriteLine($" -- Instantiate: {Exec.TypeSubstitutionInterfaceImplementationNonByRefLike()}");
Expand Down
58 changes: 29 additions & 29 deletions src/tests/Loader/classloader/generics/ByRefLike/InvalidCSharp.il
Original file line number Diff line number Diff line change
Expand Up @@ -623,35 +623,35 @@
ret
}

.method public hidebysig static
void AllocArrayOfT_Invalid() cil managed
{
.locals init (
[0] valuetype InvalidCSharp.GenericByRefLike_Over`1<valuetype ByRefLikeType>
)

ldloca.s 0
initobj valuetype InvalidCSharp.GenericByRefLike_Over`1<valuetype ByRefLikeType>
ldloca.s 0
call instance bool valuetype InvalidCSharp.GenericByRefLike_Over`1<valuetype ByRefLikeType>::AllocArrayOfT()
pop
ret
}

.method public hidebysig static
void AllocMultiDimArrayOfT_Invalid() cil managed
{
.locals init (
[0] valuetype InvalidCSharp.GenericByRefLike_Over`1<valuetype ByRefLikeType>
)

ldloca.s 0
initobj valuetype InvalidCSharp.GenericByRefLike_Over`1<valuetype ByRefLikeType>
ldloca.s 0
call instance bool valuetype InvalidCSharp.GenericByRefLike_Over`1<valuetype ByRefLikeType>::AllocMultiDimArrayOfT()
pop
ret
}
// .method public hidebysig static
fanyang-mono marked this conversation as resolved.
Show resolved Hide resolved
// void AllocArrayOfT_Invalid() cil managed
// {
// .locals init (
// [0] valuetype InvalidCSharp.GenericByRefLike_Over`1<valuetype ByRefLikeType>
// )

// ldloca.s 0
// initobj valuetype InvalidCSharp.GenericByRefLike_Over`1<valuetype ByRefLikeType>
// ldloca.s 0
// call instance bool valuetype InvalidCSharp.GenericByRefLike_Over`1<valuetype ByRefLikeType>::AllocArrayOfT()
// pop
// ret
// }

// .method public hidebysig static
// void AllocMultiDimArrayOfT_Invalid() cil managed
// {
// .locals init (
// [0] valuetype InvalidCSharp.GenericByRefLike_Over`1<valuetype ByRefLikeType>
// )

// ldloca.s 0
// initobj valuetype InvalidCSharp.GenericByRefLike_Over`1<valuetype ByRefLikeType>
// ldloca.s 0
// call instance bool valuetype InvalidCSharp.GenericByRefLike_Over`1<valuetype ByRefLikeType>::AllocMultiDimArrayOfT()
// pop
// ret
// }

.method public hidebysig static
string GenericClassWithStaticField_Invalid() cil managed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
<PropertyGroup>
<OutputType>library</OutputType>
<MonoAotIncompatible>true</MonoAotIncompatible>
<DisableProjectBuild Condition="'$(RuntimeFlavor)' == 'Mono'">true</DisableProjectBuild>
</PropertyGroup>
<ItemGroup>
<Compile Include="InvalidCSharp.il" />
Expand Down
20 changes: 14 additions & 6 deletions src/tests/Loader/classloader/generics/ByRefLike/Validate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
public class Validate
{
[Fact]
[SkipOnMono("Mono does not support ByRefLike generics yet")]
public static void Validate_TypeLoad()
{
Console.WriteLine($"{nameof(Validate_TypeLoad)}...");
Expand All @@ -29,7 +28,6 @@ public static void Validate_TypeLoad()
}

[Fact]
[SkipOnMono("Mono does not support ByRefLike generics yet")]
public static void Validate_Casting_Scenarios()
{
Console.WriteLine($"{nameof(Validate_Casting_Scenarios)}...");
Expand All @@ -44,7 +42,6 @@ public static void Validate_Casting_Scenarios()
}

[Fact]
[SkipOnMono("Mono does not support ByRefLike generics yet")]
public static void Validate_RecognizedOpCodeSequences_Scenarios()
{
Console.WriteLine($"{nameof(Validate_RecognizedOpCodeSequences_Scenarios)}...");
Expand All @@ -64,16 +61,27 @@ public static void Validate_InvalidOpCode_Scenarios()
// These methods uses opcodes that are not able to handle ByRefLike type operands.
// The TypeLoader prevents these invalid types from being constructed. We rely on
// the failure to construct these invalid types to block opcode usage.
Assert.Throws<TypeLoadException>(() => { Exec.AllocArrayOfT_Invalid(); });
Assert.Throws<TypeLoadException>(() => { Exec.AllocMultiDimArrayOfT_Invalid(); });
Assert.Throws<TypeLoadException>(() => { Exec.GenericClassWithStaticField_Invalid(); });

// Test that explicitly tries to box a ByRefLike type.
Assert.Throws<InvalidProgramException>(() => { Exec.BoxAsObject(); });
}

[Fact]
[SkipOnMono("Mono does not support ByRefLike generics yet")]
[SkipOnMono("https://github.com/dotnet/runtime/issues/99165")]
public static void Validate_InvalidOpCode_Scenarios_ArrayOfT()
{
Console.WriteLine($"{nameof(Validate_InvalidOpCode_Scenarios_ArrayOfT)}...");

// These methods uses opcodes that are not able to handle ByRefLike type operands.
// The TypeLoader prevents these invalid types from being constructed. We rely on
// the failure to construct these invalid types to block opcode usage.

// Assert.Throws<TypeLoadException>(() => { Exec.AllocArrayOfT_Invalid(); });
// Assert.Throws<TypeLoadException>(() => { Exec.AllocMultiDimArrayOfT_Invalid(); });
fanyang-mono marked this conversation as resolved.
Show resolved Hide resolved
}

[Fact]
public static void Validate_Inlining_Behavior()
{
Console.WriteLine($"{nameof(Validate_Inlining_Behavior)}...");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
<PropertyGroup>
<!-- Needed for mechanical merging of all remaining tests, this particular project may not actually need process isolation -->
<RequiresProcessIsolation>true</RequiresProcessIsolation>
<DisableProjectBuild Condition="'$(RuntimeFlavor)' == 'Mono'">true</DisableProjectBuild>
</PropertyGroup>
<ItemGroup>
<Compile Include="Validate.cs" />
Expand Down
Loading