Commit 9c5cb739

9c5cb739574eMerged

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

Description

instance_eval/exec value position: block-local return type and statement splice

Two fixes for direct instance_eval/exec (and trampoline) calls whose block has more than a single trailing expression:

  • infer_write_types gains a second targeted pass for x = recv.instance_eval/exec { ... }: the call's value is the block's last expression, which can read a block-body local defined at a higher node id than this write. Those locals are only typed by the main write loop, so a single ascending pass left x stranded at UNKNOWN. Recompute after the main loop so the value type resolves.
  • The instance_eval/exec re-infer pass (which re-types block bodies under the receiver's class) now also covers instance_exec and trampoline calls, not just instance_eval.
  • In statement position the block's value is discarded, so the splice now emits the whole body as statements (via g_ie_discard_value) instead of coercing the last node to an expression -- a trailing puts is not expressible and previously errored.

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

Details

Commit
9c5cb739574e
Parent
0c6a772c847c

src/analyze.cmodifiedView file

@@ -4405,6 +4405,33 @@
44054405 lv->type = ty_unify(lv->type, newt);
44064406 }
44074407
4408+ /* Second targeted pass for `x = recv.instance_eval/exec { ... }` (and
4409+ trampoline calls): the call's value is the block's last expression, which
4410+ may read a block-body local defined at a higher node id than this write.
4411+ Those locals were just typed by the main loop above, so recompute here so
4412+ `x` is not stranded at UNKNOWN by within-pass node ordering. */
4413+ for (int id = 0; id < nt->count; id++) {
4414+ if (strcmp(nt_type(nt, id) ? nt_type(nt, id) : "", "LocalVariableWriteNode")) continue;
4415+ int val_id = nt_ref(nt, id, "value");
4416+ if (val_id < 0 || strcmp(nt_type(nt, val_id) ? nt_type(nt, val_id) : "", "CallNode")) continue;
4417+ if (nt_ref(nt, val_id, "block") < 0) continue;
4418+ const char *vnm = nt_str(nt, val_id, "name");
4419+ int vrecv = nt_ref(nt, val_id, "receiver");
4420+ if (!vnm || vrecv < 0) continue;
4421+ int is_ie = !strcmp(vnm, "instance_eval") || !strcmp(vnm, "instance_exec");
4422+ if (!is_ie) {
4423+ TyKind vrt = infer_type(c, vrecv);
4424+ if (!ty_is_object(vrt) || !comp_trampoline_kind(c, ty_object_class(vrt), vnm, NULL)) continue;
4425+ }
4426+ const char *nm = nt_str(nt, id, "name");
4427+ LocalVar *lv = nm ? scope_local(comp_scope_of(c, id), nm) : NULL;
4428+ if (!lv || lv->is_param || lv->is_block_param) continue;
4429+ TyKind newt = infer_type(c, val_id);
4430+ if (newt == TY_NIL) newt = TY_POLY;
4431+ TyKind m2 = ty_unify(lv->type, newt);
4432+ if (m2 != lv->type) { lv->type = m2; changed = 1; }
4433+ }
4434+
44084435 /* Multiple assignment `a, b = e0, e1`: each target gets its element's
44094436 type (the RHS ArrayNode is a tuple here, not an array value). */
44104437 for (int id = 0; id < nt->count; id++) {
@@ -7675,13 +7702,17 @@
76757702 const char *ty2 = nt_type(c->nt, id);
76767703 if (!ty2 || strcmp(ty2, "CallNode")) continue;
76777704 const char *nm2 = nt_str(c->nt, id, "name");
7678- if (!nm2 || strcmp(nm2, "instance_eval")) continue;
7705+ if (!nm2) continue;
76797706 int blk2 = nt_ref(c->nt, id, "block");
76807707 int recv2 = nt_ref(c->nt, id, "receiver");
76817708 if (blk2 < 0 || recv2 < 0) continue;
76827709 TyKind rt2 = c->ntype[recv2];
76837710 if (!ty_is_object(rt2)) continue;
7684- if (comp_method_in_chain(c, ty_object_class(rt2), "instance_eval", NULL) >= 0) continue;
7711+ int is_ie2 = !strcmp(nm2, "instance_eval") || !strcmp(nm2, "instance_exec");
7712+ if (is_ie2) {
7713+ if (comp_method_in_chain(c, ty_object_class(rt2), nm2, NULL) >= 0) continue;
7714+ }
7715+ else if (!comp_trampoline_kind(c, ty_object_class(rt2), nm2, NULL)) continue;
76857716 int bdy2 = nt_ref(c->nt, blk2, "body");
76867717 if (bdy2 < 0) continue;
76877718 int bn2 = 0; const int *bb2 = nt_arr(c->nt, bdy2, "body", &bn2);