Skip to content

Commit efdd299

Browse files
Do not substitute return values of constrained calls (#111030)
Fixes #110932. The constraint would need to be resolved first.
1 parent ddf2e57 commit efdd299

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

Diff for: src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/SubstitutedILProvider.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -748,12 +748,12 @@ private bool TryGetConstantArgument(MethodIL methodIL, byte[] body, OpcodeFlags[
748748
{
749749
BodySubstitution substitution = _substitutionProvider.GetSubstitution(method);
750750
if (substitution != null && substitution.Value is int
751-
&& (opcode != ILOpcode.callvirt || !method.IsVirtual))
751+
&& ((opcode != ILOpcode.callvirt && !method.Signature.IsStatic) || !method.IsVirtual))
752752
{
753753
constant = (int)substitution.Value;
754754
return true;
755755
}
756-
if ((opcode != ILOpcode.callvirt || !method.IsVirtual)
756+
if (((opcode != ILOpcode.callvirt && !method.Signature.IsStatic) || !method.IsVirtual)
757757
&& TryGetMethodConstantValue(method, out constant))
758758
{
759759
return true;

Diff for: src/tests/nativeaot/SmokeTests/TrimmingBehaviors/DeadCodeElimination.cs

+29
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ class DeadCodeElimination
1212
public static int Run()
1313
{
1414
SanityTest.Run();
15+
Test110932Regression.Run();
1516
TestInstanceMethodOptimization.Run();
1617
TestReflectionInvokeSignatures.Run();
1718
TestAbstractTypeNeverDerivedVirtualsOptimization.Run();
@@ -52,6 +53,34 @@ public static void Run()
5253
}
5354
}
5455

56+
class Test110932Regression
57+
{
58+
static bool s_trueConst = true;
59+
static bool s_falseConst = false;
60+
61+
interface I
62+
{
63+
static virtual bool GetValue() => false;
64+
}
65+
66+
class C : I
67+
{
68+
static bool I.GetValue() => true;
69+
}
70+
71+
public static void Run()
72+
{
73+
if (!Call<C>())
74+
throw new Exception();
75+
}
76+
static bool Call<T>() where T : I
77+
{
78+
if (T.GetValue())
79+
return s_trueConst;
80+
return s_falseConst;
81+
}
82+
}
83+
5584
class TestInstanceMethodOptimization
5685
{
5786
class UnreferencedType { }

0 commit comments

Comments
 (0)