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 ... end at 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

@@ -11218,18 +11218,32 @@
1121811218 if t == "LocalVariableReadNode"
1121911219 vn_lvr = @nd_name[nid]
1122011220 base_lvr = fiber_var_ref(vn_lvr)
11221- # int-or-nil nil-guard narrow (issue #550): the local is
11222- # declared poly (e.g. h = s.index(...)) but a preceding
11223- # `return X if h.nil?` narrowed it to int. The base C ref
11224- # is the sp_RbVal slot; unbox via .v.i so arithmetic / int
11225- # consumers see mrb_int instead of the struct. Scoped to
11226- # the obvious poly->int case for now; obj_<C> narrows over
11227- # poly locals continue through the dispatch-level paths
11228- # that already handle them (#493).
11221+ # Poly local narrowed to a primitive via a preceding guard
11222+ # (`return X if h.nil?` for int-or-nil — #550; `if x.is_a?(C)`
11223+ # then-arm body — #612). The base C ref is the sp_RbVal slot;
11224+ # unbox via the matching union field so a typed callee
11225+ # (sp_re_escape's `const char *`, arithmetic operators on
11226+ # int, etc.) reads the unboxed value directly. obj_<C>
11227+ # narrows over poly locals continue through the dispatch-
11228+ # level paths that already handle them (#493).
1122911229 narrow_lvr = find_var_type(vn_lvr)
1123011230 declared_lvr = find_var_declared_type(vn_lvr)
11231- if declared_lvr == "poly" && narrow_lvr == "int"
11232- return "(" + base_lvr + ").v.i"
11231+ if declared_lvr == "poly"
11232+ if narrow_lvr == "int"
11233+ return "(" + base_lvr + ").v.i"
11234+ end
11235+ if narrow_lvr == "string" || narrow_lvr == "mutable_str"
11236+ return "(" + base_lvr + ").v.s"
11237+ end
11238+ if narrow_lvr == "float"
11239+ return "(" + base_lvr + ").v.f"
11240+ end
11241+ if narrow_lvr == "bool"
11242+ return "(" + base_lvr + ").v.b"
11243+ end
11244+ if narrow_lvr == "symbol"
11245+ return "(sp_sym)(" + base_lvr + ").v.i"
11246+ end
1123311247 end
1123411248 return base_lvr
1123511249 end
@@ -34966,6 +34980,20 @@
3496634980 end
3496734981 emit(" if (" + cond + ") {")
3496834982 @indent = @indent + 1
34983+ # Mirror compile_if_stmt's is_a? narrow push so the then-arm of
34984+ # a tail-position `if x.is_a?(C) ... end` sees `x` as the
34985+ # narrowed type. Without this, a poly-typed `x` flowing into a
34986+ # typed callee (sp_re_escape, sp_re_match_p's string arg,
34987+ # `%s` interpolation, ...) inside the body emits the raw
34988+ # sp_RbVal and trips a C compile -- the canonical #612 shape.
34989+ # The narrow is popped after the body so the else / fall-through
34990+ # emit sees the un-narrowed type.
34991+ isa_info_cr = is_unless == 0 ? parse_is_a_predicate(@nd_predicate[nid]) : ["", ""]
34992+ isa_pushed_cr = 0
34993+ if isa_info_cr[0] != ""
34994+ push_type_narrow(isa_info_cr[0], isa_info_cr[1])
34995+ isa_pushed_cr = 1
34996+ end
3496934997 body = @nd_body[nid]
3497034998 if body >= 0
3497134999 compile_body_return(body, rt)
@@ -34974,6 +35002,9 @@
3497435002 emit(" return " + c_return_default(rt) + ";")
3497535003 end
3497635004 end
35005+ if isa_pushed_cr == 1
35006+ pop_type_narrow
35007+ end
3497735008 @indent = @indent - 1
3497835009 if is_unless == 1
3497935010 sub = @nd_else_clause[nid]