From 725922e3591fd18bb3cdc2d227075d534f1c755f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20Strehovsk=C3=BD?= Date: Thu, 2 Jan 2025 09:44:21 +0100 Subject: [PATCH] Do not substitute return values of constrained calls Fixes #110932. The constraint would need to be resolved first. --- .../Compiler/SubstitutedILProvider.cs | 4 +-- .../TrimmingBehaviors/DeadCodeElimination.cs | 29 +++++++++++++++++++ 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/SubstitutedILProvider.cs b/src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/SubstitutedILProvider.cs index 6b339f8a89d2e3..fd2442c4b6b41c 100644 --- a/src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/SubstitutedILProvider.cs +++ b/src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/SubstitutedILProvider.cs @@ -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; diff --git a/src/tests/nativeaot/SmokeTests/TrimmingBehaviors/DeadCodeElimination.cs b/src/tests/nativeaot/SmokeTests/TrimmingBehaviors/DeadCodeElimination.cs index 0a6090f8092432..032c3a07d7dea6 100644 --- a/src/tests/nativeaot/SmokeTests/TrimmingBehaviors/DeadCodeElimination.cs +++ b/src/tests/nativeaot/SmokeTests/TrimmingBehaviors/DeadCodeElimination.cs @@ -12,6 +12,7 @@ class DeadCodeElimination public static int Run() { SanityTest.Run(); + Test110932Regression.Run(); TestInstanceMethodOptimization.Run(); TestReflectionInvokeSignatures.Run(); TestAbstractTypeNeverDerivedVirtualsOptimization.Run(); @@ -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()) + throw new Exception(); + } + static bool Call() where T : I + { + if (T.GetValue()) + return s_trueConst; + return s_falseConst; + } + } + class TestInstanceMethodOptimization { class UnreferencedType { }