Skip to content

Commit b96bcdb

Browse files
committed
Make CallCtorAsMethod tests work
1 parent 9134ab0 commit b96bcdb

File tree

2 files changed

+9
-5
lines changed

2 files changed

+9
-5
lines changed

src/mono/mono/metadata/marshal-lightweight.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2429,7 +2429,6 @@ emit_missing_method_error (MonoMethodBuilder *mb, MonoError *failure, const char
24292429
static void
24302430
emit_unsafe_accessor_ctor_wrapper (MonoMethodBuilder *mb, MonoMethod *accessor_method, MonoMethodSignature *sig, MonoGenericContext *ctx, MonoUnsafeAccessorKind kind, const char *member_name)
24312431
{
2432-
// TODO: support coreCLR's weird "you can call the .ctor as a normal instance member" method
24332432
g_assert (kind == MONO_UNSAFE_ACCESSOR_CTOR);
24342433
// null or empty string member name is ok for a constructor
24352434
if (!member_name || member_name[0] == '\0')
@@ -2473,6 +2472,9 @@ emit_unsafe_accessor_method_wrapper (MonoMethodBuilder *mb, MonoMethod *accessor
24732472
g_assert (kind == MONO_UNSAFE_ACCESSOR_METHOD || kind == MONO_UNSAFE_ACCESSOR_STATIC_METHOD);
24742473
g_assert (member_name != NULL);
24752474

2475+
// We explicitly allow calling a constructor as if it was an instance method, but we need some hacks in a couple of places
2476+
gboolean ctor_as_method = !strcmp (member_name, ".ctor");
2477+
24762478
if (sig->param_count < 1 || sig->params[0] == NULL || unsafe_accessor_target_type_forbidden (sig->params[0])) {
24772479
mono_mb_emit_exception_full (mb, "System", "BadImageFormatException", "Invalid usage of UnsafeAccessorAttribute.");
24782480
return;
@@ -2491,7 +2493,11 @@ emit_unsafe_accessor_method_wrapper (MonoMethodBuilder *mb, MonoMethod *accessor
24912493

24922494
ERROR_DECL(find_method_error);
24932495
MonoClass *in_class = mono_class_is_ginst (target_class) ? mono_class_get_generic_class (target_class)->container_class : target_class;
2494-
MonoMethod *target_method = mono_unsafe_accessor_find_method (in_class, member_name, member_sig, target_class, find_method_error);
2496+
MonoMethod *target_method = NULL;
2497+
if (!ctor_as_method)
2498+
target_method = mono_unsafe_accessor_find_method (in_class, member_name, member_sig, target_class, find_method_error);
2499+
else
2500+
target_method = mono_unsafe_accessor_find_ctor (in_class, member_sig, target_class, find_method_error);
24952501
if (!is_ok (find_method_error) || target_method == NULL) {
24962502
// g_warning ("FAILed to find '%s' in '%s' with sig '%s', due to %s\n", member_name, m_class_get_name (target_class), mono_signature_full_name (member_sig), mono_error_get_message (find_method_error));
24972503
emit_missing_method_error (mb, find_method_error, member_name);

src/tests/baseservices/compilerservices/UnsafeAccessors/UnsafeAccessorsTests.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,6 @@ public static void Verify_CallCtorValue()
120120
}
121121

122122
[Fact]
123-
[ActiveIssue("https://github.com/dotnet/runtime/issues/86040", TestRuntimes.Mono)]
124123
public static void Verify_CallCtorAsMethod()
125124
{
126125
Console.WriteLine($"Running {nameof(Verify_CallCtorAsMethod)}");
@@ -136,7 +135,6 @@ public static void Verify_CallCtorAsMethod()
136135
}
137136

138137
[Fact]
139-
[ActiveIssue("https://github.com/dotnet/runtime/issues/86040", TestRuntimes.Mono)]
140138
public static void Verify_CallCtorAsMethodValue()
141139
{
142140
Console.WriteLine($"Running {nameof(Verify_CallCtorAsMethodValue)}");
@@ -508,4 +506,4 @@ public static void Verify_InvalidUseUnsafeAccessor()
508506
[UnsafeAccessor(UnsafeAccessorKind.Method, Name=nameof(ToString))]
509507
extern static string LookUpFailsOnFunctionPointers(delegate* <void> fptr);
510508
}
511-
}
509+
}

0 commit comments

Comments
 (0)