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:

  1. 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.

  2. 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

@@ -24486,7 +24486,15 @@
2448624486 end
2448724487 end
2448824488 vref = fiber_var_ref(lname)
24489- vt = find_var_type(lname)
24489+ # LV-write picks the slot's tracked C storage type — the narrow
24490+ # stack (find_var_type) is read-only. A re-assignment inside an
24491+ # `if x.is_a?(String) ... end` body must use the slot's actual
24492+ # type for the emit decision (poly_box arm, hash-variant arm,
24493+ # etc.), not the in-body narrow that only governs reads. Without
24494+ # this, `opt = { a: 7 } if opt.is_a?(String)` would look up
24495+ # vt=="string" inside the modifier-if body and skip the
24496+ # sym_poly_hash literal arm. See #615.
24497+ vt = find_var_declared_type(lname)
2449024498 # Empty array literal: create the correct array type. Returning
2449124499 # early here also preserves the scope's already-promoted type
2449224500 # , #85) — the fall-through path below would clobber
@@ -25736,7 +25744,25 @@
2573625744 end
2573725745 emit(" if (" + cond + ") {")
2573825746 @indent = @indent + 1
25747+ # Mirror compile_cond_return's is_a? narrow push so the then-arm of
25748+ # a non-tail `if x.is_a?(C) ... end` sees `x` as the narrowed type.
25749+ # Without this, a poly-typed `x` flowing into a typed callee
25750+ # (sp_re_escape, sp_re_match_p's string arg, `%s` interpolation,
25751+ # ...) inside the body emits the raw sp_RbVal and trips a C
25752+ # compile -- the #615 shape (volatile locals were a red herring;
25753+ # the actual gap was non-tail-position ifs). The narrow is popped
25754+ # before the else branch and after the body so the else sees the
25755+ # un-narrowed type.
25756+ isa_info_is = parse_is_a_predicate(@nd_predicate[nid])
25757+ isa_pushed_is = 0
25758+ if isa_info_is[0] != ""
25759+ push_type_narrow(isa_info_is[0], isa_info_is[1])
25760+ isa_pushed_is = 1
25761+ end
2573925762 compile_stmts_body(@nd_body[nid])
25763+ if isa_pushed_is == 1
25764+ pop_type_narrow
25765+ end
2574025766 @indent = @indent - 1
2574125767 sub = @nd_subsequent[nid]
2574225768 if sub >= 0