Commit a8418d90

a8418d90c089Merged

Ryan Seys <[email protected]> · 6 months ago · 4 files · +2 added · ~2 modified

Description

fix(analyze,codegen): BeginNode infer_type unifies begin and rescue branch types

Method bodies that end in def f; X; rescue; Y; end had the method's inferred return type fall through to the int default when the body was a BeginNode. With the preceding commit on this branch capturing the begin / rescue last-expr into the return slot, the remaining gap was the type-inference side: a string- returning rescue body would fail to compile because the ret_tmp slot was typed mrb_int but the assigned value was const char *.

Three new arms across spinel_analyze.rb (which determines the method's return type read by codegen) and spinel_codegen.rb (used during emission for the same begin/rescue shape):

  1. infer_type BeginNode arm in both files — walks the begin body's last expr and each rescue clause's last expr, calls unify_return_type to pick the common type.

  2. collect_return_types BeginNode arm in spinel_analyze.rb — walks both the begin body and the rescue chain so explicit return X inside either branch contributes to the method's return-type inference, matching the behaviour of IfNode at the same level. Without this, def f; raise; return X; rescue; Y; end only saw Y and silently used int as the BeginNode contribution in infer_type fallback.

  3. infer_type ReturnNode arm in both files — return X in expression position now returns infer_type(arg) rather than falling through to the universal "int" default. Currently masked by unify_return_type's int-as-sentinel demotion when paired with a concrete contribution from collect_return_types, so no observable behaviour change today; added for defensive correctness and to be resilient to future sentinel-handling changes. Bare return contributes "nil"; return a, b materializes as a fixed-arity tuple via tuple_type_from_elems.

Helper: extracted begin_node_arm_bodies(nid) next to unify_return_type in both files. The "walk the begin body + each rescue clause body in chain order, filtering out missing bodies" pattern was duplicated across the three arms above; the helper returns an int_array of body IDs and each arm becomes a tight while bi < bodies.length loop. (Note: the int_array init idiom in this codebase is [], not "".split(",") — the latter infers as str_array per the convention documented next to the @nd_* parallel-array initializers.)

After this:

  • def fetch(s); ...; rescue; "default"; end returns string cleanly. Previously failed C compilation.
  • def parse(s); ...; rescue; ...; end with multi-stmt rescue bodies works for any uniform branch type.
  • Explicit return X from inside begin or rescue contributes correctly to the inferred type.

New test: test/method_level_rescue_return_string.rb covers string-returning rescue bodies, multi-stmt rescue, and explicit return-inside-begin shapes the preceding commit had to defer.

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

Details

Commit
a8418d90c089
Parent
c4d8870b663a

spinel_analyze.rbmodifiedView file

@@ -1981,6 +1981,49 @@
19811981 end
19821982 return t1
19831983 end
1984+ if t == "ReturnNode"
1985+ # `return X` evaluated in expression position — same shape as
1986+ # collect_return_types' ReturnNode arm. Without this case the
1987+ # universal `"int"` fallback fires; in practice that's demoted
1988+ # by unify_return_type's int-as-sentinel logic when paired with
1989+ # a concrete type from collect_return_types, but returning the
1990+ # actual arg type makes infer_type semantically correct and
1991+ # resilient to future sentinel-handling changes. `return a, b`
1992+ # materializes as a fixed-arity tuple; bare `return` contributes
1993+ # "nil" matching CRuby.
1994+ args_id = @nd_arguments[nid]
1995+ if args_id >= 0
1996+ arg_ids = get_args(args_id)
1997+ if arg_ids.length == 1
1998+ return infer_type(arg_ids[0])
1999+ end
2000+ if arg_ids.length > 1
2001+ return tuple_type_from_elems(arg_ids)
2002+ end
2003+ end
2004+ return "nil"
2005+ end
2006+ if t == "BeginNode"
2007+ # Method bodies that end in `def f; X; rescue; Y; end` need the
2008+ # method's inferred return type to unify both branches; without
2009+ # this arm spinel falls back to int (the default), and string-
2010+ # returning rescue bodies fail C compilation when the ret_tmp
2011+ # slot's type doesn't match the value being assigned.
2012+ types_b = "".split(",")
2013+ bodies_b = begin_node_arm_bodies(nid)
2014+ bi = 0
2015+ while bi < bodies_b.length
2016+ bstmts = get_stmts(bodies_b[bi])
2017+ if bstmts.length > 0
2018+ types_b.push(infer_type(bstmts.last))
2019+ end
2020+ bi = bi + 1
2021+ end
2022+ if types_b.length > 0
2023+ return unify_return_type(types_b)
2024+ end
2025+ return "void"
2026+ end
19842027 if t == "LocalVariableReadNode"
19852028 vt = find_var_type(@nd_name[nid])
19862029 if vt != ""
@@ -14503,6 +14546,42 @@
1450314546 end
1450414547 return
1450514548 end
14549+ if @nd_type[nid] == "BeginNode"
14550+ # Walk both the begin body and each rescue clause body so an
14551+ # explicit `return X` inside either branch contributes to the
14552+ # method's return-type inference. Mirrors the IfNode arm above.
14553+ bodies_cr = begin_node_arm_bodies(nid)
14554+ bi = 0
14555+ while bi < bodies_cr.length
14556+ collect_return_types_nid(bodies_cr[bi], types)
14557+ bi = bi + 1
14558+ end
14559+ return
14560+ end
14561+ end
14562+
14563+ # Returns the body-stmt-list IDs for each arm of a BeginNode (the
14564+ # begin body followed by each rescue clause's body in chain order),
14565+ # filtering out missing bodies (negative IDs). Used by both the
14566+ # `infer_type` BeginNode arm (last-stmt type unification) and the
14567+ # `collect_return_types` BeginNode arm (explicit-return walk).
14568+ # `[]` (not `"".split(",")`) is the int_array init idiom in this
14569+ # codebase — see the @nd_* parallel arrays in `initialize`.
14570+ def begin_node_arm_bodies(nid)
14571+ out = []
14572+ bid = @nd_body[nid]
14573+ if bid >= 0
14574+ out.push(bid)
14575+ end
14576+ rc = @nd_rescue_clause[nid]
14577+ while rc >= 0
14578+ rb = @nd_body[rc]
14579+ if rb >= 0
14580+ out.push(rb)
14581+ end
14582+ rc = @nd_subsequent[rc]
14583+ end
14584+ out
1450614585 end
1450714586
1450814587 def unify_return_type(types)