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):
-
infer_typeBeginNode arm in both files — walks the begin body's last expr and each rescue clause's last expr, callsunify_return_typeto pick the common type. -
collect_return_typesBeginNode arm in spinel_analyze.rb — walks both the begin body and the rescue chain so explicitreturn Xinside 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; endonly saw Y and silently used int as the BeginNode contribution in infer_type fallback. -
infer_typeReturnNode arm in both files —return Xin expression position now returnsinfer_type(arg)rather than falling through to the universal"int"default. Currently masked byunify_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. Barereturncontributes "nil";return a, bmaterializes as a fixed-arity tuple viatuple_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"; endreturns string cleanly. Previously failed C compilation.def parse(s); ...; rescue; ...; endwith multi-stmt rescue bodies works for any uniform branch type.- Explicit
return Xfrom 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
end return t1 end if t == "ReturnNode" # `return X` evaluated in expression position — same shape as # collect_return_types' ReturnNode arm. Without this case the # universal `"int"` fallback fires; in practice that's demoted # by unify_return_type's int-as-sentinel logic when paired with # a concrete type from collect_return_types, but returning the # actual arg type makes infer_type semantically correct and # resilient to future sentinel-handling changes. `return a, b` # materializes as a fixed-arity tuple; bare `return` contributes # "nil" matching CRuby. args_id = @nd_arguments[nid] if args_id >= 0 arg_ids = get_args(args_id) if arg_ids.length == 1 return infer_type(arg_ids[0]) end if arg_ids.length > 1 return tuple_type_from_elems(arg_ids) end end return "nil" end if t == "BeginNode" # Method bodies that end in `def f; X; rescue; Y; end` need the # method's inferred return type to unify both branches; without # this arm spinel falls back to int (the default), and string- # returning rescue bodies fail C compilation when the ret_tmp # slot's type doesn't match the value being assigned. types_b = "".split(",") bodies_b = begin_node_arm_bodies(nid) bi = 0 while bi < bodies_b.length bstmts = get_stmts(bodies_b[bi]) if bstmts.length > 0 types_b.push(infer_type(bstmts.last)) end bi = bi + 1 end if types_b.length > 0 return unify_return_type(types_b) end return "void" end if t == "LocalVariableReadNode" vt = find_var_type(@nd_name[nid]) if vt != "" end return end if @nd_type[nid] == "BeginNode" # Walk both the begin body and each rescue clause body so an # explicit `return X` inside either branch contributes to the # method's return-type inference. Mirrors the IfNode arm above. bodies_cr = begin_node_arm_bodies(nid) bi = 0 while bi < bodies_cr.length collect_return_types_nid(bodies_cr[bi], types) bi = bi + 1 end return end end # Returns the body-stmt-list IDs for each arm of a BeginNode (the # begin body followed by each rescue clause's body in chain order), # filtering out missing bodies (negative IDs). Used by both the # `infer_type` BeginNode arm (last-stmt type unification) and the # `collect_return_types` BeginNode arm (explicit-return walk). # `[]` (not `"".split(",")`) is the int_array init idiom in this # codebase — see the @nd_* parallel arrays in `initialize`. def begin_node_arm_bodies(nid) out = [] bid = @nd_body[nid] if bid >= 0 out.push(bid) end rc = @nd_rescue_clause[nid] while rc >= 0 rb = @nd_body[rc] if rb >= 0 out.push(rb) end rc = @nd_subsequent[rc] end out end def unify_return_type(types)spinel_codegen.rbmodifiedView file
Loading diff…
test/method_level_rescue_return_string.rbaddedView file
Loading diff…
test/method_level_rescue_return_string.rb.expectedaddedView file
Loading diff…