Commit 40c6cbf3

40c6cbf3d163Merged

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

Description

fix(codegen): chained ivar write widens every participant, not just head

Issue #234 added chain-drill widening for @a = @b = expr so the chain head sees the bottom rhs type, but the tail (and any intermediate participants in longer chains) relied on scan_ivars's dual-definite-literal gate to widen. That gate fires for literal RHS like 42 or nil but not for CallNode/expression RHS, leaving the tail at its pre-existing concrete type.

Reproduction:

class C
  def initialize
    @a = "hello"
    @b = "world"
  end
  def make_int; 42; end
  def reset; @a = @b = make_int; end
end

Pre-fix: @a widens to poly via the chain-head drill, @b keeps its string slot (CallNode RHS bypasses scan_ivars's literal gate). compile_chained_ivar_writes emits

mrb_int _t1 = sp_C_make_int(self);
self->iv_a = sp_box_int(_t1);
self->iv_b = _t1;       /* iv_b is const char *! */

and cc rejects the tail store with assignment to 'const char *' from 'mrb_int'. Same C error shape #234 was fixing for the head.

Fix: collect every chain participant's name during the drill and update each one's slot type with the bottom rhs. The drill loop already walks every IVW in the chain — just push each name onto a list and call update_ivar_type per name once bottom reaches a non-IVW. The recursion that visits each IVW separately (via push_child_ids in scan_writer_calls) still runs and is idempotent; widening is monotonic so a redundant update is a no-op.

Test: test/issue235_chain_tail_widening.rb covers the call-RHS shape (@a = @b = make_int) and the 3-chain (@a = @b = @c = c.length). Both must compile and produce the correct values.

289 pass, 0 fail. Bootstrap clean (gen2 == gen3).

Closes #235 Refs #234

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

Details

Commit
40c6cbf3d163
Parent
3b24523ba11b

spinel_codegen.rbmodifiedView file

@@ -8768,16 +8768,22 @@
87688768 if @current_class_idx >= 0
87698769 iname = @nd_name[nid]
87708770 expr_id = @nd_expression[nid]
8771- # Drill through chained `@a = @b = ... = expr` so the chain head
8772- # sees the bottom rhs type — infer_type returns the default
8773- # "int" for an InstanceVariableWriteNode expr, which would
8774- # leave @a's slot un-widened against a real concrete bottom
8775- # type and force compile_stmt to emit a type-mismatched store.
8771+ # Drill through chained `@a = @b = ... = expr` so every chain
8772+ # participant sees the bottom rhs type — infer_type returns
8773+ # the default "int" for an InstanceVariableWriteNode expr,
8774+ # which would leave participants un-widened against a real
8775+ # concrete bottom type and force compile_stmt to emit
8776+ # type-mismatched stores. Issue #235: collecting *every*
8777+ # participant (not just the head) is required because a
8778+ # CallNode rhs (`@a = @b = make_int`) bypasses scan_ivars's
8779+ # dual-definite-literal widening and tail slots stay at
8780+ # their pre-existing concrete type.
8781+ chain_inames = "".split(",")
8782+ chain_inames.push(iname)
87768783 bottom = expr_id
8777- is_chain_head = 0
87788784 while bottom >= 0 && @nd_type[bottom] == "InstanceVariableWriteNode"
8785+ chain_inames.push(@nd_name[bottom])
87798786 bottom = @nd_expression[bottom]
8780- is_chain_head = 1
87818787 end
87828788 # Empty `{}` / `[]` literal: don't reset the ivar's tracked
87838789 # type to the default (`str_int_hash` / `int_array`), since a
@@ -8786,12 +8792,16 @@
87868792 # widen the promoted type to poly on the next iteration.
87878793 if is_empty_hash_literal(bottom) == 0 && is_empty_array_literal(bottom) == 0
87888794 at = infer_type(bottom)
8789- # Chain heads must widen even on int/nil rhs — the head's
8795+ # Chain participants must widen even on int/nil rhs — the
87908796 # current slot type may be a concrete pointer type that
87918797 # update_ivar_type promotes to poly (or the nullable
87928798 # variant) once it sees the bottom rhs.
8793- if is_chain_head == 1
8794- update_ivar_type(@current_class_idx, iname, at)
8799+ if chain_inames.length > 1
8800+ ci_idx = 0
8801+ while ci_idx < chain_inames.length
8802+ update_ivar_type(@current_class_idx, chain_inames[ci_idx], at)
8803+ ci_idx = ci_idx + 1
8804+ end
87958805 elsif at != "int" && at != "nil"
87968806 update_ivar_type(@current_class_idx, iname, at)
87978807 end