Commit fb91e06f
fb91e06f7aa9Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 3 files · +2 added · ~1 modified
Description
fix(codegen): is_a? narrow + LV-read unbox in non-tail if bodies
81d6010 added the is_a? narrow push only to compile_cond_return
(tail-position ifs) and left compile_if_stmt's then-arm without
one. So if x.is_a?(String) ... yield if block end -- the body
flows into a non-tail if -- still emitted the raw sp_RbVal for
typed callees like sp_re_escape. The volatile decl noted on the
issue was a red herring; the actual gap was the non-tail-position
codepath. The unbox arm at LocalVariableReadNode already handles
both volatile and non-volatile poly slots once the narrow is in
place.
Two pieces:
-
compile_if_stmt now mirrors compile_cond_return's parse_is_a_predicate / push_type_narrow / pop_type_narrow bracketing around compile_stmts_body for the then-arm.
-
compile_stmt's LocalVariableWriteNode arm now uses find_var_declared_type instead of find_var_type for the slot-type lookup. The narrow stack is read-only -- a rewrite of the narrowed variable inside the if body (
opt = { a: 7 } if opt.is_a?(String)) must pick the emit arm by the slot's actual C storage type, not by the in-body narrow that only governs reads. Without this, the slot-type lookup landed on the narrow's "string" inside the body and skipped the sym_poly_hash literal arm in the existing sym_poly_hash_merge regression test.
Test: poly_local_is_a_string_narrow_in_non_tail_if.rb exercises
the matcher shape from the issue (Hash | String poly param,
narrow in non-tail if with yield if block after, Regexp.escape
in body). Pre-fix the C compile errored on
sp_re_escape(lv_content); post-fix the emit is
sp_re_escape((lv_content).v.s) and the binary outputs "OK".
Fixes #615
Co-Authored-By: Claude Opus 4.7 [email protected]
Details
- Commit
- fb91e06f7aa9
- Parent
- b7131c5b593c
spinel_codegen.rbmodifiedView file
end end vref = fiber_var_ref(lname) vt = find_var_type(lname) # LV-write picks the slot's tracked C storage type — the narrow # stack (find_var_type) is read-only. A re-assignment inside an # `if x.is_a?(String) ... end` body must use the slot's actual # type for the emit decision (poly_box arm, hash-variant arm, # etc.), not the in-body narrow that only governs reads. Without # this, `opt = { a: 7 } if opt.is_a?(String)` would look up # vt=="string" inside the modifier-if body and skip the # sym_poly_hash literal arm. See #615. vt = find_var_declared_type(lname) # Empty array literal: create the correct array type. Returning # early here also preserves the scope's already-promoted type # , #85) — the fall-through path below would clobber end emit(" if (" + cond + ") {") @indent = @indent + 1 # Mirror compile_cond_return's is_a? narrow push so the then-arm of # a non-tail `if x.is_a?(C) ... end` sees `x` as the narrowed type. # Without this, a poly-typed `x` flowing into a typed callee # (sp_re_escape, sp_re_match_p's string arg, `%s` interpolation, # ...) inside the body emits the raw sp_RbVal and trips a C # compile -- the #615 shape (volatile locals were a red herring; # the actual gap was non-tail-position ifs). The narrow is popped # before the else branch and after the body so the else sees the # un-narrowed type. isa_info_is = parse_is_a_predicate(@nd_predicate[nid]) isa_pushed_is = 0 if isa_info_is[0] != "" push_type_narrow(isa_info_is[0], isa_info_is[1]) isa_pushed_is = 1 end compile_stmts_body(@nd_body[nid]) if isa_pushed_is == 1 pop_type_narrow end @indent = @indent - 1 sub = @nd_subsequent[nid] if sub >= 0test/poly_local_is_a_string_narrow_in_non_tail_if.rbaddedView file
Loading diff…
test/poly_local_is_a_string_narrow_in_non_tail_if.rb.expectedaddedView file
Loading diff…