Skip to content

Lua-CSharp can't handle break statement properly #215

@yuychu

Description

@yuychu

In the following code, the expected output when running in standard Lua should be 50. However, when executed in the Lua-CSharp implementation, the actual output is 100.

local n = 0
while n < 100 do
    n = n + 1
    if n == 50 then break end
    while false do
        n = n + 7
        break
    end
end
print(n)

Expected Behavior
Output:
50
Actual Behavior (Lua-CSharp)
Output:
100

After reviewing the code, I believe there is an issue with how the compiler handles break statements. The break queue is maintained in the FunctionCompilationContext, and all scopes share a single queue. This causes a problem when compiling a nested while statement: while compiling the inner while, the break queue is processed in such a way that breaks belonging to the outer while loop are also resolved, leading to incorrect jump targets.

In fact, during additional testing with other examples, I found that this behavior can sometimes result in unintended infinite loops.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions