Skip to content

Commit 8ff4dd4

Browse files
authored
Extend mono_gsharedvt_constrained_call for static calls and handle nullable value types (#101491)
This PR extends mono_gsharedvt_constrained_call to handle static MONO_GSHAREDVT_CONSTRAINT_CALL_TYPE_REF calls. If the cmethod is a static method, this_arg should be NULL. Also, it skips dereferencing sharedvt ref arguments if they are nullable value types.
1 parent 6fa6fb7 commit 8ff4dd4

File tree

4 files changed

+55
-7
lines changed

4 files changed

+55
-7
lines changed

src/mono/mono/mini/jit-icalls.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1450,7 +1450,8 @@ mono_gsharedvt_constrained_call (gpointer mp, MonoMethod *cmethod, MonoClass *kl
14501450
break;
14511451
case MONO_GSHAREDVT_CONSTRAINT_CALL_TYPE_REF:
14521452
/* Calling a ref method with a ref receiver */
1453-
this_arg = *(gpointer*)mp;
1453+
/* Static calls don't have this arg */
1454+
this_arg = m_method_is_static (cmethod) ? NULL : *(gpointer*)mp;
14541455
m = info->method;
14551456
break;
14561457
default:

src/mono/mono/mini/method-to-ir.c

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3884,13 +3884,8 @@ handle_constrained_gsharedvt_call (MonoCompile *cfg, MonoMethod *cmethod, MonoMe
38843884
int addr_reg;
38853885

38863886
if (mini_is_gsharedvt_type (fsig->params [i])) {
3887-
MonoInst *is_deref;
3888-
int deref_arg_reg;
38893887
ins = mini_emit_get_gsharedvt_info_klass (cfg, mono_class_from_mono_type_internal (fsig->params [i]), MONO_RGCTX_INFO_CLASS_BOX_TYPE);
3890-
deref_arg_reg = alloc_preg (cfg);
3891-
/* deref_arg = BOX_TYPE != MONO_GSHAREDVT_BOX_TYPE_VTYPE */
3892-
EMIT_NEW_BIALU_IMM (cfg, is_deref, OP_ISUB_IMM, deref_arg_reg, ins->dreg, 1);
3893-
MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI1_MEMBASE_REG, is_gsharedvt_ins->dreg, i, is_deref->dreg);
3888+
MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI1_MEMBASE_REG, is_gsharedvt_ins->dreg, i, ins->dreg);
38943889
} else if (has_gsharedvt) {
38953890
MONO_EMIT_NEW_STORE_MEMBASE_IMM (cfg, OP_STOREI1_MEMBASE_IMM, is_gsharedvt_ins->dreg, i, 0);
38963891
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System;
5+
using Xunit;
6+
7+
public static class Runtime_94467
8+
{
9+
public interface ITypeChecker
10+
{
11+
static abstract bool Test<T>(T value);
12+
}
13+
14+
public interface IHandler
15+
{
16+
bool Test<T>(T value);
17+
}
18+
19+
public struct TypeChecker : ITypeChecker
20+
{
21+
public static bool Test<T>(T value) => true;
22+
}
23+
24+
public class Handler<TChecker> : IHandler where TChecker : ITypeChecker
25+
{
26+
public bool Test<T>(T value) => TChecker.Test(value);
27+
}
28+
29+
public static IHandler GetHandler() => new Handler<TypeChecker>();
30+
31+
[Fact]
32+
public static int Test()
33+
{
34+
try {
35+
var handler = GetHandler();
36+
if (handler.Test<bool>(true) && handler.Test<bool?>(true))
37+
return 100;
38+
else
39+
return 101;
40+
} catch (Exception) {
41+
return -1;
42+
}
43+
}
44+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<Optimize>True</Optimize>
4+
</PropertyGroup>
5+
<ItemGroup>
6+
<Compile Include="$(MSBuildProjectName).cs" />
7+
</ItemGroup>
8+
</Project>

0 commit comments

Comments
 (0)