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 7 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
1 change: 0 additions & 1 deletion src/mono/mono/metadata/loader.c
Original file line number Diff line number Diff line change
Expand Up @@ -823,7 +823,6 @@ mono_method_search_in_array_class (MonoClass *klass, const char *name, MonoMetho
int i;

mono_class_setup_methods (klass);
g_assert (!mono_class_has_failure (klass)); /*FIXME this should not fail, right?*/
fanyang-mono marked this conversation as resolved.
Show resolved Hide resolved
int mcount = mono_class_get_method_count (klass);
MonoMethod **klass_methods = m_class_get_methods (klass);
for (i = 0; i < mcount; ++i) {
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
2 changes: 1 addition & 1 deletion src/mono/mono/mini/method-to-ir.c
Original file line number Diff line number Diff line change
Expand Up @@ -9277,7 +9277,7 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b

mono_save_token_info (cfg, image, token, cmethod);

if (!mono_class_init_internal (cmethod->klass))
if (mono_class_has_failure (cmethod->klass) || !mono_class_init_internal (cmethod->klass))
TYPE_LOAD_ERROR (cmethod->klass);

context_used = mini_method_check_context_used (cfg, cmethod);
Expand Down
2 changes: 2 additions & 0 deletions src/mono/mono/mini/mini-generic-sharing.c
Original file line number Diff line number Diff line change
Expand Up @@ -590,6 +590,7 @@ inflate_info (MonoMemoryManager *mem_manager, MonoRuntimeGenericContextInfoTempl
inflated_method = mono_class_inflate_generic_method_checked (method, context, error);
g_assert (is_ok (error)); /* FIXME don't swallow the error */
}
g_assert (inflated_method);
mono_class_init_internal (inflated_method->klass);
g_assert (inflated_method->klass == inflated_class);
return inflated_method;
Expand Down Expand Up @@ -654,6 +655,7 @@ inflate_info (MonoMemoryManager *mem_manager, MonoRuntimeGenericContextInfoTempl
inflated_method = mono_class_inflate_generic_method_checked (method, context, error);
g_assert (is_ok (error)); /* FIXME don't swallow the error */
}
g_assert (inflated_method);
mono_class_init_internal (inflated_method->klass);
g_assert (inflated_method->klass == inflated_class);

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
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
19 changes: 13 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,26 @@ 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")]
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(); });
}

[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