Commit 9a76fc98

9a76fc98a8ddMerged

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

Description

fix(codegen): scan_ivars_multi_target_type handles collect + trusts block return

Follow-up to #242 — Gemini's two medium review concerns.

  1. collect alias. collect is the standard alias for map in Ruby; the multi-write peek-through handled map only, so A, B = arr.collect { block } fell through to the rt-based heuristics and missed the array-of-array element type.

  2. Trust the block return even when it's int. The original guard bret != "int" && bret != "" && bret != "void" deferred to rt (the outer call's inferred type) on int returns. But the comment block right above explains that rt is unreliable for nested-array shapes — Spinel collapses array-of-array to int_array placeholder. Trusting bret directly when it's non-empty / non-void is the safer rule, and for plain int_array cases the rt-fallthrough also returned "int" anyway, so this is monotonic on the working cases and strictly better on the collapsed cases.

295 pass / build clean.

Refs #242

Co-Authored-By: Yusuke Endoh [email protected] Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]

Details

Commit
9a76fc98a8dd
Parent
b82298afb7ef

spinel_codegen.rbmodifiedView file

@@ -6508,8 +6508,12 @@
65086508 # mapped array, so its type is the block's return type. Spinel
65096509 # collapses array-of-array to a placeholder (int_array) at the
65106510 # outer infer_type level, so we have to peek through the call
6511- # node directly to recover the element type.
6512- if @nd_type[val_id] == "CallNode" && @nd_name[val_id] == "map"
6511+ # node directly to recover the element type. `collect` is the
6512+ # standard alias for `map`; treat both. Trust the block return
6513+ # even when it's `int` — `rt` (the outer call's inferred type)
6514+ # is unreliable for nested-array shapes per the comment above,
6515+ # so the block return is more authoritative.
6516+ if @nd_type[val_id] == "CallNode" && (@nd_name[val_id] == "map" || @nd_name[val_id] == "collect")
65136517 blk = @nd_block[val_id]
65146518 if blk >= 0
65156519 bbody = @nd_body[blk]
@@ -6517,7 +6521,7 @@
65176521 bbs = get_stmts(bbody)
65186522 if bbs.length > 0
65196523 bret = infer_type(bbs.last)
6520- if bret != "int" && bret != "" && bret != "void"
6524+ if bret != "" && bret != "void"
65216525 return bret
65226526 end
65236527 end