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
.mapnow 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
end5004+
# Promote-mode bigint block return: there's no bigint_array5005+
# variant. The codegen builds a sp_PtrArray that stores each5006+
# bigint pointer; mirror that here so the LV / function return5007+
# slot is typed ptr_array (poly_array, etc.) instead of the5008+
# default int_array.5009+
if bret == "bigint"5010+
@needs_gc = 15011+
return "poly_array"5012+
end50045013
if is_obj_type(bret) == 150055014
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 last20561+
# expression is a `.map` block. promote_int_to_bigint_globally20562+
# widens param / ivar / scope LV types but not container-shaped20563+
# method returns. A `def f; @items.map { |b| b.v * 2 } end`20564+
# whose @items is an obj_ptr_array used to return int_array20565+
# (block was int); under promote the block now returns bigint20566+
# and the codegen builds a PolyArray. Without this sweep the20567+
# function signature stays sp_IntArray * and the return-site20568+
# type mismatch trips -Wincompatible-pointer-types.20569+
def promote_reinfer_array_returns20570+
if @int_overflow_mode != "promote"20571+
return20572+
end20573+
# Top-level methods.20574+
mi = 020575+
while mi < @meth_return_types.length20576+
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] : -120579+
new_rt = reinfer_method_array_return(bid)20580+
if new_rt != "" && new_rt != cur_rt20581+
@meth_return_types[mi] = new_rt20582+
end20583+
end20584+
mi = mi + 120585+
end20586+
# Per-class instance methods + class methods.20587+
ci = 020588+
while ci < @cls_names.length20589+
promote_reinfer_array_returns_table(ci, @cls_meth_returns, @cls_meth_bodies)20590+
if ci < @cls_cmeth_returns.length20591+
promote_reinfer_array_returns_table(ci, @cls_cmeth_returns, @cls_cmeth_bodies)20592+
end20593+
ci = ci + 120594+
end20595+
end20596+
20597+
def promote_reinfer_array_returns_table(ci, ret_tbl, body_tbl)20598+
if ci >= ret_tbl.length20599+
return20600+
end20601+
rets = ret_tbl[ci].split(";")20602+
bodies = ci < body_tbl.length ? body_tbl[ci].split(";") : "".split(",")20603+
changed = 020604+
mj = 020605+
while mj < rets.length20606+
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 : -120609+
new_rt = reinfer_method_array_return(bid)20610+
if new_rt != "" && new_rt != cur_rt20611+
rets[mj] = new_rt20612+
changed = 120613+
end20614+
end20615+
mj = mj + 120616+
end20617+
if changed == 120618+
ret_tbl[ci] = rets.join(";")20619+
end20620+
end20621+
20622+
# Re-infer the return type of a method whose body is `bid`. Returns20623+
# "" when the body doesn't end with a recognized shape, or when the20624+
# re-inference matches the old type. Currently handles the20625+
# `.map { ... }` tail (the canonical shape that goes20626+
# int_array -> poly_array under promote).20627+
def reinfer_method_array_return(bid)20628+
if bid < 020629+
return ""20630+
end20631+
stmts = get_stmts(bid)20632+
if stmts.length == 020633+
return ""20634+
end20635+
last = stmts.last20636+
if @nd_type[last] != "CallNode"20637+
return ""20638+
end20639+
if @nd_name[last] != "map" || @nd_block[last] < 020640+
return ""20641+
end20642+
infer_type(last)20643+
end20644+
2055120645
def promote_int_to_bigint_globally2055220646
if @int_overflow_mode != "promote"2055320647
return@@ -21632,6 +21726,12 @@
2163221726
# n + huge; end` because the param `n` stays mrb_int and the2163321727
# call site can't widen across the function boundary.2163421728
promote_int_to_bigint_globally21729+
# Re-infer method return types whose body's tail is a `.map`21730+
# block now returning bigint (or some other shape that widens21731+
# past int_array under promote). promote_meth_returns_table only21732+
# handles scalar int->bigint and tuples; an int_array return21733+
# that should become poly_array stays stale otherwise.21734+
promote_reinfer_array_returns2163521735
# Pre-detect bigint variables before feature detection2163621736
pre_detect_bigint2163721737
detect_featuresspinel_codegen.rbmodifiedView file
Loading diff…