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 end
18641864 return t1
18651865 end
1866+ if t == "ReturnNode"
1867+ # `return X` evaluated in expression position — same shape as
1868+ # collect_return_types' ReturnNode arm. Without this case the
1869+ # universal `"int"` fallback fires; in practice that's demoted
1870+ # by unify_return_type's int-as-sentinel logic when paired with
1871+ # a concrete type from collect_return_types, but returning the
1872+ # actual arg type makes infer_type semantically correct and
1873+ # resilient to future sentinel-handling changes. `return a, b`
1874+ # materializes as a fixed-arity tuple; bare `return` contributes
1875+ # "nil" matching CRuby.
1876+ args_id = @nd_arguments[nid]
1877+ if args_id >= 0
1878+ arg_ids = get_args(args_id)
1879+ if arg_ids.length == 1
1880+ return infer_type(arg_ids[0])
1881+ end
1882+ if arg_ids.length > 1
1883+ return tuple_type_from_elems(arg_ids)
1884+ end
1885+ end
1886+ return "nil"
1887+ end
1888+ if t == "BeginNode"
1889+ # Method bodies that end in `def f; X; rescue; Y; end` need the
1890+ # method's inferred return type to unify both branches; without
1891+ # this arm spinel falls back to int (the default), and string-
1892+ # returning rescue bodies fail C compilation when the ret_tmp
1893+ # slot's type doesn't match the value being assigned.
1894+ types_b = "".split(",")
1895+ bodies_b = begin_node_arm_bodies(nid)
1896+ bi = 0
1897+ while bi < bodies_b.length
1898+ bstmts = get_stmts(bodies_b[bi])
1899+ if bstmts.length > 0
1900+ types_b.push(infer_type(bstmts.last))
1901+ end
1902+ bi = bi + 1
1903+ end
1904+ if types_b.length > 0
1905+ return unify_return_type(types_b)
1906+ end
1907+ return "void"
1908+ end
18661909 if t == "LocalVariableReadNode"
18671910 vt = find_var_type(@nd_name[nid])
18681911 if vt != ""
@@ -14416,6 +14459,42 @@
1441614459 end
1441714460 return
1441814461 end
14462+ if @nd_type[nid] == "BeginNode"
14463+ # Walk both the begin body and each rescue clause body so an
14464+ # explicit `return X` inside either branch contributes to the
14465+ # method's return-type inference. Mirrors the IfNode arm above.
14466+ bodies_cr = begin_node_arm_bodies(nid)
14467+ bi = 0
14468+ while bi < bodies_cr.length
14469+ collect_return_types_nid(bodies_cr[bi], types)
14470+ bi = bi + 1
14471+ end
14472+ return
14473+ end
14474+ end
14475+
14476+ # Returns the body-stmt-list IDs for each arm of a BeginNode (the
14477+ # begin body followed by each rescue clause's body in chain order),
14478+ # filtering out missing bodies (negative IDs). Used by both the
14479+ # `infer_type` BeginNode arm (last-stmt type unification) and the
14480+ # `collect_return_types` BeginNode arm (explicit-return walk).
14481+ # `[]` (not `"".split(",")`) is the int_array init idiom in this
14482+ # 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 >= 0
14487+ out.push(bid)
14488+ end
14489+ rc = @nd_rescue_clause[nid]
14490+ while rc >= 0
14491+ rb = @nd_body[rc]
14492+ if rb >= 0
14493+ out.push(rb)
14494+ end
14495+ rc = @nd_subsequent[rc]
14496+ end
14497+ out
1441914498 end
1442014499
1442114500 def unify_return_type(types)