Commit 34f8cd54

34f8cd5461a2Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 10 files · +6 added · ~4 modified

Description

Allow break/next inside a direct instance_exec splice

A direct recv.instance_exec { ... } runs in the caller's frame, so break <v> / next <v> set the call's value and return exits the enclosing method. return already worked; break/next emitted a C break/continue with no enclosing loop ("break statement not within loop").

When the splice body has a break/next that binds to it (not one consumed by a nested loop or block -- ie_body_has_break_next stops at While/Until/For/ Block/Lambda/Def/Class), wrap the body in do{}while(0): a top-level break exits it, next continues out of the single iteration. break <v> captures into the result temp via the existing g_loop_break_var; next <v> via a new g_ie_next_var. A break/next inside a nested each {...} still binds to that loop (the inliner save/restores g_loop_break_var), and a stray nested next <v> is harmless since the fall-through last expression overwrites the temp.

Adds instance_exec_break / _next; nonlocal_return already worked and gains a regression test.

Fixes #1376 Co-Authored-By: Claude Opus 4.8 (1M context) [email protected]

Details

Commit
34f8cd5461a2
Parent
84bdebc0e2b3

src/codegen_call.cmodifiedView file

@@ -130,6 +130,25 @@
130130 return 1;
131131 }
132132
133+/* Does `node` (an instance_exec body subtree) contain a break/next that binds
134+ to the splice itself -- i.e. not consumed by a nested loop or block? */
135+static int ie_body_has_break_next(Compiler *c, int node) {
136+ const NodeTable *nt = c->nt;
137+ if (node < 0) return 0;
138+ const char *ty = nt_type(nt, node);
139+ if (!ty) return 0;
140+ if (!strcmp(ty, "BreakNode") || !strcmp(ty, "NextNode")) return 1;
141+ /* constructs that bind their own break/next */
142+ if (!strcmp(ty, "WhileNode") || !strcmp(ty, "UntilNode") || !strcmp(ty, "ForNode") ||
143+ !strcmp(ty, "BlockNode") || !strcmp(ty, "LambdaNode") || !strcmp(ty, "DefNode") ||
144+ !strcmp(ty, "ClassNode") || !strcmp(ty, "ModuleNode")) return 0;
145+ int nr = nt_num_refs(nt, node);
146+ for (int i = 0; i < nr; i++) if (ie_body_has_break_next(c, nt_ref_at(nt, node, i))) return 1;
147+ int na = nt_num_arrs(nt, node);
148+ for (int i = 0; i < na; i++) { int n = 0; const int *ids = nt_arr_at(nt, node, i, &n); for (int k = 0; k < n; k++) if (ie_body_has_break_next(c, ids[k])) return 1; }
149+ return 0;
150+}
151+
133152 void emit_call(Compiler *c, int id, Buf *b) {
134153 const NodeTable *nt = c->nt;
135154 if (emit_partition_expr(c, id, b)) return;
@@ -1856,6 +1875,18 @@
18561875 int last_as_stmt = g_ie_discard_value && !scalar_res;
18571876 int upto = last_as_stmt ? ie_bn : ie_bn - 1;
18581877 int saved_discard = g_ie_discard_value; g_ie_discard_value = 0;
1878+ /* A break/next that binds to the splice (not a nested loop) needs a C
1879+ loop to target: wrap the body in do{}while(0). `break <v>` captures
1880+ into the result temp via g_loop_break_var; `next <v>` via
1881+ g_ie_next_var. A `return` still returns from the enclosing function. */
1882+ int ie_bn_wrap = ie_body_has_break_next(c, blk_body);
1883+ const char *sv_lb = g_loop_break_var, *sv_nx = g_ie_next_var;
1884+ char bvbuf[32];
1885+ if (ie_bn_wrap) {
1886+ emit_indent(g_pre, g_indent); buf_puts(g_pre, "do {\n"); g_indent++;
1887+ if (scalar_res) { snprintf(bvbuf, sizeof bvbuf, "_t%d", tres); g_loop_break_var = bvbuf; g_ie_next_var = bvbuf; }
1888+ else { g_loop_break_var = NULL; g_ie_next_var = NULL; }
1889+ }
18591890 for (int j = 0; j < upto; j++) emit_stmt(c, ie_bb[j], g_pre, g_indent);
18601891 if (!last_as_stmt) {
18611892 Buf vb; memset(&vb, 0, sizeof vb);
@@ -1871,6 +1902,10 @@
18711902 }
18721903 free(vb.p);
18731904 }
1905+ if (ie_bn_wrap) {
1906+ g_loop_break_var = sv_lb; g_ie_next_var = sv_nx;
1907+ g_indent--; emit_indent(g_pre, g_indent); buf_puts(g_pre, "} while (0);\n");
1908+ }
18741909 g_ie_discard_value = saved_discard;
18751910 }
18761911 g_ie_class_id = saved_ie;