Commit ab2c59d0
ab2c59d09229Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 6 files · +4 added · ~2 modified
Description
Merge pull request #481 from ryanseys/rs/begin-node-infer-type
fix(analyze,codegen): method-level rescue propagates values and unifies branch types
Details
- Commit
- ab2c59d09229
- Parent
- f3326270fe33
- Merge parent
- a8418d90c089
spinel_analyze.rbmodifiedView file
@@ -1863,6 +1863,49 @@
18631863
end18641864
return t118651865
end1866+
if t == "ReturnNode"1867+
# `return X` evaluated in expression position — same shape as1868+
# collect_return_types' ReturnNode arm. Without this case the1869+
# universal `"int"` fallback fires; in practice that's demoted1870+
# by unify_return_type's int-as-sentinel logic when paired with1871+
# a concrete type from collect_return_types, but returning the1872+
# actual arg type makes infer_type semantically correct and1873+
# resilient to future sentinel-handling changes. `return a, b`1874+
# materializes as a fixed-arity tuple; bare `return` contributes1875+
# "nil" matching CRuby.1876+
args_id = @nd_arguments[nid]1877+
if args_id >= 01878+
arg_ids = get_args(args_id)1879+
if arg_ids.length == 11880+
return infer_type(arg_ids[0])1881+
end1882+
if arg_ids.length > 11883+
return tuple_type_from_elems(arg_ids)1884+
end1885+
end1886+
return "nil"1887+
end1888+
if t == "BeginNode"1889+
# Method bodies that end in `def f; X; rescue; Y; end` need the1890+
# method's inferred return type to unify both branches; without1891+
# this arm spinel falls back to int (the default), and string-1892+
# returning rescue bodies fail C compilation when the ret_tmp1893+
# slot's type doesn't match the value being assigned.1894+
types_b = "".split(",")1895+
bodies_b = begin_node_arm_bodies(nid)1896+
bi = 01897+
while bi < bodies_b.length1898+
bstmts = get_stmts(bodies_b[bi])1899+
if bstmts.length > 01900+
types_b.push(infer_type(bstmts.last))1901+
end1902+
bi = bi + 11903+
end1904+
if types_b.length > 01905+
return unify_return_type(types_b)1906+
end1907+
return "void"1908+
end18661909
if t == "LocalVariableReadNode"18671910
vt = find_var_type(@nd_name[nid])18681911
if vt != ""@@ -14416,6 +14459,42 @@
1441614459
end1441714460
return1441814461
end14462+
if @nd_type[nid] == "BeginNode"14463+
# Walk both the begin body and each rescue clause body so an14464+
# explicit `return X` inside either branch contributes to the14465+
# method's return-type inference. Mirrors the IfNode arm above.14466+
bodies_cr = begin_node_arm_bodies(nid)14467+
bi = 014468+
while bi < bodies_cr.length14469+
collect_return_types_nid(bodies_cr[bi], types)14470+
bi = bi + 114471+
end14472+
return14473+
end14474+
end14475+
14476+
# Returns the body-stmt-list IDs for each arm of a BeginNode (the14477+
# begin body followed by each rescue clause's body in chain order),14478+
# filtering out missing bodies (negative IDs). Used by both the14479+
# `infer_type` BeginNode arm (last-stmt type unification) and the14480+
# `collect_return_types` BeginNode arm (explicit-return walk).14481+
# `[]` (not `"".split(",")`) is the int_array init idiom in this14482+
# codebase — see the @nd_* parallel arrays in `initialize`.14483+
def begin_node_arm_bodies(nid)14484+
out = []14485+
bid = @nd_body[nid]14486+
if bid >= 014487+
out.push(bid)14488+
end14489+
rc = @nd_rescue_clause[nid]14490+
while rc >= 014491+
rb = @nd_body[rc]14492+
if rb >= 014493+
out.push(rb)14494+
end14495+
rc = @nd_subsequent[rc]14496+
end14497+
out1441914498
end1442014499
1442114500
def unify_return_type(types)spinel_codegen.rbmodifiedView file
Loading diff…
test/method_level_rescue_return.rbaddedView file
Loading diff…
test/method_level_rescue_return.rb.expectedaddedView file
Loading diff…
test/method_level_rescue_return_string.rbaddedView file
Loading diff…
test/method_level_rescue_return_string.rb.expectedaddedView file
Loading diff…