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
def cls_ivar_type(ci, iname) names = @cls_ivar_names[ci].split(";") types = @cls_ivar_types[ci].split(";") own_t = "" j = 0 while j < names.length if names[j] == iname if j < types.length return types[j] own_t = types[j] else own_t = "int" end return "int" break end j = j + 1 end parent_t = "" if @cls_parents[ci] != "" pi = find_class_idx(@cls_parents[ci]) if pi >= 0 return cls_ivar_type(pi, iname) if pi >= 0 && ivar_in_chain(pi, iname) == 1 parent_t = cls_ivar_type(pi, iname) end end # The struct embeds parent ivars at the parent's recorded type # (`emit_class_fields` skips own copies that are also in the # parent chain). When the parent has the same ivar, the parent's # type is what the C struct field actually has — so any access # through `self->iv_X` must agree with that, regardless of what # this child's own table happened to record. Defer to the parent # type when both classes know the ivar. if parent_t != "" return parent_t end if own_t != "" return own_t end "int" endtest/cls_ivar_type_parent_defer.rbaddedView file
Loading diff…