Commit 0bdcf4eb

0bdcf4eb9121Merged

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

Description

fix(analyze,codegen): map block returning bigint widens method ret to poly_array

A method def doubled; @items.map { |b| b.v * 2 } end whose @items is an obj_ptr_array used to return int_array (block ret int). Under promote mode b.v widens to bigint, b.v * 2 returns bigint, and compile_map_expr's ptr_array recv branch fell to the catch-all PtrArray of bigint pointers -- but the function signature stayed sp_IntArray * because promote_meth_returns_table only handles scalar int->bigint.

analyze:

  • infer_call_type for .map now returns "poly_array" when the block's last expr infers as "bigint".
  • new promote_reinfer_array_returns post-pass walks every method (top-level + per-class instance/class) whose declared return is int_array/str_array/float_array/sym_array, re-infers via the map-tail shape, and updates the return slot when the new inference widens it.

codegen:

  • compile_map_expr's ptr_array recv branch now has an explicit bigint block-ret arm that builds an sp_PolyArray and pushes sp_box_int(sp_bigint_to_int(val)) per element. Default-mode behaviour (int / string / float / obj block returns) is untouched.

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

Details

Commit
0bdcf4eb9121
Parent
a6d2bd938ded

spinel_analyze.rbmodifiedView file

@@ -5001,6 +5001,15 @@
50015001 if bret == "int"
50025002 return "int_array"
50035003 end
5004+ # Promote-mode bigint block return: there's no bigint_array
5005+ # variant. The codegen builds a sp_PtrArray that stores each
5006+ # bigint pointer; mirror that here so the LV / function return
5007+ # slot is typed ptr_array (poly_array, etc.) instead of the
5008+ # default int_array.
5009+ if bret == "bigint"
5010+ @needs_gc = 1
5011+ return "poly_array"
5012+ end
50045013 if is_obj_type(bret) == 1
50055014 return bret + "_ptr_array"
50065015 end
@@ -20548,6 +20557,91 @@
2054820557 # explicit bigint local crosses one of those boundaries.
2054920558 #
2055020559 # No-op when mode != promote.
20560+ # Post-promote: re-infer method return types whose body's last
20561+ # expression is a `.map` block. promote_int_to_bigint_globally
20562+ # widens param / ivar / scope LV types but not container-shaped
20563+ # method returns. A `def f; @items.map { |b| b.v * 2 } end`
20564+ # whose @items is an obj_ptr_array used to return int_array
20565+ # (block was int); under promote the block now returns bigint
20566+ # and the codegen builds a PolyArray. Without this sweep the
20567+ # function signature stays sp_IntArray * and the return-site
20568+ # type mismatch trips -Wincompatible-pointer-types.
20569+ def promote_reinfer_array_returns
20570+ if @int_overflow_mode != "promote"
20571+ return
20572+ end
20573+ # Top-level methods.
20574+ mi = 0
20575+ while mi < @meth_return_types.length
20576+ cur_rt = @meth_return_types[mi]
20577+ if cur_rt == "int_array" || cur_rt == "str_array" || cur_rt == "float_array" || cur_rt == "sym_array"
20578+ bid = mi < @meth_body_ids.length ? @meth_body_ids[mi] : -1
20579+ new_rt = reinfer_method_array_return(bid)
20580+ if new_rt != "" && new_rt != cur_rt
20581+ @meth_return_types[mi] = new_rt
20582+ end
20583+ end
20584+ mi = mi + 1
20585+ end
20586+ # Per-class instance methods + class methods.
20587+ ci = 0
20588+ while ci < @cls_names.length
20589+ promote_reinfer_array_returns_table(ci, @cls_meth_returns, @cls_meth_bodies)
20590+ if ci < @cls_cmeth_returns.length
20591+ promote_reinfer_array_returns_table(ci, @cls_cmeth_returns, @cls_cmeth_bodies)
20592+ end
20593+ ci = ci + 1
20594+ end
20595+ end
20596+
20597+ def promote_reinfer_array_returns_table(ci, ret_tbl, body_tbl)
20598+ if ci >= ret_tbl.length
20599+ return
20600+ end
20601+ rets = ret_tbl[ci].split(";")
20602+ bodies = ci < body_tbl.length ? body_tbl[ci].split(";") : "".split(",")
20603+ changed = 0
20604+ mj = 0
20605+ while mj < rets.length
20606+ cur_rt = rets[mj]
20607+ if cur_rt == "int_array" || cur_rt == "str_array" || cur_rt == "float_array" || cur_rt == "sym_array"
20608+ bid = mj < bodies.length ? bodies[mj].to_i : -1
20609+ new_rt = reinfer_method_array_return(bid)
20610+ if new_rt != "" && new_rt != cur_rt
20611+ rets[mj] = new_rt
20612+ changed = 1
20613+ end
20614+ end
20615+ mj = mj + 1
20616+ end
20617+ if changed == 1
20618+ ret_tbl[ci] = rets.join(";")
20619+ end
20620+ end
20621+
20622+ # Re-infer the return type of a method whose body is `bid`. Returns
20623+ # "" when the body doesn't end with a recognized shape, or when the
20624+ # re-inference matches the old type. Currently handles the
20625+ # `.map { ... }` tail (the canonical shape that goes
20626+ # int_array -> poly_array under promote).
20627+ def reinfer_method_array_return(bid)
20628+ if bid < 0
20629+ return ""
20630+ end
20631+ stmts = get_stmts(bid)
20632+ if stmts.length == 0
20633+ return ""
20634+ end
20635+ last = stmts.last
20636+ if @nd_type[last] != "CallNode"
20637+ return ""
20638+ end
20639+ if @nd_name[last] != "map" || @nd_block[last] < 0
20640+ return ""
20641+ end
20642+ infer_type(last)
20643+ end
20644+
2055120645 def promote_int_to_bigint_globally
2055220646 if @int_overflow_mode != "promote"
2055320647 return
@@ -21632,6 +21726,12 @@
2163221726 # n + huge; end` because the param `n` stays mrb_int and the
2163321727 # call site can't widen across the function boundary.
2163421728 promote_int_to_bigint_globally
21729+ # Re-infer method return types whose body's tail is a `.map`
21730+ # block now returning bigint (or some other shape that widens
21731+ # past int_array under promote). promote_meth_returns_table only
21732+ # handles scalar int->bigint and tuples; an int_array return
21733+ # that should become poly_array stays stale otherwise.
21734+ promote_reinfer_array_returns
2163521735 # Pre-detect bigint variables before feature detection
2163621736 pre_detect_bigint
2163721737 detect_features