Skip to content

Commit d62b152

Browse files
authored
Fix literal conditional branch emission (#2756)
Fixes #2747 Copilot-Session: d27ee0ef-46db-423b-93dc-db64f8eae1ff
1 parent 47819f5 commit d62b152

2 files changed

Lines changed: 38 additions & 0 deletions

File tree

src/Core/CodeAnalysis/Emit/MethodBodyEmitter.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1105,6 +1105,16 @@ private void EmitBranch(BoundLabel target, BoundExpression conditional, bool jum
11051105
var crossesRegion = this.protectedRegionStack.Count > 0
11061106
&& !this.protectedRegionStack.Peek().Contains(target);
11071107

1108+
if (conditional is BoundLiteralExpression { Value: bool value })
1109+
{
1110+
if (value == jumpIfTrue)
1111+
{
1112+
this.il.Branch(crossesRegion ? ILOpCode.Leave : ILOpCode.Br, targetHandle);
1113+
}
1114+
1115+
return;
1116+
}
1117+
11081118
if (conditional == null)
11091119
{
11101120
this.il.Branch(crossesRegion ? ILOpCode.Leave : ILOpCode.Br, targetHandle);

test/Compiler.Tests/Emit/Issue2283SwitchAllTerminalArmsEmitTests.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,34 @@ namespace GSharp.Compiler.Tests.Emit;
4242
/// </summary>
4343
public class Issue2283SwitchAllTerminalArmsEmitTests
4444
{
45+
[Fact]
46+
public void LiteralTrueLoopEndingInReturn_VerifiesAndRuns()
47+
{
48+
const string source = """
49+
package Issue2747.InfiniteLoop
50+
import System
51+
52+
class Login {
53+
func Callback() Uri {
54+
while true {
55+
let text = "https://example.com"
56+
var uri Uri? = nil
57+
if !Uri.TryCreate(text, UriKind.Absolute, out uri) {
58+
continue
59+
}
60+
61+
return uri!!
62+
}
63+
}
64+
}
65+
66+
Console.WriteLine(Login().Callback().Host)
67+
""";
68+
69+
var output = CompileAndRun(source);
70+
Assert.Equal("example.com\n", output);
71+
}
72+
4573
[Fact]
4674
public void AllArmsTerminal_AsLastStatement_VerifiesAndRuns()
4775
{

0 commit comments

Comments
 (0)