Commit 7f22768e

7f22768e2e4fMerged

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

Description

fix(codegen): cls_ivar_type defers to parent's recorded type

When the same ivar is registered on both a child class and a parent class, the C struct embeds the parent's field at the parent's recorded type — emit_class_fields skips own copies that are also in the parent chain. Returning the child's own-table type let codegen sites disagree with the actual struct layout: an ivar the parent had widened to poly while the child's table still held int would emit raw iv += 1 against an sp_RbVal field, or miscompile a read.

Look up own table; if a parent class also has the ivar in its chain, return the parent's type instead. This matches how the C struct is built. Pure ancestor-only lookups (own_t empty) still walk up via the recursive call.

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

Details

Commit
7f22768e2e4f
Parent
a8c8ac8156dc

spinel_codegen.rbmodifiedView file

@@ -1639,22 +1639,39 @@
16391639 def cls_ivar_type(ci, iname)
16401640 names = @cls_ivar_names[ci].split(";")
16411641 types = @cls_ivar_types[ci].split(";")
1642+ own_t = ""
16421643 j = 0
16431644 while j < names.length
16441645 if names[j] == iname
16451646 if j < types.length
1646- return types[j]
1647+ own_t = types[j]
1648+ else
1649+ own_t = "int"
16471650 end
1648- return "int"
1651+ break
16491652 end
16501653 j = j + 1
16511654 end
1655+ parent_t = ""
16521656 if @cls_parents[ci] != ""
16531657 pi = find_class_idx(@cls_parents[ci])
1654- if pi >= 0
1655- return cls_ivar_type(pi, iname)
1658+ if pi >= 0 && ivar_in_chain(pi, iname) == 1
1659+ parent_t = cls_ivar_type(pi, iname)
16561660 end
16571661 end
1662+ # The struct embeds parent ivars at the parent's recorded type
1663+ # (`emit_class_fields` skips own copies that are also in the
1664+ # parent chain). When the parent has the same ivar, the parent's
1665+ # type is what the C struct field actually has — so any access
1666+ # through `self->iv_X` must agree with that, regardless of what
1667+ # this child's own table happened to record. Defer to the parent
1668+ # type when both classes know the ivar.
1669+ if parent_t != ""
1670+ return parent_t
1671+ end
1672+ if own_t != ""
1673+ return own_t
1674+ end
16581675 "int"
16591676 end
16601677