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
0 end def ivar_exists_in_ancestor(ci, iname) if @cls_parents[ci] != "" pi = find_class_idx(@cls_parents[ci]) if pi >= 0 if ivar_exists(pi, iname) == 1 return 1 end return ivar_exists_in_ancestor(pi, iname) end end 0 end def add_ivar(ci, iname, itype, definite = 0) if @cls_ivar_names[ci] != "" @cls_ivar_names[ci] = @cls_ivar_names[ci] + ";" + iname if @nd_type[nid] == "InstanceVariableWriteNode" iname = @nd_name[nid] expr_first = @nd_expression[nid] if ivar_exists(ci, iname) == 0 if ivar_exists(ci, iname) == 0 && ivar_exists_in_ancestor(ci, iname) == 1 # Slot is on a parent class — route the write through # update_ivar_type so the parent's type widens consistently. # Without this, the child re-adds the ivar to its own table # with the new write's type, while the parent widens to poly # via update_ivar_type's recurse, leaving the two tables # disagreeing — and downstream cls_ivar_type lookups on the # child see only its (narrower) entry, missing the widening. vtype = infer_ivar_init_type(expr_first) if vtype != "int" && vtype != "nil" update_ivar_type(ci, iname, vtype) end elsif ivar_exists(ci, iname) == 0 vtype = infer_ivar_init_type(expr_first) add_ivar(ci, iname, vtype, is_definite_ivar_init(expr_first)) elsetest/scan_ivars_inherited_slot.rbaddedView file
Loading diff…