Commit 64c18e5c

64c18e5ccfd5Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 3 files · +2 added · ~1 modified

Description

fix(codegen): chain-head widening bypass restricted to int RHS only

Issue raised in PR #238: my fix for #235 widened every chain participant against the bottom RHS, including when the bottom was nil. That interacted badly with the subclass-cascade in update_ivar_type and the iterative inference fixpoint:

  1. Iter 1 — Base#reset's @a = @b = nil chain head bypass calls update_ivar_type(Base, @a, "nil"), overwriting the int-default. Subclass @a = "hello" then cascades up via parent walk and pins Base.@a at "string".
  2. Iter 2 — chain bypass fires again with "nil" against the now- "string" slot. update_ivar_type widens to "string?" (nullable). Subclass cascade then sees "string?" vs incoming "string" — neither side is recognized as compatible, falls through to "poly".
  3. Poly cascades back to subclass slot. The subclass's @a = "hello" literal store then fails the C compile against the now-sp_RbVal slot.

Reproduction (test/chained_ivar_write_subclass.rb):

class Base
  def reset; @a = @b = nil; end
end
class Sub < Base
  def initialize; @a = "hello"; @b = 42; end
  attr_reader :a, :b
end
Sub.new

Pre-fix: error: incompatible types when assigning to type 'sp_RbVal' from type 'char *' at every @a = "hello" store.

Fix: gate the chain-participant widening on at == "int". The existing elsif at != "int" && at != "nil" branch picks up non-int non-nil bottoms (string/CallNode/etc.) for the head naturally; nil bottoms now fall through entirely, since compile_chained_ivar_writes's NilNode special case handles emit-time correctness via per-slot recursion (NilNode lowers to literal 0, a null pointer constant valid for any slot type).

Test fixture from mame's PR #238, verbatim. Existing chain tests (#235, #236, plus chained_ivar_write_split / call_rhs) still pass.

291 pass, bootstrap clean.

Closes #238 Refs #235

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

Details

Commit
64c18e5ccfd5
Parent
77b4451a7942

spinel_codegen.rbmodifiedView file

@@ -8804,11 +8804,20 @@
88048804 # widen the promoted type to poly on the next iteration.
88058805 if is_empty_hash_literal(bottom) == 0 && is_empty_array_literal(bottom) == 0
88068806 at = infer_type(bottom)
8807- # Chain participants must widen even on int/nil rhs — the
8808- # current slot type may be a concrete pointer type that
8809- # update_ivar_type promotes to poly (or the nullable
8810- # variant) once it sees the bottom rhs.
8811- if chain_inames.length > 1
8807+ # Chain participants bypass the `int` guard so that
8808+ # `@string_slot = @int_slot = expr_returning_int` widens
8809+ # the head to poly instead of leaving it stuck at
8810+ # `string`. The `nil` guard stays in place even for
8811+ # chains: `@a = @b = ... = nil` is handled at emit time
8812+ # by `compile_chained_ivar_writes`'s per-slot recurse
8813+ # path (NilNode lowers to a literal `0`, a null pointer
8814+ # constant valid for any slot type), and forcing nil
8815+ # into the slot type interacts badly with parent-cascade
8816+ # update_ivar_type — a subclass write that pins the same
8817+ # slot to a concrete obj type ping-pongs between obj_X
8818+ # and obj_X? across iter rounds and lands on poly,
8819+ # breaking the typed-pointer store at emit time.
8820+ if chain_inames.length > 1 && at == "int"
88128821 ci_idx = 0
88138822 while ci_idx < chain_inames.length
88148823 update_ivar_type(@current_class_idx, chain_inames[ci_idx], at)