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
if @current_class_idx >= 0 iname = @nd_name[nid] expr_id = @nd_expression[nid] # Drill through chained `@a = @b = ... = expr` so the chain head # sees the bottom rhs type — infer_type returns the default # "int" for an InstanceVariableWriteNode expr, which would # leave @a's slot un-widened against a real concrete bottom # type and force compile_stmt to emit a type-mismatched store. # Drill through chained `@a = @b = ... = expr` so every chain # participant sees the bottom rhs type — infer_type returns # the default "int" for an InstanceVariableWriteNode expr, # which would leave participants un-widened against a real # concrete bottom type and force compile_stmt to emit # type-mismatched stores. Issue #235: collecting *every* # participant (not just the head) is required because a # CallNode rhs (`@a = @b = make_int`) bypasses scan_ivars's # dual-definite-literal widening and tail slots stay at # their pre-existing concrete type. chain_inames = "".split(",") chain_inames.push(iname) bottom = expr_id is_chain_head = 0 while bottom >= 0 && @nd_type[bottom] == "InstanceVariableWriteNode" chain_inames.push(@nd_name[bottom]) bottom = @nd_expression[bottom] is_chain_head = 1 end # Empty `{}` / `[]` literal: don't reset the ivar's tracked # type to the default (`str_int_hash` / `int_array`), since a # 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 heads must widen even on int/nil rhs — the head's # 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 is_chain_head == 1 update_ivar_type(@current_class_idx, iname, at) if chain_inames.length > 1 ci_idx = 0 while ci_idx < chain_inames.length update_ivar_type(@current_class_idx, chain_inames[ci_idx], at) ci_idx = ci_idx + 1 end elsif at != "int" && at != "nil" update_ivar_type(@current_class_idx, iname, at) endtest/issue235_chain_tail_widening.rbaddedView file
Loading diff…
test/issue235_chain_tail_widening.rb.expectedaddedView file
Loading diff…