Commit 3111c2c5

3111c2c54e3bMerged

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

Description

analyze: propagate ivar types to transplanted module copies

When a module method is included into a class, the body AST nodes still belong to the module scope so infer_ivar_types only updated the module's class. The including class's ivar stayed TY_UNKNOWN, which the post-fixpoint backstop widened to TY_POLY — mismatching the module's TY_STRING and causing C compile errors in the generated method body.

Fix: after updating the primary class, iterate scopes sharing the same def_node (transplanted copies) and propagate the same ivar type to the host class.

Fixes module_reopen_after_include_ivar.

Co-Authored-By: Claude Sonnet 4.6 [email protected]

Details

Commit
3111c2c54e3b
Parent
461cfd2f9d59

src/analyze.cmodifiedView file

@@ -3758,6 +3758,22 @@
37583758 }
37593759 TyKind merged = ty_unify(ci->ivar_types[iv], vt);
37603760 if (merged != ci->ivar_types[iv]) { ci->ivar_types[iv] = merged; changed = 1; }
3761+ /* Propagate to transplanted copies (module included into a class).
3762+ Body nodes still point to the module scope, so cls_id2 is the module.
3763+ Any scope sharing the same def_node but with a different class_id is
3764+ a transplanted copy that must see the same ivar type. */
3765+ if (s->class_id >= 0 && s->def_node >= 0) {
3766+ int sdef = s->def_node;
3767+ int orig_cid = s->class_id;
3768+ for (int si = 0; si < c->nscopes; si++) {
3769+ Scope *ts = &c->scopes[si];
3770+ if (ts->def_node != sdef || ts->class_id == orig_cid || ts->class_id < 0) continue;
3771+ ClassInfo *tc = &c->classes[ts->class_id];
3772+ int tiv = comp_ivar_intern(tc, nm);
3773+ TyKind tmerged = ty_unify(tc->ivar_types[tiv], vt);
3774+ if (tmerged != tc->ivar_types[tiv]) { tc->ivar_types[tiv] = tmerged; changed = 1; }
3775+ }
3776+ }
37613777 }
37623778 else if (!strcmp(ty, "CallNode")) {
37633779 /* attr-writer assignment: obj.x = v (CallNode "x=") */