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_y1raw whereconst char *is expected and tripped a C compile.
Two pieces:
-
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.
-
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.setc. 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
# level paths that already handle them (#493). narrow_lvr = find_var_type(vn_lvr) declared_lvr = find_var_declared_type(vn_lvr) # Inlined yield-method body: declare_var pushes the renamed # `<name>_y1` local into scope_names, but the LV-read still # carries the original AST name (`content`). compile_if_stmt's # is_a? narrow push uses the AST name too, so find_var_type # picks up the narrow correctly, but find_var_declared_type # falls through to "" because scope_names only has the renamed # entry. Walk the inline rename map and retry under the # renamed name so the unbox arm fires inside the y1 # specialization. Issue #624. if declared_lvr == "" && @inline_rename_map_from != nil ki = 0 while ki < @inline_rename_map_from.length if @inline_rename_map_from[ki] == vn_lvr declared_lvr = find_var_declared_type(@inline_rename_map_to[ki]) ki = @inline_rename_map_from.length else ki = ki + 1 end end end if declared_lvr == "poly" if narrow_lvr == "int" return "(" + base_lvr + ").v.i" cond = compile_expr_remap(@nd_predicate[nid], map_from, map_to) emit(" if (" + cond + ") {") @indent = @indent + 1 # Mirror compile_if_stmt's is_a? narrow push so the y1-inlined # then-arm of `if x.is_a?(C) ... end` sees `x` as the narrowed # type. Without this, the LV-read inside the inlined body # emits the raw poly local where the typed callee # (sp_re_escape, sp_re_match_p, %s interpolation) wants the # unboxed primitive. Issue #624. isa_info_wb = parse_is_a_predicate(@nd_predicate[nid]) isa_pushed_wb = 0 if isa_info_wb[0] != "" push_type_narrow(isa_info_wb[0], isa_info_wb[1]) isa_pushed_wb = 1 end body = @nd_body[nid] if body >= 0 stmts = get_stmts(body) sk = sk + 1 end end if isa_pushed_wb == 1 pop_type_narrow end @indent = @indent - 1 sub = @nd_subsequent[nid] if sub >= 0 t = @nd_type[nid] if t == "LocalVariableReadNode" rname = remap_local(@nd_name[nid], map_from, map_to) # Mirror compile_expr's LocalVariableReadNode unbox arm so a # poly-typed inlined local narrowed via `if x.is_a?(C) ... end` # in the source body emits the matching `.v.s` / `.v.i` / ... # unbox inside the y1 specialization. Without this, the inlined # copy lands `lv_content_y1` raw where `const char *` is expected # (sp_re_escape / sp_re_match_p / interpolation) and trips a C # compile. Issue #624. The narrow stack uses the original AST # name; the declared-type lookup needs the renamed local so it # sees the y1 scope's poly slot. orig_name = @nd_name[nid] narrow_t = find_var_type(orig_name) declared_t = find_var_declared_type(rname) if declared_t == "poly" if narrow_t == "int" return "(lv_" + rname + ").v.i" end if narrow_t == "string" || narrow_t == "mutable_str" return "(lv_" + rname + ").v.s" end if narrow_t == "float" return "(lv_" + rname + ").v.f" end if narrow_t == "bool" return "(lv_" + rname + ").v.b" end if narrow_t == "symbol" return "(sp_sym)(lv_" + rname + ").v.i" end end return "lv_" + rname end if t == "InstanceVariableReadNode"test/poly_local_is_a_string_narrow_in_y1_inline.rbaddedView file
Loading diff…
test/poly_local_is_a_string_narrow_in_y1_inline.rb.expectedaddedView file
Loading diff…