Commit d882e601

d882e601090eMerged

Yusuke Endoh <[email protected]> · 6 months ago · 2 files · +1 added · ~1 modified

Description

fix(codegen): scan_ivars routes writes to parent's slot when inherited

When a child class first writes to an ivar that already lives on a parent, scan_ivars was registering the ivar a second time on the child. update_ivar_type then recursed up to the parent and widened — e.g. heterogeneous int+string in the parent → poly — but the child's own-table entry kept the new write's narrower type. Two tables disagreed about the slot, so downstream type lookups picked one or the other depending on path: cls_ivar_type(Child) returned int while the C struct (emit_class_fields skips own copies that are also in the parent chain) embedded the parent's poly field. The Child-method emit then read/wrote mrb_int shape against an sp_RbVal field, failing the C compile.

Add ivar_exists_in_ancestor and use it to detect "slot lives on an ancestor". When that fires, route the write through update_ivar_type (which already recurses up) instead of add_ivar. The child's table doesn't grow a duplicate entry; the parent's table is the single source of truth.

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

Details

Commit
d882e601090e
Parent
a8c8ac8156dc

spinel_codegen.rbmodifiedView file

@@ -6392,6 +6392,19 @@
63926392 0
63936393 end
63946394
6395+ def ivar_exists_in_ancestor(ci, iname)
6396+ if @cls_parents[ci] != ""
6397+ pi = find_class_idx(@cls_parents[ci])
6398+ if pi >= 0
6399+ if ivar_exists(pi, iname) == 1
6400+ return 1
6401+ end
6402+ return ivar_exists_in_ancestor(pi, iname)
6403+ end
6404+ end
6405+ 0
6406+ end
6407+
63956408 def add_ivar(ci, iname, itype, definite = 0)
63966409 if @cls_ivar_names[ci] != ""
63976410 @cls_ivar_names[ci] = @cls_ivar_names[ci] + ";" + iname
@@ -6477,7 +6490,19 @@
64776490 if @nd_type[nid] == "InstanceVariableWriteNode"
64786491 iname = @nd_name[nid]
64796492 expr_first = @nd_expression[nid]
6480- if ivar_exists(ci, iname) == 0
6493+ if ivar_exists(ci, iname) == 0 && ivar_exists_in_ancestor(ci, iname) == 1
6494+ # Slot is on a parent class — route the write through
6495+ # update_ivar_type so the parent's type widens consistently.
6496+ # Without this, the child re-adds the ivar to its own table
6497+ # with the new write's type, while the parent widens to poly
6498+ # via update_ivar_type's recurse, leaving the two tables
6499+ # disagreeing — and downstream cls_ivar_type lookups on the
6500+ # child see only its (narrower) entry, missing the widening.
6501+ vtype = infer_ivar_init_type(expr_first)
6502+ if vtype != "int" && vtype != "nil"
6503+ update_ivar_type(ci, iname, vtype)
6504+ end
6505+ elsif ivar_exists(ci, iname) == 0
64816506 vtype = infer_ivar_init_type(expr_first)
64826507 add_ivar(ci, iname, vtype, is_definite_ivar_init(expr_first))
64836508 else