Commit cad6f26b

cad6f26b3bf1Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 1 file · ~1 modified

Description

fix(codegen): IntArray literal unboxes poly element from sibling LV in promote

[x - y, i] where x, y are block-param destructured from a poly_array source (e.g. .each_cons(2).with_index(N).map { |(x, y), i| ... }) previously emitted sp_IntArray_push(_t9, sp_poly_sub(lv_x, lv_y)) -- sp_RbVal pushed into a mrb_int slot.

Root cause: annotate_all_node_types' walk_and_cache declares only named block params (RequiredParameterNode), not the inner targets of a MultiTargetNode destructure. The cached type for x - y falls back to "int" at analyze time. At codegen emit, find_var_type sees x as poly (the destructure assigns sp_PolyArray_get into a sp_RbVal slot), so the - arm picks sp_poly_sub and produces sp_RbVal -- but the array literal builder still trusts the cached "int" classification and lowers to IntArray.

Fix: in compile_array_literal_from_ids' IntArray push site, when the element is an arith CallNode (+ - * / % **) whose LV receiver's current scope type is poly, treat the element as poly and emit .v.i unbox before the push. Matches the existing unbox branch that handles already-poly-typed elements; just refreshes a stale "int" classification when the receiver-scope evidence contradicts.

Verified: each_cons_with_index_map_no_double_gets passes in promote (was ERR), default 649/0/0 unchanged, optcarrot checksum 59662 unchanged.

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

Details

Commit
cad6f26b3bf1
Parent
20cfb9ed546d

spinel_codegen.rbmodifiedView file

@@ -26791,6 +26791,25 @@
2679126791 end
2679226792 ev = compile_expr(eid)
2679326793 et = infer_type(eid)
26794+ # Block-param destructure (`|(x, y), i|` from a poly_array source)
26795+ # leaves x, y uncached by annotate_all_node_types — the cached
26796+ # type for `x - y` falls back to "int" while the actual emit
26797+ # picks `sp_poly_sub` (sp_RbVal) because find_var_type at codegen
26798+ # time sees x as poly. Refresh the cached classification by
26799+ # checking the receiver's CURRENT scope type. Issue surfaced via
26800+ # `each_cons_with_index_map_no_double_gets` under promote.
26801+ if et == "int" && @nd_type[eid] == "CallNode"
26802+ e_mn = @nd_name[eid]
26803+ if e_mn == "+" || e_mn == "-" || e_mn == "*" || e_mn == "/" || e_mn == "%" || e_mn == "**"
26804+ e_rcv = @nd_receiver[eid]
26805+ if e_rcv >= 0 && @nd_type[e_rcv] == "LocalVariableReadNode"
26806+ e_rcv_t = find_var_type(@nd_name[e_rcv])
26807+ if e_rcv_t == "poly"
26808+ et = "poly"
26809+ end
26810+ end
26811+ end
26812+ end
2679426813 # Unbox poly values when pushing into a (concrete) IntArray.
2679526814 # An ArrayNode literal whose elements are poly (e.g.
2679626815 # `[addr]` where addr's recorded type is sp_RbVal) needs the