Commit b1e92805

b1e92805f9d2Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 3 files · +2 added · ~1 modified

Description

fix(codegen): is_a? narrow + LV-read unbox in y1-inlined body

#615 / 817149f added the is_a? narrow push to compile_if_stmt and switched compile_stmt's LV-write to find_var_declared_type so the non-inlined sp_Base_check body emitted sp_re_escape((lv_content).v.s) cleanly. But yield-method inlining (block-passing call) runs through compile_stmt_with_block

  • compile_expr_remap rather than the canonical compile_stmt / compile_expr, and neither saw a narrow push or an unbox arm. The y1 specialization landed lv_content_y1 raw where const char * is expected and tripped a C compile.

Two pieces:

  1. compile_stmt_with_block's IfNode mirrors compile_if_stmt's parse_is_a_predicate + push_type_narrow / pop_type_narrow bracketing around the then-arm.

  2. compile_expr_remap's LocalVariableReadNode mirrors compile_expr's unbox arm: when the local's declared C type is poly and the narrow stack pins it to a primitive, return (lv_<renamed>).v.s etc. The narrow stack uses the original AST name (the inliner doesn't rename narrow entries); the declared-type lookup uses the renamed local so it lands on the y1 scope's poly slot.

Test exercises the matcher shape from the issue (poly param + &block + is_a?(String) + Regexp.escape inside the narrow). Pre- fix the y1 specialization tripped 2 errors on sp_re_escape; post-fix the emit is (lv_content_y1).v.s.

Fixes #624

Co-Authored-By: Claude Opus 4.7 [email protected]

Details

Commit
b1e92805f9d2
Parent
afc75997ae0e

spinel_codegen.rbmodifiedView file

@@ -11271,6 +11271,26 @@
1127111271 # level paths that already handle them (#493).
1127211272 narrow_lvr = find_var_type(vn_lvr)
1127311273 declared_lvr = find_var_declared_type(vn_lvr)
11274+ # Inlined yield-method body: declare_var pushes the renamed
11275+ # `<name>_y1` local into scope_names, but the LV-read still
11276+ # carries the original AST name (`content`). compile_if_stmt's
11277+ # is_a? narrow push uses the AST name too, so find_var_type
11278+ # picks up the narrow correctly, but find_var_declared_type
11279+ # falls through to "" because scope_names only has the renamed
11280+ # entry. Walk the inline rename map and retry under the
11281+ # renamed name so the unbox arm fires inside the y1
11282+ # specialization. Issue #624.
11283+ if declared_lvr == "" && @inline_rename_map_from != nil
11284+ ki = 0
11285+ while ki < @inline_rename_map_from.length
11286+ if @inline_rename_map_from[ki] == vn_lvr
11287+ declared_lvr = find_var_declared_type(@inline_rename_map_to[ki])
11288+ ki = @inline_rename_map_from.length
11289+ else
11290+ ki = ki + 1
11291+ end
11292+ end
11293+ end
1127411294 if declared_lvr == "poly"
1127511295 if narrow_lvr == "int"
1127611296 return "(" + base_lvr + ").v.i"
@@ -33929,6 +33949,18 @@
3392933949 cond = compile_expr_remap(@nd_predicate[nid], map_from, map_to)
3393033950 emit(" if (" + cond + ") {")
3393133951 @indent = @indent + 1
33952+ # Mirror compile_if_stmt's is_a? narrow push so the y1-inlined
33953+ # then-arm of `if x.is_a?(C) ... end` sees `x` as the narrowed
33954+ # type. Without this, the LV-read inside the inlined body
33955+ # emits the raw poly local where the typed callee
33956+ # (sp_re_escape, sp_re_match_p, %s interpolation) wants the
33957+ # unboxed primitive. Issue #624.
33958+ isa_info_wb = parse_is_a_predicate(@nd_predicate[nid])
33959+ isa_pushed_wb = 0
33960+ if isa_info_wb[0] != ""
33961+ push_type_narrow(isa_info_wb[0], isa_info_wb[1])
33962+ isa_pushed_wb = 1
33963+ end
3393233964 body = @nd_body[nid]
3393333965 if body >= 0
3393433966 stmts = get_stmts(body)
@@ -33938,6 +33970,9 @@
3393833970 sk = sk + 1
3393933971 end
3394033972 end
33973+ if isa_pushed_wb == 1
33974+ pop_type_narrow
33975+ end
3394133976 @indent = @indent - 1
3394233977 sub = @nd_subsequent[nid]
3394333978 if sub >= 0
@@ -34051,6 +34086,35 @@
3405134086 t = @nd_type[nid]
3405234087 if t == "LocalVariableReadNode"
3405334088 rname = remap_local(@nd_name[nid], map_from, map_to)
34089+ # Mirror compile_expr's LocalVariableReadNode unbox arm so a
34090+ # poly-typed inlined local narrowed via `if x.is_a?(C) ... end`
34091+ # in the source body emits the matching `.v.s` / `.v.i` / ...
34092+ # unbox inside the y1 specialization. Without this, the inlined
34093+ # copy lands `lv_content_y1` raw where `const char *` is expected
34094+ # (sp_re_escape / sp_re_match_p / interpolation) and trips a C
34095+ # compile. Issue #624. The narrow stack uses the original AST
34096+ # name; the declared-type lookup needs the renamed local so it
34097+ # sees the y1 scope's poly slot.
34098+ orig_name = @nd_name[nid]
34099+ narrow_t = find_var_type(orig_name)
34100+ declared_t = find_var_declared_type(rname)
34101+ if declared_t == "poly"
34102+ if narrow_t == "int"
34103+ return "(lv_" + rname + ").v.i"
34104+ end
34105+ if narrow_t == "string" || narrow_t == "mutable_str"
34106+ return "(lv_" + rname + ").v.s"
34107+ end
34108+ if narrow_t == "float"
34109+ return "(lv_" + rname + ").v.f"
34110+ end
34111+ if narrow_t == "bool"
34112+ return "(lv_" + rname + ").v.b"
34113+ end
34114+ if narrow_t == "symbol"
34115+ return "(sp_sym)(lv_" + rname + ").v.i"
34116+ end
34117+ end
3405434118 return "lv_" + rname
3405534119 end
3405634120 if t == "InstanceVariableReadNode"