Commit 57e6383a

57e6383a0c3cMerged

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

Description

Merge pull request #1431 from ryanseys/rs-c-iexec-dynamic-ivar

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

Details

Commit
57e6383a0c3c
Parent
9053fd9e81a9
Merge parent
aabdaf462947

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);