Commit be714533

be7145336cfcMerged

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

Description

fix(analyze): no-recv yield method propagates yield arg type to block local

emit_rows { |row| ... } against def emit_rows; ...; yield row; end where the yield arg is a typed Hash (or any non-int type) declared the block-local as mrb_int at the caller's scope. Spinel's existing block_param_type_at had the right plumbing for obj-receiver yield methods (c.each { |k| ... } walked the method's body for yield arg types) but the no-receiver, top-level shape fell through to the int_array fallback. The typed pointer then got coerced into an int slot via the inlined lv_row = lv_row_y1 assignment, and downstream row["k"] reads dispatched on int (folding to 0).

Two parallel sites in spinel_analyze.rb needed the lookup:

  1. block_param_type_at — the helper used by walk_and_cache's block-scope push. Wires up recv < 0 + find_method_idx + @meth_has_yield + method_yield_arity, mirroring the existing cls_method arm.

  2. scan_locals' block-param branch (where the block params are pushed into the OUTER scope's lnames/ltypes that become @nd_scope_names[@root_id]). Same lookup; if it returns a non-int type, that's what the C declaration uses.

Same shape via a returning-helper (yield make_h("v")) also works once the analyzer's infer_type on the yield arg resolves through make_h's declared return.

Fixes #628

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

Details

Commit
be7145336cfc
Parent
9fdf82e37fe5

spinel_analyze.rbmodifiedView file

@@ -24038,7 +24038,43 @@
2403824038 end
2403924039 end
2404024040 end
24041- if mname == "times" || mname == "upto" || mname == "downto"
24041+ pushed_block_param_628 = 0
24042+ # No-recv call to a user-defined top-level yield method: look up
24043+ # its body's yield arg type at position bk. Mirrors the obj-recv
24044+ # block above. Issue #628.
24045+ if pushed_block_param_628 == 0 && @nd_receiver[nid] < 0
24046+ mi_628 = find_method_idx(mname)
24047+ if mi_628 >= 0 && mi_628 < @meth_has_yield.length && @meth_has_yield[mi_628] == 1
24048+ ybid_628 = @meth_body_ids[mi_628]
24049+ if ybid_628 >= 0
24050+ arity_628 = method_yield_arity(mi_628)
24051+ ytypes_628 = "".split(",")
24052+ ka_628 = 0
24053+ while ka_628 < arity_628
24054+ ytypes_628.push("")
24055+ ka_628 = ka_628 + 1
24056+ end
24057+ push_scope
24058+ yl_names_628 = "".split(",")
24059+ yl_types_628 = "".split(",")
24060+ scan_locals(ybid_628, yl_names_628, yl_types_628, "".split(","))
24061+ yj_628 = 0
24062+ while yj_628 < yl_names_628.length
24063+ declare_var(yl_names_628[yj_628], yl_types_628[yj_628])
24064+ yj_628 = yj_628 + 1
24065+ end
24066+ body_yield_arg_types(ybid_628, ytypes_628)
24067+ pop_scope
24068+ if bk < ytypes_628.length && ytypes_628[bk] != "" && ytypes_628[bk] != "int"
24069+ types.push(ytypes_628[bk])
24070+ pushed_block_param_628 = 1
24071+ end
24072+ end
24073+ end
24074+ end
24075+ if pushed_block_param_628 == 1
24076+ # Already pushed via the no-recv yield branch above.
24077+ elsif mname == "times" || mname == "upto" || mname == "downto"
2404224078 types.push("int")
2404324079 # `<source>.with_index(off).<consumer> { |elem, idx| }`:
2404424080 # block param at bk=1 is the idx counter (int), not another
@@ -26264,6 +26300,80 @@
2626426300 if recv >= 0
2626526301 recv_t = infer_type(recv)
2626626302 end
26303+ # No-recv call to a user-defined top-level method that uses yield:
26304+ # look up the method body's yield arg type at position pi. Without
26305+ # this, `emit_rows { |row| ... }` against `def emit_rows; yield row;
26306+ # end` where `row` is sp_StrStrHash * gets the block-local declared
26307+ # as mrb_int (the int_array fallback), the typed pointer flows
26308+ # into an int slot and downstream `row["k"]` lookups dispatch on
26309+ # int. Mirrors the cls_method arm in scan_locals' each branch.
26310+ # Issue #628.
26311+ if recv < 0
26312+ mi_bp = find_method_idx(mname)
26313+ if mi_bp >= 0 && mi_bp < @meth_has_yield.length && @meth_has_yield[mi_bp] == 1
26314+ ybid_bp = @meth_body_ids[mi_bp]
26315+ if ybid_bp >= 0
26316+ arity_bp = method_yield_arity(mi_bp)
26317+ ytypes_bp = "".split(",")
26318+ ka_bp = 0
26319+ while ka_bp < arity_bp
26320+ ytypes_bp.push("")
26321+ ka_bp = ka_bp + 1
26322+ end
26323+ push_scope
26324+ yl_names_bp = "".split(",")
26325+ yl_types_bp = "".split(",")
26326+ scan_locals(ybid_bp, yl_names_bp, yl_types_bp, "".split(","))
26327+ yj_bp = 0
26328+ while yj_bp < yl_names_bp.length
26329+ declare_var(yl_names_bp[yj_bp], yl_types_bp[yj_bp])
26330+ yj_bp = yj_bp + 1
26331+ end
26332+ body_yield_arg_types(ybid_bp, ytypes_bp)
26333+ pop_scope
26334+ if pi < ytypes_bp.length && ytypes_bp[pi] != "" && ytypes_bp[pi] != "int"
26335+ return ytypes_bp[pi]
26336+ end
26337+ end
26338+ end
26339+ end
26340+ # No-recv call to a user-defined top-level method that uses yield:
26341+ # look up the method body's yield arg type at position pi. Without
26342+ # this, `emit_rows { |row| ... }` against `def emit_rows; yield row;
26343+ # end` where `row` is sp_StrStrHash * gets the block-local declared
26344+ # as mrb_int (the int_array fallback), the typed pointer flows
26345+ # into an int slot and downstream `row["k"]` lookups dispatch on
26346+ # int. Mirrors the cls_method arm a few hundred lines below.
26347+ # Issue #628.
26348+ if recv < 0
26349+ mi_bp = find_method_idx(mname)
26350+ if mi_bp >= 0 && mi_bp < @meth_has_yield.length && @meth_has_yield[mi_bp] == 1
26351+ ybid_bp = @meth_body_ids[mi_bp]
26352+ if ybid_bp >= 0
26353+ arity_bp = method_yield_arity(mi_bp)
26354+ ytypes_bp = "".split(",")
26355+ ka_bp = 0
26356+ while ka_bp < arity_bp
26357+ ytypes_bp.push("")
26358+ ka_bp = ka_bp + 1
26359+ end
26360+ push_scope
26361+ yl_names_bp = "".split(",")
26362+ yl_types_bp = "".split(",")
26363+ scan_locals(ybid_bp, yl_names_bp, yl_types_bp, "".split(","))
26364+ yj_bp = 0
26365+ while yj_bp < yl_names_bp.length
26366+ declare_var(yl_names_bp[yj_bp], yl_types_bp[yj_bp])
26367+ yj_bp = yj_bp + 1
26368+ end
26369+ body_yield_arg_types(ybid_bp, ytypes_bp)
26370+ pop_scope
26371+ if pi < ytypes_bp.length && ytypes_bp[pi] != "" && ytypes_bp[pi] != "int"
26372+ return ytypes_bp[pi]
26373+ end
26374+ end
26375+ end
26376+ end
2626726377 # `<source>.with_index(off).<consumer> { |elem, idx| }` -- the
2626826378 # block sees the source's element shape at pi=0 and the int idx
2626926379 # counter at pi=1. The receiver chain here is `with_index(off)`