diff --git a/src/linker/Linker.Steps/UnreachableBlocksOptimizer.cs b/src/linker/Linker.Steps/UnreachableBlocksOptimizer.cs index b1adf6de6fd7..8e8fda213e32 100644 --- a/src/linker/Linker.Steps/UnreachableBlocksOptimizer.cs +++ b/src/linker/Linker.Steps/UnreachableBlocksOptimizer.cs @@ -838,6 +838,10 @@ bool RemoveConditions () var opcode = instr.OpCode; if (opcode.FlowControl == FlowControl.Cond_Branch) { + // No support for removing branches from switch instruction + if (opcode == OpCodes.Switch) + continue; + if (opcode.StackBehaviourPop == StackBehaviour.Pop1_pop1) { if (!GetOperandsConstantValues (i, out left, out right)) continue; @@ -892,7 +896,7 @@ bool RemoveConditions () } // Common pattern generated by C# compiler in debug mode - if (i > 3 && GetConstantValue (FoldedInstructions[i - 3], out operand) && operand is int opint2 && IsPairedStlocLdloc (FoldedInstructions[i - 2], FoldedInstructions[i - 1])) { + if (i >= 3 && GetConstantValue (FoldedInstructions[i - 3], out operand) && operand is int opint2 && IsPairedStlocLdloc (FoldedInstructions[i - 2], FoldedInstructions[i - 1])) { if (IsJumpTargetRange (i - 2, i)) continue; diff --git a/test/Mono.Linker.Tests.Cases/UnreachableBlock/ComplexConditionsOptimized.cs b/test/Mono.Linker.Tests.Cases/UnreachableBlock/ComplexConditionsOptimized.cs index 85a5ecdda225..a59e0cdc3aba 100644 --- a/test/Mono.Linker.Tests.Cases/UnreachableBlock/ComplexConditionsOptimized.cs +++ b/test/Mono.Linker.Tests.Cases/UnreachableBlock/ComplexConditionsOptimized.cs @@ -27,9 +27,6 @@ static int KnownInteger { [ExpectBodyModified] public static void Test () { - // https://github.com/dotnet/linker/issues/2872 - // Uncomment the call to make the test crash the linker - //Helper (); switch (KnownInteger) { case 0: Unreached (); @@ -44,17 +41,13 @@ public static void Test () } } - // https://github.com/dotnet/linker/issues/2872 + // https://github.com/dotnet/linker/issues/2888 // Should be removed [Kept] static void Unreached () { } [Kept] static void Reached () { } - - // https://github.com/dotnet/linker/issues/2872 - //[Kept] - static void Helper () { } } } } \ No newline at end of file