Commit cad6d50d
cad6d50d8a16Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 3 files · +2 added · ~1 modified
Description
fix(codegen): tail-position is_a? narrow + LV-read unbox for poly scalars
Two fix sites for #612. The visible symptom: inside an
if x.is_a?(String) ... end body, a poly-typed x flowing into a
typed callee (Regexp.escape, sp_re_match_p's string arg,
%s interpolation, ...) emitted the raw sp_RbVal and the C
compile failed.
Root cause:
-
Tail-position
if ... endat the end of a method body doesn't go through compile_if_stmt or compile_if_expr — it routes to compile_if_return via compile_body_return's last-stmt switch.compile_if_return(→ compile_cond_return) emitted the cond- body but never pushed an is_a? narrow, so find_var_type inside the body returned the un-narrowed poly type. compile_if_stmt has the same gap for non-tail ifs but the canonical repro happens to land on the tail-position path; the same arm has to live in both eventually, but this commit only adds compile_cond_return since compile_if_stmt isn't hit by the open repros.
-
Even with the narrow push in place, LocalVariableReadNode's codegen only unboxed for the int case (#550's narrow-on-nil path). For the new string / float / bool / symbol narrows, the base ref leaked the raw sp_RbVal. Extended the existing declared-poly+narrow-prim arm to cover the four sibling primitive narrows.
Test: test/poly_local_is_a_string_narrow_in_tail_if.rb exercises
the matcher shape from the issue body (Hash | String poly, narrow
in tail if, 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 "match".
Closes #612. 587 tests pass / bootstrap fixpoint clean.
Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]
Details
- Commit
- cad6d50d8a16
- Parent
- e3e748ce6d76
spinel_codegen.rbmodifiedView file
if t == "LocalVariableReadNode" vn_lvr = @nd_name[nid] base_lvr = fiber_var_ref(vn_lvr) # int-or-nil nil-guard narrow (issue #550): the local is # declared poly (e.g. h = s.index(...)) but a preceding # `return X if h.nil?` narrowed it to int. The base C ref # is the sp_RbVal slot; unbox via .v.i so arithmetic / int # consumers see mrb_int instead of the struct. Scoped to # the obvious poly->int case for now; obj_<C> narrows over # poly locals continue through the dispatch-level paths # that already handle them (#493). # Poly local narrowed to a primitive via a preceding guard # (`return X if h.nil?` for int-or-nil — #550; `if x.is_a?(C)` # then-arm body — #612). The base C ref is the sp_RbVal slot; # unbox via the matching union field so a typed callee # (sp_re_escape's `const char *`, arithmetic operators on # int, etc.) reads the unboxed value directly. obj_<C> # narrows over poly locals continue through the dispatch- # level paths that already handle them (#493). narrow_lvr = find_var_type(vn_lvr) declared_lvr = find_var_declared_type(vn_lvr) if declared_lvr == "poly" && narrow_lvr == "int" return "(" + base_lvr + ").v.i" if declared_lvr == "poly" if narrow_lvr == "int" return "(" + base_lvr + ").v.i" end if narrow_lvr == "string" || narrow_lvr == "mutable_str" return "(" + base_lvr + ").v.s" end if narrow_lvr == "float" return "(" + base_lvr + ").v.f" end if narrow_lvr == "bool" return "(" + base_lvr + ").v.b" end if narrow_lvr == "symbol" return "(sp_sym)(" + base_lvr + ").v.i" end end return base_lvr end end emit(" if (" + cond + ") {") @indent = @indent + 1 # Mirror compile_if_stmt's is_a? narrow push so the then-arm of # a tail-position `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 canonical #612 shape. # The narrow is popped after the body so the else / fall-through # emit sees the un-narrowed type. isa_info_cr = is_unless == 0 ? parse_is_a_predicate(@nd_predicate[nid]) : ["", ""] isa_pushed_cr = 0 if isa_info_cr[0] != "" push_type_narrow(isa_info_cr[0], isa_info_cr[1]) isa_pushed_cr = 1 end body = @nd_body[nid] if body >= 0 compile_body_return(body, rt) emit(" return " + c_return_default(rt) + ";") end end if isa_pushed_cr == 1 pop_type_narrow end @indent = @indent - 1 if is_unless == 1 sub = @nd_else_clause[nid]test/poly_local_is_a_string_narrow_in_tail_if.rbaddedView file
Loading diff…
test/poly_local_is_a_string_narrow_in_tail_if.rb.expectedaddedView file
Loading diff…