diff --git a/src/mono/mono/metadata/class-init.c b/src/mono/mono/metadata/class-init.c index 6d2426cc71a49..d9cfd751c8192 100644 --- a/src/mono/mono/metadata/class-init.c +++ b/src/mono/mono/metadata/class-init.c @@ -2573,6 +2573,10 @@ mono_class_layout_fields (MonoClass *klass, int base_instance_size, int packing_ case MONO_TYPE_VALUETYPE: case MONO_TYPE_GENERICINST: field_class = mono_class_from_mono_type_internal (field->type); + if (mono_class_is_ginst (field_class) && !mono_verifier_class_is_valid_generic_instantiation (field_class)) { + mono_class_set_type_load_failure (klass, "Field '%s' is an invalid generic instantiation of type %s", field->name, mono_type_get_full_name (field_class)); + return; + } break; default: break; diff --git a/src/tests/Loader/classloader/generics/ByRefLike/ValidateNegative.cs b/src/tests/Loader/classloader/generics/ByRefLike/ValidateNegative.cs index 20feede256b48..78f091b14acaf 100644 --- a/src/tests/Loader/classloader/generics/ByRefLike/ValidateNegative.cs +++ b/src/tests/Loader/classloader/generics/ByRefLike/ValidateNegative.cs @@ -18,7 +18,6 @@ public static void AllowByRefLike_Substituted_For_NonByRefLike_Invalid() Console.WriteLine($"{nameof(AllowByRefLike_Substituted_For_NonByRefLike_Invalid)}..."); Assert.Throws(() => { Exec.TypeSubstitutionInterfaceImplementationAllowByRefLikeIntoNonByRefLike(); }); - Assert.Throws(() => { Exec.TypeSubstitutionFieldAllowByRefLikeIntoNonByRefLike(); }); Assert.Throws(() => { Exec.OverrideMethodNotByRefLike(); }); } @@ -28,5 +27,6 @@ public static void AllowByRefLike_Substituted_For_NonByRefLike_Invalid_Class() Console.WriteLine($"{nameof(AllowByRefLike_Substituted_For_NonByRefLike_Invalid_Class)}..."); Assert.Throws(() => { Exec.TypeSubstitutionInheritanceAllowByRefLikeIntoNonByRefLike(); }); + Assert.Throws(() => { Exec.TypeSubstitutionFieldAllowByRefLikeIntoNonByRefLike(); }); } } \ No newline at end of file