Commit 349ff17e
349ff17e6875Merged
Erik Peterson <[email protected]> · 5 months ago · 1 file · ~1 modified
Description
codegen: case with a loop-targeting break must use if-else, not a C switch
A Ruby break inside a when clause breaks the enclosing loop. The case
fast-path lowered case <int> when <int literals> to a C switch, but a C
break inside a switch exits the SWITCH, not the loop -- so the loop kept
running. The Tungsten lexer's scan_string is exactly this:
loop do
case byte
when 34 # close quote
advance
break # must break the loop
...
compiled to a switch whose break only left the switch, so after the closing
quote the loop continued to EOF -> "unterminated string" on every string
literal.
Fix: new subtree_has_loop_break() detects a break that targets the enclosing
loop (not one captured by a nested loop / block-bearing iterator). When any
when/else body has such a break, skip the switch fast-path and use the if-else
form, where C break correctly targets the loop. The perf-critical
integer-dispatch switch (optcarrot opcodes, no loop-break in arms) is
unaffected.
Full test suite green: 894 pass / 0 fail / 0 error. With this + the loop-condition-prelude fix, the migrated stage0 lexer+parser now fully parse compiler/tungsten.w (bounded memory, seconds) and reach the evaluation phase.
Co-Authored-By: Claude Opus 4.8 (1M context) [email protected]
Details
- Commit
- 349ff17e6875
- Parent
- fdeb639a7db1
src/codegen_stmt.cmodifiedView file
}/* case/when -> an if / else-if chain. Statement form. *//* True if the subtree contains a `break` that targets the ENCLOSING loop (i.e. not captured by a nested loop or block-bearing iterator). Used to decide whether a `case` may lower to a C `switch`: a Ruby `break` inside a `when` must break the loop, but a C `break` inside a `switch` only exits the switch -- so a case whose when-bodies break the loop must use the if-else form instead, where C `break` correctly targets the loop. */static int subtree_has_loop_break(Compiler *c, int root) { if (root < 0) return 0; const NodeTable *nt = c->nt; const char *ty = nt_type(nt, root); if (ty) { if (!strcmp(ty, "BreakNode")) return 1; /* nested loops / block-bearing iterators capture their own break/next */ if (!strcmp(ty, "WhileNode") || !strcmp(ty, "UntilNode") || !strcmp(ty, "ForNode")) return 0; if (!strcmp(ty, "CallNode") && nt_ref(nt, root, "block") >= 0) return 0; } int nr = nt_num_refs(nt, root); for (int i = 0; i < nr; i++) if (subtree_has_loop_break(c, nt_ref_at(nt, root, i))) return 1; int na = nt_num_arrs(nt, root); for (int i = 0; i < na; i++) { int n = 0; const int *el = nt_arr_at(nt, root, i, &n); for (int j = 0; j < n; j++) if (subtree_has_loop_break(c, el[j])) return 1; } return 0;}void emit_case(Compiler *c, int id, Buf *b, int indent) { const NodeTable *nt = c->nt; int pred = nt_ref(nt, id, "predicate"); if (!cty || strcmp(cty, "IntegerNode")) { all_int = 0; break; } } } if (all_int) { /* A C switch can't host a Ruby `break` that targets the enclosing loop (C break exits the switch, not the loop). If any arm breaks the loop, fall through to the if-else form below where break works correctly. */ int has_lbreak = (else_clause >= 0) && subtree_has_loop_break(c, nt_ref(nt, else_clause, "statements")); for (int w = 0; w < nw && !has_lbreak; w++) if (subtree_has_loop_break(c, nt_ref(nt, whens[w], "statements"))) has_lbreak = 1; if (all_int && !has_lbreak) { emit_indent(b, indent); if (pt == TY_POLY) buf_printf(b, "switch (sp_poly_to_i(_t%d)) {\n", t); else buf_printf(b, "switch (_t%d) {\n", t);