Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not eliminate conditional branch if it falls through to a different exception handler. #29517

Merged
merged 1 commit into from Sep 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
70 changes: 70 additions & 0 deletions src/Compilers/CSharp/Test/Emit/CodeGen/CodeGenTryFinally.cs
Expand Up @@ -3683,6 +3683,76 @@ public static void Main(string[] args)
.maxstack 0
IL_0000: ret
}
");
}

[Fact]
[WorkItem(29481, "https://github.com/dotnet/roslyn/issues/29481")]
public void Issue29481()
{
var source = @"
using System;

public class Program
{
public static void Main()
{
try
{
bool b = false;
if (b)
{
try
{
return;
}
finally
{
Console.WriteLine(""Prints"");
}
}
else
{
return;
}
}
finally
{
GC.KeepAlive(null);
}
}
}";

CompileAndVerify(source, expectedOutput: "", options: TestOptions.DebugExe);
CompileAndVerify(source, expectedOutput: "", options: TestOptions.ReleaseExe).VerifyIL("Program.Main",
@"
{
// Code size 26 (0x1a)
.maxstack 1
.try
{
IL_0000: ldc.i4.0
IL_0001: brfalse.s IL_0010
.try
{
IL_0003: leave.s IL_0019
}
finally
{
IL_0005: ldstr ""Prints""
IL_000a: call ""void System.Console.WriteLine(string)""
IL_000f: endfinally
}
IL_0010: leave.s IL_0019
}
finally
{
IL_0012: ldnull
IL_0013: call ""void System.GC.KeepAlive(object)""
IL_0018: endfinally
}
IL_0019: ret
}
");
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/Compilers/Core/Portable/CodeGen/BasicBlock.cs
Expand Up @@ -518,7 +518,8 @@ private bool TryOptimizeBranchToNextOrRet(BasicBlock next, ref int delta)
private bool TryOptimizeBranchToEquivalent(BasicBlock next, ref int delta)
{
var curBranchCode = this.BranchCode;
if (curBranchCode.IsConditionalBranch())
if (curBranchCode.IsConditionalBranch() &&
next.EnclosingHandler == this.EnclosingHandler)
{
// check for branch to next,
// or if both blocks are identical
Expand Down
Expand Up @@ -1015,6 +1015,69 @@ End Module
CompileAndVerify(compilation.WithOptions(TestOptions.ReleaseExe), expectedOutput:="Exited Try")
End Sub

<Fact>
<WorkItem(29481, "https://github.com/dotnet/roslyn/issues/29481")>
Public Sub Issue29481()
Dim source =
<compilation>
<file name="a.vb">
Imports System

Class C
Shared Sub Main()
try
Dim b As Boolean = false
if b
try
return
finally
Console.WriteLine("Prints")
end try
else
return
end if
finally
GC.KeepAlive(Nothing)
end try
End Sub
End Class
</file>
</compilation>

CompileAndVerify(source, expectedOutput:="", options:=TestOptions.DebugExe)
CompileAndVerify(source, expectedOutput:="", options:=TestOptions.ReleaseExe).
VerifyIL("C.Main",
<![CDATA[
{
// Code size 26 (0x1a)
.maxstack 1
.try
{
IL_0000: ldc.i4.0
IL_0001: brfalse.s IL_0010
.try
{
IL_0003: leave.s IL_0019
}
finally
{
IL_0005: ldstr "Prints"
IL_000a: call "Sub System.Console.WriteLine(String)"
IL_000f: endfinally
}
IL_0010: leave.s IL_0019
}
finally
{
IL_0012: ldnull
IL_0013: call "Sub System.GC.KeepAlive(Object)"
IL_0018: endfinally
}
IL_0019: ret
}
]]>)
End Sub

End Class
End Namespace