Commit cca27aa7
cca27aa74597Merged
Yusuke Endoh <[email protected]> · 6 months ago · 2 files · +1 added · ~1 modified
Description
feat(codegen): widen heterogeneous int/nil + obj scalar slots to poly
When an ivar's first observed write is int or nil and a later
write assigns an obj_<X> value, update_ivar_type previously
overwrote the recorded type with the obj — silently casting the
prior int payload to a struct pointer and miscompiling any further
dispatch through the slot.
Widen to poly instead so the slot holds an sp_RbVal that can
carry either an int or a class instance. The poly-aware emit paths
(IVW/IVR/op-assign/binop/&&/attr_writer/etc.) take it from there
and dispatch per cls_id at the call site.
Pairs with the supporting fixes that make the cascade compile — each handles a specific emit / inference site that surfaces once a slot can legitimately be poly:
- AndNode wraps poly with sp_poly_truthy
- name-based
sampleshortcut defers to user-class lookup - poly-dispatch arms truncate over-arity callers
- attr-writer boxes rhs for poly slot
- cls_ivar_type defers to parent
- scan_ivars routes inherited-slot writes through update
Optcarrot end-to-end: checksum 59662, ~980 fps.
Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]
Details
- Commit
- cca27aa74597
- Parent
- 84d241a676e0
spinel_codegen.rbmodifiedView file
if names[k] == iname if k < types.length old = types[k] if old == "int" || old == "nil" # Heterogeneous int/nil + obj → poly when the prior write # was a *definite* int/nil literal. The previous "int wins" # / "nil wins" overwrite silently cast the int payload to a # struct pointer, miscompiling `@x = 10; @x = Box.new` and # any subsequent obj method dispatch. Widen to poly so the # slot can carry either case at runtime; the dispatch path # then decides per cls_id at the call site. # # The definiteness gate avoids false widening when "int" # was just `infer_ivar_init_type`'s placeholder fallback for # a CallNode rhs that's later refined to an obj type by # the writer-scan / inference passes (e.g. # `@m = method(:foo)` initially scans as int and a # refinement promotes it to `obj_Method` — no heterogeneity # to widen for). if (old == "int" || old == "nil") && is_obj_type(new_type) == 1 && cls_ivar_definite_flag(ci, iname) == 1 types[k] = "poly" @needs_rb_value = 1 @cls_ivar_types[ci] = types.join(";") elsif old == "int" || old == "nil" types[k] = new_type @cls_ivar_types[ci] = types.join(";") elsif old != new_type && old != "poly"test/widen_int_obj_to_poly.rbaddedView file
Loading diff…