[core] Poison brace-depth tracking once it goes negative

Address Copilot review: the prior comment promised that a negative depth would never re-enable splitting, but an arithmetically-balanced later { could bring depth back to 0 and resume flushing mid-stream. Track an explicit 'poisoned' flag set once depth < 0 that permanently disables further flushes for the rest of the input. Adds a regression test where a leading } and a later { would have re-enabled splitting without the poison flag.
This commit is contained in:
J. Nick Koston
2026-04-17 19:01:12 -05:00
parent 6446f309c1
commit 4ea417966d
2 changed files with 29 additions and 4 deletions
+14
View File
@@ -943,6 +943,20 @@ def test_wrap_in_iifes_unbalanced_braces_fall_through() -> None:
assert [line for line in result if line in lines] == lines
def test_wrap_in_iifes_negative_depth_stays_poisoned() -> None:
# Once depth goes negative the brace tracker is unreliable; a later
# arithmetic return to depth 0 must not re-enable splitting. Here
# "}" drives depth to -1 immediately, then "{" later brings depth
# back to 0 arithmetically. With max=1 every statement would flush
# if splitting were still enabled — assert everything emits as one
# IIFE because the poisoned flag stayed set.
lines = ["}", "a();", "{", "b();", "}", "c();"]
result = core._wrap_in_iifes(lines, max_statements=1)
assert sum(1 for line in result if line == "[]() {") == 1
assert result[0] == "[]() {"
assert result[-1] == "}();"
def test_wrap_in_iifes_never_splits_inline_brace_lines() -> None:
# Defensive: if codegen ever emits control flow with braces on the
# same line (if/else/for), the depth tracker should keep the whole