Commit 0c6a772c

0c6a772c847cMerged

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

Description

instance_eval: rebound self for ivar/param receivers and trailing attr-writes

Three fixes let recv.instance_eval { ... } work when it sits inside a class method and when its block ends in an attribute write:

  • Bare-call dispatch inside an instance_eval splice now prefers the rebound receiver class (g_ie_class_id) over the enclosing method's class (g_emitting_class_id). A @routes.instance_eval { get(...) } inside a Boot method must dispatch get against Routes, not Boot.
  • self inside an instance_eval block infers to the rebound receiver class (via g_ie_class_id / the ie node->class map) instead of TY_UNKNOWN, so self.attr = v resolves the writer.
  • Added an expression-level obj.attr = val writer that stores into the ivar and yields the assigned value, hit when the assignment is the block's last statement (the splice emits the tail as an expression).

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

Details

Commit
0c6a772c847c
Parent
c722dfb32b3e

src/analyze.cmodifiedView file

@@ -2413,8 +2413,11 @@
24132413 }
24142414 if (!strcmp(ty, "SelfNode")) {
24152415 Scope *s = comp_scope_of(c, id);
2416- if (s->class_id < 0) return TY_UNKNOWN;
2417- const char *cn = c->classes[s->class_id].name;
2416+ int self_cls = s->class_id;
2417+ /* `self` inside an instance_eval/exec block is the rebound receiver. */
2418+ if (self_cls < 0) self_cls = (g_ie_class_id >= 0) ? g_ie_class_id : ie_class_of(c, id);
2419+ if (self_cls < 0) return TY_UNKNOWN;
2420+ const char *cn = c->classes[self_cls].name;
24182421 if (!strcmp(cn, "String")) return TY_STRING;
24192422 if (!strcmp(cn, "Integer")) return TY_INT;
24202423 if (!strcmp(cn, "Float")) return TY_FLOAT;
@@ -2422,7 +2425,7 @@
24222425 if (!strcmp(cn, "TrueClass") || !strcmp(cn, "FalseClass") || !strcmp(cn, "NilClass")) return TY_BOOL;
24232426 if (!strcmp(cn, "Array")) return TY_POLY_ARRAY;
24242427 if (!strcmp(cn, "Object")) return TY_POLY; /* dynamic: called on any receiver type */
2425- return ty_object(s->class_id);
2428+ return ty_object(self_cls);
24262429 }
24272430 if (!strcmp(ty, "InstanceVariableReadNode")) {
24282431 const char *nm = nt_str(nt, id, "name");