Commit 4f9a7187

4f9a7187ce3bMerged

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

Description

codegen,analyze: size instance_exec result by its break/next value types

The instance_exec/eval splice result temp was typed from the block's last expression alone. A value-carrying next <v>/break <v> bound to the splice can be wider -- e.g. next val + 1 is poly (val is an int ivar widened in promote mode) while the trailing 999 is int -- so the poly value landed in an mrb_int slot. Unify the result type over the last expression and every splice-bound break/next value (matching helpers in analyze_infer and codegen), and box a scalar break/next/last value when the slot is poly via a new g_ie_res_poly flag. Fixes instance_exec_next, instance_exec_break.

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

Details

Commit
4f9a7187ce3b
Parent
eacedad8da2e

src/analyze_infer.cmodifiedView file

@@ -28,6 +28,40 @@
2828 g_nmemo[slot].gen = g_narrow_gen; g_nmemo[slot].key = key; g_nmemo[slot].val = (signed char)val;
2929 }
3030
31+/* Unify the value type of every splice-bound break/next in `node` (break/next
32+ inside a nested loop or block bind there, not to the splice). TY_UNKNOWN if
33+ none carry a value. Mirrors codegen's ie_splice_value_ty so the inferred
34+ instance_exec result type matches the slot codegen sizes. */
35+static TyKind ie_block_break_next_ty(Compiler *c, int node) {
36+ const NodeTable *nt = c->nt;
37+ if (node < 0) return TY_UNKNOWN;
38+ const char *ty = nt_type(nt, node);
39+ if (!ty) return TY_UNKNOWN;
40+ if (!strcmp(ty, "BreakNode") || !strcmp(ty, "NextNode")) {
41+ int a = nt_ref(nt, node, "arguments"); int an = 0;
42+ const int *av = a >= 0 ? nt_arr(nt, a, "arguments", &an) : NULL;
43+ return an > 0 ? infer_type(c, av[0]) : TY_UNKNOWN;
44+ }
45+ if (!strcmp(ty, "WhileNode") || !strcmp(ty, "UntilNode") || !strcmp(ty, "ForNode") ||
46+ !strcmp(ty, "BlockNode") || !strcmp(ty, "LambdaNode") || !strcmp(ty, "DefNode") ||
47+ !strcmp(ty, "ClassNode") || !strcmp(ty, "ModuleNode")) return TY_UNKNOWN;
48+ TyKind r = TY_UNKNOWN;
49+ int nr = nt_num_refs(nt, node);
50+ for (int i = 0; i < nr; i++) {
51+ TyKind s = ie_block_break_next_ty(c, nt_ref_at(nt, node, i));
52+ if (s != TY_UNKNOWN) r = (r == TY_UNKNOWN) ? s : ty_unify(r, s);
53+ }
54+ int na = nt_num_arrs(nt, node);
55+ for (int i = 0; i < na; i++) {
56+ int n = 0; const int *ids = nt_arr_at(nt, node, i, &n);
57+ for (int k = 0; k < n; k++) {
58+ TyKind s = ie_block_break_next_ty(c, ids[k]);
59+ if (s != TY_UNKNOWN) r = (r == TY_UNKNOWN) ? s : ty_unify(r, s);
60+ }
61+ }
62+ return r;
63+}
64+
3165 /* Whether every element written into the poly-array ivar `@<ivname>` of class
3266 `cid` is an int-returning kind: a bound method (a dispatch-table entry, called
3367 with an int arg returns int), an int array, an int, or nil filler. Mirrors
@@ -471,7 +505,16 @@
471505 }
472506 int body = nt_ref(nt, blk, "body");
473507 int bn = 0; const int *bb = body >= 0 ? nt_arr(nt, body, "body", &bn) : NULL;
474- if (bn > 0) { TyKind bt = infer_type(c, bb[bn - 1]); return bt == TY_VOID ? TY_NIL : bt; }
508+ if (bn > 0) {
509+ TyKind bt = infer_type(c, bb[bn - 1]);
510+ if (bt == TY_VOID) bt = TY_NIL;
511+ /* A value-carrying break/next can widen the result past the last
512+ expression (e.g. `next val + 1` poly vs trailing `999` int). */
513+ TyKind bnt = ie_block_break_next_ty(c, body);
514+ if (bnt != TY_UNKNOWN)
515+ bt = (bt == TY_NIL || bt == TY_UNKNOWN) ? bnt : ty_unify(bt, bnt);
516+ return bt;
517+ }
475518 return TY_NIL;
476519 }
477520 }