Skip to content

Commit

Permalink
Do not substitute return values of constrained calls (#111030)
Browse files Browse the repository at this point in the history
Fixes #110932.

The constraint would need to be resolved first.
  • Loading branch information
MichalStrehovsky authored Jan 2, 2025
1 parent ddf2e57 commit efdd299
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -748,12 +748,12 @@ private bool TryGetConstantArgument(MethodIL methodIL, byte[] body, OpcodeFlags[
{
BodySubstitution substitution = _substitutionProvider.GetSubstitution(method);
if (substitution != null && substitution.Value is int
&& (opcode != ILOpcode.callvirt || !method.IsVirtual))
&& ((opcode != ILOpcode.callvirt && !method.Signature.IsStatic) || !method.IsVirtual))
{
constant = (int)substitution.Value;
return true;
}
if ((opcode != ILOpcode.callvirt || !method.IsVirtual)
if (((opcode != ILOpcode.callvirt && !method.Signature.IsStatic) || !method.IsVirtual)
&& TryGetMethodConstantValue(method, out constant))
{
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class DeadCodeElimination
public static int Run()
{
SanityTest.Run();
Test110932Regression.Run();
TestInstanceMethodOptimization.Run();
TestReflectionInvokeSignatures.Run();
TestAbstractTypeNeverDerivedVirtualsOptimization.Run();
Expand Down Expand Up @@ -52,6 +53,34 @@ public static void Run()
}
}

class Test110932Regression
{
static bool s_trueConst = true;
static bool s_falseConst = false;

interface I
{
static virtual bool GetValue() => false;
}

class C : I
{
static bool I.GetValue() => true;
}

public static void Run()
{
if (!Call<C>())
throw new Exception();
}
static bool Call<T>() where T : I
{
if (T.GetValue())
return s_trueConst;
return s_falseConst;
}
}

class TestInstanceMethodOptimization
{
class UnreferencedType { }
Expand Down

0 comments on commit efdd299

Please sign in to comment.