Commit db4ac968

db4ac9684731Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 3 files · +2 added · ~1 modified

Description

Box mixed int / concrete-pointer method returns to poly

A method whose return paths mix a genuine int (an int literal or a written int ivar) with a concrete pointer type unified to that pointer type and silently returned the int branch through the pointer slot, failing C compilation (incompatible integer to pointer conversion).

The existing post-fixpoint widening (widen_returns_for_mixed_paths / method_returns_need_poly_widening) only inspected the top-level return-path types, so an int arm buried inside a tail branch expression -- e.g. def [](f); case f; when :id then @id; when :name then @name; end; end whose case unifies to a string -- was hidden by the branch unify and escaped widening.

Add a genuine-int-return walker that recurses into tail branch expressions (if/unless/case/case-in/begin) and explicit returns, and fold it into method_returns_need_poly_widening so a buried genuine int under a concrete pointer return boxes the whole return to poly. A "genuine" int is an int literal or a written int ivar; a var or call that merely defaults to int (the unresolved-default the unify heuristic exists for, as in the compiler's own type helpers) does not qualify. The check runs post-fixpoint, so a param-sourced path ivar reads as its converged string type rather than a transient int and does not force a spurious box.

Closes #1255

Co-Authored-By: Claude Opus 4.8 [email protected]

Details

Commit
db4ac9684731
Parent
ed62c43227ab

spinel_analyze.rbmodifiedView file

@@ -22192,6 +22192,17 @@
2219222192 end
2219322193 k = k + 1
2219422194 end
22195+ # The loop above only inspects the top-level return-path types
22196+ # (explicit returns plus the last statement's already-unified type).
22197+ # When the int return is buried inside a tail branch expression --
22198+ # `def [](f); if f==0; @id; else; @name; end; end` unifies the
22199+ # IfNode to "string", hiding the int @id arm -- recurse into the
22200+ # branches to find a genuine int and box the whole return to poly.
22201+ # Runs post-fixpoint, so ivar types are converged (a param-sourced
22202+ # path ivar reads as its real string type here, not a transient int).
22203+ if declared_is_ptr == 1 && body_has_genuine_int_return?(body_id) == 1
22204+ return 1
22205+ end
2219522206 0
2219622207 end
2219722208
@@ -22910,6 +22921,158 @@
2291022921 "int"
2291122922 end
2291222923
22924+ # When return-unify picks a concrete pointer type for a method that
22925+ # ALSO genuinely returns an int (an int literal, or a written int
22926+ # ivar, in tail/return position), the int branch would be silently
22927+ # mis-returned through the pointer slot. Detect that case so the
22928+ # caller can box the return to poly. "Genuine" excludes the
22929+ # unresolved-default int the unify heuristic exists for (a var or
22930+ # call that merely defaults to int, as in the compiler's own
22931+ # type-computing helpers); those keep their concrete return.
22932+ def body_has_genuine_int_return?(body_id)
22933+ body_genuine_int_returns?(body_id, 1)
22934+ end
22935+
22936+ def body_genuine_int_returns?(body_id, tail)
22937+ if body_id < 0
22938+ return 0
22939+ end
22940+ stmts = get_stmts(body_id)
22941+ n = stmts.length
22942+ if n == 0
22943+ return 0
22944+ end
22945+ gi = 0
22946+ while gi < n
22947+ st_tail = 0
22948+ if gi == n - 1
22949+ st_tail = tail
22950+ end
22951+ if stmt_genuine_int_return?(stmts[gi], st_tail) == 1
22952+ return 1
22953+ end
22954+ gi = gi + 1
22955+ end
22956+ 0
22957+ end
22958+
22959+ def stmt_genuine_int_return?(nid, tail)
22960+ if nid < 0
22961+ return 0
22962+ end
22963+ gt = @nd_type[nid]
22964+ if gt == "ReturnNode"
22965+ args_id = @nd_arguments[nid]
22966+ if args_id >= 0
22967+ a = get_args(args_id)
22968+ if a.length == 1
22969+ return leaf_genuine_int?(a[0])
22970+ end
22971+ end
22972+ return 0
22973+ end
22974+ if gt == "DefNode"
22975+ return 0
22976+ end
22977+ if gt == "IfNode"
22978+ if body_genuine_int_returns?(@nd_body[nid], tail) == 1
22979+ return 1
22980+ end
22981+ sub_g = @nd_subsequent[nid]
22982+ if sub_g >= 0 && stmt_genuine_int_return?(sub_g, tail) == 1
22983+ return 1
22984+ end
22985+ return 0
22986+ end
22987+ if gt == "UnlessNode"
22988+ if body_genuine_int_returns?(@nd_body[nid], tail) == 1
22989+ return 1
22990+ end
22991+ ec_g = @nd_else_clause[nid]
22992+ if ec_g >= 0 && stmt_genuine_int_return?(ec_g, tail) == 1
22993+ return 1
22994+ end
22995+ return 0
22996+ end
22997+ if gt == "ElseNode"
22998+ return body_genuine_int_returns?(@nd_body[nid], tail)
22999+ end
23000+ if gt == "WhileNode"
23001+ # A loop body's stmts are never the method's implicit tail, but an
23002+ # explicit `return <int>` inside still counts.
23003+ return body_genuine_int_returns?(@nd_body[nid], 0)
23004+ end
23005+ if gt == "CaseNode"
23006+ conds_g = parse_id_list(@nd_conditions[nid])
23007+ kg = 0
23008+ while kg < conds_g.length
23009+ wid_g = conds_g[kg]
23010+ if @nd_type[wid_g] == "WhenNode" && body_genuine_int_returns?(@nd_body[wid_g], tail) == 1
23011+ return 1
23012+ end
23013+ kg = kg + 1
23014+ end
23015+ ecn_g = @nd_else_clause[nid]
23016+ if ecn_g >= 0 && body_genuine_int_returns?(@nd_body[ecn_g], tail) == 1
23017+ return 1
23018+ end
23019+ return 0
23020+ end
23021+ if gt == "CaseMatchNode"
23022+ cm_g = parse_id_list(@nd_conditions[nid])
23023+ mi = 0
23024+ while mi < cm_g.length
23025+ if @nd_type[cm_g[mi]] == "InNode" && body_genuine_int_returns?(@nd_body[cm_g[mi]], tail) == 1
23026+ return 1
23027+ end
23028+ mi = mi + 1
23029+ end
23030+ return 0
23031+ end
23032+ if gt == "BeginNode"
23033+ arms_g = begin_node_arm_bodies(nid)
23034+ bgi = 0
23035+ while bgi < arms_g.length
23036+ if body_genuine_int_returns?(arms_g[bgi], tail) == 1
23037+ return 1
23038+ end
23039+ bgi = bgi + 1
23040+ end
23041+ return 0
23042+ end
23043+ if tail == 1
23044+ return leaf_genuine_int?(nid)
23045+ end
23046+ 0
23047+ end
23048+
23049+ # A value node counts as a genuine int only when it is an int literal
23050+ # or a read of a written int ivar. A plain var/call that merely
23051+ # infers int (the unresolved default) does NOT count.
23052+ def leaf_genuine_int?(nid)
23053+ if nid < 0
23054+ return 0
23055+ end
23056+ lt = @nd_type[nid]
23057+ if lt == "IntegerNode"
23058+ return 1
23059+ end
23060+ if lt == "InstanceVariableReadNode"
23061+ # An ivar reads as "int" both when it genuinely stores an int and
23062+ # when it is never written (the nonexistent-ivar default). Only the
23063+ # former -- an ivar actually recorded in the class's table -- is a
23064+ # genuine int return; a never-written ivar (e.g. an optional path
23065+ # slot only ever compared to nil) must not force a poly box.
23066+ if @current_class_idx >= 0
23067+ iname = @nd_name[nid]
23068+ if ivar_exists(@current_class_idx, iname) == 1 && cls_ivar_type(@current_class_idx, iname) == "int"
23069+ return 1
23070+ end
23071+ end
23072+ end
23073+ 0
23074+ end
23075+
2291323076 def infer_body_return(body_id)
2291423077 # An empty method body -- whether the parser yields no body node
2291523078 # (body_id < 0) or an empty statement list -- implicitly returns nil