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:
- Iter 1 —
Base#reset's@a = @b = nilchain head bypass callsupdate_ivar_type(Base, @a, "nil"), overwriting the int-default. Subclass@a = "hello"then cascades up via parent walk and pinsBase.@aat "string". - Iter 2 — chain bypass fires again with "nil" against the now-
"string" slot.
update_ivar_typewidens to "string?" (nullable). Subclass cascade then sees "string?" vs incoming "string" — neither side is recognized as compatible, falls through to "poly". - Poly cascades back to subclass slot. The subclass's
@a = "hello"literal store then fails the C compile against the now-sp_RbValslot.
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
# widen the promoted type to poly on the next iteration. if is_empty_hash_literal(bottom) == 0 && is_empty_array_literal(bottom) == 0 at = infer_type(bottom) # Chain participants must widen even on int/nil rhs — the # current slot type may be a concrete pointer type that # update_ivar_type promotes to poly (or the nullable # variant) once it sees the bottom rhs. if chain_inames.length > 1 # Chain participants bypass the `int` guard so that # `@string_slot = @int_slot = expr_returning_int` widens # the head to poly instead of leaving it stuck at # `string`. The `nil` guard stays in place even for # chains: `@a = @b = ... = nil` is handled at emit time # by `compile_chained_ivar_writes`'s per-slot recurse # path (NilNode lowers to a literal `0`, a null pointer # constant valid for any slot type), and forcing nil # into the slot type interacts badly with parent-cascade # update_ivar_type — a subclass write that pins the same # slot to a concrete obj type ping-pongs between obj_X # and obj_X? across iter rounds and lands on poly, # breaking the typed-pointer store at emit time. if chain_inames.length > 1 && at == "int" ci_idx = 0 while ci_idx < chain_inames.length update_ivar_type(@current_class_idx, chain_inames[ci_idx], at)test/chained_ivar_write_subclass.rbaddedView file
Loading diff…
test/chained_ivar_write_subclass.rb.expectedaddedView file
Loading diff…