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 sample shortcut 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

@@ -6430,7 +6430,26 @@
64306430 if names[k] == iname
64316431 if k < types.length
64326432 old = types[k]
6433- if old == "int" || old == "nil"
6433+ # Heterogeneous int/nil + obj → poly when the prior write
6434+ # was a *definite* int/nil literal. The previous "int wins"
6435+ # / "nil wins" overwrite silently cast the int payload to a
6436+ # struct pointer, miscompiling `@x = 10; @x = Box.new` and
6437+ # any subsequent obj method dispatch. Widen to poly so the
6438+ # slot can carry either case at runtime; the dispatch path
6439+ # then decides per cls_id at the call site.
6440+ #
6441+ # The definiteness gate avoids false widening when "int"
6442+ # was just `infer_ivar_init_type`'s placeholder fallback for
6443+ # a CallNode rhs that's later refined to an obj type by
6444+ # the writer-scan / inference passes (e.g.
6445+ # `@m = method(:foo)` initially scans as int and a
6446+ # refinement promotes it to `obj_Method` — no heterogeneity
6447+ # to widen for).
6448+ if (old == "int" || old == "nil") && is_obj_type(new_type) == 1 && cls_ivar_definite_flag(ci, iname) == 1
6449+ types[k] = "poly"
6450+ @needs_rb_value = 1
6451+ @cls_ivar_types[ci] = types.join(";")
6452+ elsif old == "int" || old == "nil"
64346453 types[k] = new_type
64356454 @cls_ivar_types[ci] = types.join(";")
64366455 elsif old != new_type && old != "poly"