Commit 138302d3

138302d3ddc8Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 1 file · ~1 modified

Description

codegen: default-init and box-break a poly-result loop{} expression

r = loop { break v } in expression position declared its result temp with a literal NULL for any non-int type, invalid for a poly (sp_RbVal) slot (break x with x widened). Use default_value() for the initializer and set g_ie_res_poly around the body so a scalar break <v> boxes into the poly slot. Fixes i916.

Co-Authored-By: Claude Opus 4.8 [email protected]

Details

Commit
138302d3ddc8
Parent
5396dc78c1f4

src/codegen_call.cmodifiedView file

@@ -3597,14 +3597,18 @@
35973597 if (bt != TY_UNKNOWN && bt != TY_NIL) {
35983598 int t = ++g_tmp;
35993599 emit_indent(g_pre, g_indent); emit_ctype(c, bt, g_pre);
3600- buf_printf(g_pre, " _t%d = %s;\n", t, (bt == TY_INT) ? "0" : "NULL");
3600+ buf_printf(g_pre, " _t%d = %s;\n", t,
3601+ bt == TY_RANGE ? "(sp_Range){0}" : default_value(bt));
36013602 emit_indent(g_pre, g_indent); buf_puts(g_pre, "for (;;) {\n");
36023603 const char *sv_lb = g_loop_break_var;
3604+ int sv_iep = g_ie_res_poly;
3605+ g_ie_res_poly = (bt == TY_POLY); /* box a scalar `break <v>` into the poly slot */
36033606 char lb_buf[32]; snprintf(lb_buf, sizeof lb_buf, "_t%d", t);
36043607 g_loop_break_var = lb_buf;
36053608 int lbody = nt_ref(nt, blk, "body");
36063609 emit_stmts(c, lbody, g_pre, g_indent + 1);
36073610 g_loop_break_var = sv_lb;
3611+ g_ie_res_poly = sv_iep;
36083612 emit_indent(g_pre, g_indent); buf_puts(g_pre, "}\n");
36093613 buf_printf(b, "_t%d", t);
36103614 return;