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

@@ -1175,6 +1175,34 @@
11751175 }
11761176
11771177 /* case/when -> an if / else-if chain. Statement form. */
1178+/* True if the subtree contains a `break` that targets the ENCLOSING loop
1179+ (i.e. not captured by a nested loop or block-bearing iterator). Used to
1180+ decide whether a `case` may lower to a C `switch`: a Ruby `break` inside a
1181+ `when` must break the loop, but a C `break` inside a `switch` only exits the
1182+ switch -- so a case whose when-bodies break the loop must use the if-else
1183+ form instead, where C `break` correctly targets the loop. */
1184+static int subtree_has_loop_break(Compiler *c, int root) {
1185+ if (root < 0) return 0;
1186+ const NodeTable *nt = c->nt;
1187+ const char *ty = nt_type(nt, root);
1188+ if (ty) {
1189+ if (!strcmp(ty, "BreakNode")) return 1;
1190+ /* nested loops / block-bearing iterators capture their own break/next */
1191+ if (!strcmp(ty, "WhileNode") || !strcmp(ty, "UntilNode") || !strcmp(ty, "ForNode"))
1192+ return 0;
1193+ if (!strcmp(ty, "CallNode") && nt_ref(nt, root, "block") >= 0)
1194+ return 0;
1195+ }
1196+ int nr = nt_num_refs(nt, root);
1197+ for (int i = 0; i < nr; i++) if (subtree_has_loop_break(c, nt_ref_at(nt, root, i))) return 1;
1198+ int na = nt_num_arrs(nt, root);
1199+ for (int i = 0; i < na; i++) {
1200+ int n = 0; const int *el = nt_arr_at(nt, root, i, &n);
1201+ for (int j = 0; j < n; j++) if (subtree_has_loop_break(c, el[j])) return 1;
1202+ }
1203+ return 0;
1204+}
1205+
11781206 void emit_case(Compiler *c, int id, Buf *b, int indent) {
11791207 const NodeTable *nt = c->nt;
11801208 int pred = nt_ref(nt, id, "predicate");
@@ -1208,7 +1236,13 @@
12081236 if (!cty || strcmp(cty, "IntegerNode")) { all_int = 0; break; }
12091237 }
12101238 }
1211- if (all_int) {
1239+ /* A C switch can't host a Ruby `break` that targets the enclosing loop
1240+ (C break exits the switch, not the loop). If any arm breaks the loop,
1241+ fall through to the if-else form below where break works correctly. */
1242+ int has_lbreak = (else_clause >= 0) && subtree_has_loop_break(c, nt_ref(nt, else_clause, "statements"));
1243+ for (int w = 0; w < nw && !has_lbreak; w++)
1244+ if (subtree_has_loop_break(c, nt_ref(nt, whens[w], "statements"))) has_lbreak = 1;
1245+ if (all_int && !has_lbreak) {
12121246 emit_indent(b, indent);
12131247 if (pt == TY_POLY) buf_printf(b, "switch (sp_poly_to_i(_t%d)) {\n", t);
12141248 else buf_printf(b, "switch (_t%d) {\n", t);