Commit aabdaf46

aabdaf462947Merged

Ryan Seys <[email protected]> · 5 months ago · 3 files · +2 added · ~1 modified

Description

feat(analyze): slot ivars first assigned inside an instance_exec/eval block

An ivar whose first (or only) assignment is inside an instance_exec / instance_eval block had no struct slot on the receiver's class: register_locals only interns ivar writes whose enclosing scope is a class body or method, and a lifted block's scope is the toplevel/method, not the receiver. Reading such an ivar then failed to resolve.

Add register_ie_block_ivars, run in the inference fixpoint right after build_ie_map: for each ivar write whose node maps to a receiver class (g_ie_node_class), intern the slot on that class and type it from the assignment value. An ivar written only inside iexec blocks has no competing writer, so widening from the value type is safe; the pass returns 1 on a change so the fixpoint re-runs and propagates the new ivar's type.

Adds test/instance_exec_dynamic_ivar.rb (int ivar assigned + read in one block, string ivar assigned in an instance_eval and read in a later instance_exec, and a statically-declared ivar still resolving).

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

Details

Commit
aabdaf462947
Parent
d40bc31b97a3

src/analyze.cmodifiedView file

@@ -578,6 +578,49 @@
578578 return (g_ie_node_class && node >= 0) ? g_ie_node_class[node] : -1;
579579 }
580580
581+/* Register an ivar first assigned inside an instance_exec/instance_eval block on
582+ the block's receiver class. register_locals only interns ivar writes whose
583+ enclosing scope is a class body or method; an ivar written solely inside a
584+ lifted iexec block has no such scope, so without this it gets no struct slot
585+ (and any read of it fails to resolve). Runs in the fixpoint right after
586+ build_ie_map; returns 1 when it adds a new slot so the fixpoint re-runs and
587+ infers the new ivar's type from its assignment. */
588+int register_ie_block_ivars(Compiler *c) {
589+ const NodeTable *nt = c->nt;
590+ if (!g_ie_node_class) return 0;
591+ int changed = 0;
592+ for (int id = 0; id < nt->count; id++) {
593+ int cls = g_ie_node_class[id];
594+ if (cls < 0) continue;
595+ const char *ty = nt_type(nt, id);
596+ if (!ty) continue;
597+ if (strcmp(ty, "InstanceVariableWriteNode") &&
598+ strcmp(ty, "InstanceVariableOperatorWriteNode") &&
599+ strcmp(ty, "InstanceVariableOrWriteNode") &&
600+ strcmp(ty, "InstanceVariableAndWriteNode")) continue;
601+ const char *nm = nt_str(nt, id, "name");
602+ if (!nm) continue;
603+ ClassInfo *ci = &c->classes[cls];
604+ int iv = comp_ivar_index(ci, nm);
605+ if (iv < 0) { iv = comp_ivar_intern(ci, nm); changed = 1; }
606+ /* Type the slot from the assignment value. The general ivar-write inference
607+ keys on the write's enclosing scope class_id, which for a lifted iexec
608+ block is the toplevel/method, not the receiver -- so it never types this
609+ slot. An ivar written only inside iexec blocks has no competing writer, so
610+ widening it here from the value type is safe. */
611+ int v = nt_ref(nt, id, "value");
612+ if (v >= 0) {
613+ TyKind vt = infer_type(c, v);
614+ if (vt != TY_UNKNOWN && vt != TY_NIL) {
615+ TyKind cur = ci->ivar_types[iv];
616+ TyKind nu = (cur == TY_UNKNOWN) ? vt : ty_unify(cur, vt);
617+ if (nu != cur) { ci->ivar_types[iv] = nu; changed = 1; }
618+ }
619+ }
620+ }
621+ return changed;
622+}
623+
581624 /* ---- Block/lambda parameter alpha-renaming ----------------------------
582625 * Block and lambda parameters are interned into the *enclosing* scope, so a
583626 * parameter sharing a name with an enclosing local collapses onto a single
@@ -1390,6 +1433,7 @@
13901433 int ch = 0;
13911434 sp_narrow_memo_bump(); /* invalidate per-iteration narrow-helper memo */
13921435 build_ie_map(c); /* refresh instance_exec receiver-class map each pass */
1436+ ch |= register_ie_block_ivars(c); /* slot ivars first assigned in iexec blocks */
13931437 ch |= infer_write_types(c);
13941438 ch |= infer_param_types(c);
13951439 ch |= infer_param_hash_value(c);