Commit 491f016d

491f016d0fd1Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 1 file · ~1 modified

Description

feat(analyze): method-level RBS definite-conflict detection (#626 sub-issue 2)

Phase 2 add-on. Mirrors the ivar arbitration's definite-literal conflict path (#634 phase 1 model) to method returns and method param call sites.

Return conflict path: arbitrate_rbs_method_returns_definite_conflict scans every RBS-pinned method body for return <literal> statements and the implicit final-expression literal type. A literal whose type can't unify with the RBS-declared return errors out with 'RBS conflict on Class#method return: declared X but a literal expression infers Y'. Covers @cls_meth_, @cls_cmeth_, and top-level / module @meth_*.

Param conflict path: arbitrate_rbs_method_params_definite_conflict walks every CallNode in the program once after annotate_all_node_types populates the type cache. For each call that targets an RBS-pinned method (toplevel, ConstantReadNode receiver, or obj_-typed receiver via the type cache), each positional arg is checked: a literal arg whose type contradicts the RBS- declared param at that index errors with 'RBS conflict on Class#method param N: declared X but a literal arg infers Y'.

Known gap: when the receiver is a LocalVariableReadNode (the common c = Foo.new; c.method(...) pattern), infer_type needs a populated scope stack to resolve the local's type. The arbitration walker runs outside any per-method scope, so this case falls through to the default "int" and the RBS lookup misses. Exercising it requires the scope-aware walk that infer_main_call_types / scan_writer_calls build up; filed as a followup if the gap matters.

Verified manually: literal-int return from a def label: () -> String errors. Calc.parse(42) against a def self.parse: (String) -> Integer errors. Conforming code (no literal-arg conflict) builds clean.

Refs #626 (sub-issue 2).

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

Details

Commit
491f016d0fd1
Parent
16184cb7ed88

spinel_analyze.rbmodifiedView file

@@ -20399,6 +20399,16 @@
2039920399 # RBS as needed. Restores the RBS-declared type as the final
2040020400 # inferred type either way.
2040120401 def arbitrate_rbs_vs_inferred_methods
20402+ # Definite-conflict detection for returns. A LITERAL return whose
20403+ # type can't unify with the RBS-declared return errors out before
20404+ # we silently restore. Same confidence model as the ivar pass
20405+ # (#634 phase 1): the user has written something concrete that
20406+ # contradicts their own RBS line, and only they can fix it. The
20407+ # literal-arg-at-param-call-site sibling check needs the
20408+ # node-type annotation cache populated (so the recv's class
20409+ # resolves), so it runs in its own pass after
20410+ # annotate_all_node_types.
20411+ arbitrate_rbs_method_returns_definite_conflict
2040220412 # Class instance methods.
2040320413 @cls_meth_rbs_ptypes.each_pair { |k, rbs_pt|
2040420414 parts = k.split("|")
@@ -20471,6 +20481,313 @@
2047120481 }
2047220482 end
2047320483
20484+ # Returns the literal type if `nid` is an AST literal expression,
20485+ # else "". Mirrors the relevant arms of `infer_ivar_init_type`
20486+ # but stays focused on the literal-node shapes we want to treat
20487+ # as high-confidence (no transitive inference). Used by the
20488+ # method-arbitration definite-conflict check.
20489+ def rbs_literal_expr_type(nid)
20490+ if nid < 0
20491+ return ""
20492+ end
20493+ t = @nd_type[nid]
20494+ if t == "IntegerNode"
20495+ return "int"
20496+ end
20497+ if t == "FloatNode"
20498+ return "float"
20499+ end
20500+ if t == "StringNode"
20501+ return "string"
20502+ end
20503+ if t == "SymbolNode"
20504+ return "symbol"
20505+ end
20506+ if t == "TrueNode" || t == "FalseNode"
20507+ return "bool"
20508+ end
20509+ if t == "NilNode"
20510+ return "nil"
20511+ end
20512+ ""
20513+ end
20514+
20515+ # Walk a method body and collect literal-expr types for every
20516+ # `return X` node, plus the body's implicit final-expr type if
20517+ # it's a literal. Output is appended to `acc` (caller-owned).
20518+ def collect_rbs_literal_returns(nid, acc)
20519+ if nid < 0
20520+ return
20521+ end
20522+ if @nd_type[nid] == "DefNode"
20523+ return
20524+ end
20525+ if @nd_type[nid] == "ReturnNode"
20526+ args_id = @nd_arguments[nid]
20527+ if args_id >= 0
20528+ arg_ids = get_args(args_id)
20529+ if arg_ids.length == 1
20530+ lt = rbs_literal_expr_type(arg_ids[0])
20531+ if lt != ""
20532+ acc.push(lt)
20533+ end
20534+ end
20535+ end
20536+ return
20537+ end
20538+ cs = []
20539+ push_child_ids(nid, cs)
20540+ k = 0
20541+ while k < cs.length
20542+ collect_rbs_literal_returns(cs[k], acc)
20543+ k = k + 1
20544+ end
20545+ end
20546+
20547+ # Returns the literal type of a body's implicit final expression
20548+ # (the last statement when no explicit return covers the path).
20549+ # "" when the final stmt isn't a literal node, when the body is
20550+ # empty, or when the body's last stmt is itself a return (in which
20551+ # case collect_rbs_literal_returns already covers it).
20552+ def rbs_body_implicit_return_literal(body_id)
20553+ if body_id < 0
20554+ return ""
20555+ end
20556+ stmts = get_stmts(body_id)
20557+ if stmts.length == 0
20558+ return ""
20559+ end
20560+ last = stmts.last
20561+ rbs_literal_expr_type(last)
20562+ end
20563+
20564+ def arbitrate_rbs_method_returns_definite_conflict
20565+ @cls_meth_rbs_returns.each_pair { |k, rbs_ret|
20566+ parts = k.split("|")
20567+ ci = parts[0].to_i
20568+ midx = parts[1].to_i
20569+ if ci < 0 || ci >= @cls_meth_bodies.length
20570+ next
20571+ end
20572+ bodies = @cls_meth_bodies[ci].split(";")
20573+ if midx >= bodies.length
20574+ next
20575+ end
20576+ bid = bodies[midx].to_i
20577+ if bid < 0
20578+ next
20579+ end
20580+ lits = "".split(",")
20581+ collect_rbs_literal_returns(bid, lits)
20582+ implicit = rbs_body_implicit_return_literal(bid)
20583+ if implicit != ""
20584+ lits.push(implicit)
20585+ end
20586+ mnames = @cls_meth_names[ci].split(";")
20587+ mname = midx < mnames.length ? mnames[midx] : "?"
20588+ li = 0
20589+ while li < lits.length
20590+ lt = lits[li]
20591+ if rbs_inferred_incompatible?(rbs_ret, lt)
20592+ $stderr.puts "Spinel: RBS conflict on " + @cls_names[ci] + "#" + mname + " return: declared \"" + rbs_ret + "\" but a literal expression infers \"" + lt + "\""
20593+ exit(1)
20594+ end
20595+ li = li + 1
20596+ end
20597+ }
20598+ @cls_cmeth_rbs_returns.each_pair { |k, rbs_ret|
20599+ parts = k.split("|")
20600+ ci = parts[0].to_i
20601+ midx = parts[1].to_i
20602+ if ci < 0 || ci >= @cls_cmeth_bodies.length
20603+ next
20604+ end
20605+ bodies = @cls_cmeth_bodies[ci].split(";")
20606+ if midx >= bodies.length
20607+ next
20608+ end
20609+ bid = bodies[midx].to_i
20610+ if bid < 0
20611+ next
20612+ end
20613+ lits = "".split(",")
20614+ collect_rbs_literal_returns(bid, lits)
20615+ implicit = rbs_body_implicit_return_literal(bid)
20616+ if implicit != ""
20617+ lits.push(implicit)
20618+ end
20619+ cmnames = @cls_cmeth_names[ci].split(";")
20620+ mname = midx < cmnames.length ? cmnames[midx] : "?"
20621+ li = 0
20622+ while li < lits.length
20623+ lt = lits[li]
20624+ if rbs_inferred_incompatible?(rbs_ret, lt)
20625+ $stderr.puts "Spinel: RBS conflict on " + @cls_names[ci] + ".#" + mname + " return: declared \"" + rbs_ret + "\" but a literal expression infers \"" + lt + "\""
20626+ exit(1)
20627+ end
20628+ li = li + 1
20629+ end
20630+ }
20631+ @meth_rbs_returns.each_pair { |k, rbs_ret|
20632+ mi = k.to_i
20633+ if mi < 0 || mi >= @meth_body_ids.length
20634+ next
20635+ end
20636+ bid = @meth_body_ids[mi]
20637+ if bid < 0
20638+ next
20639+ end
20640+ lits = "".split(",")
20641+ collect_rbs_literal_returns(bid, lits)
20642+ implicit = rbs_body_implicit_return_literal(bid)
20643+ if implicit != ""
20644+ lits.push(implicit)
20645+ end
20646+ mname = mi < @meth_names.length ? @meth_names[mi] : "?"
20647+ li = 0
20648+ while li < lits.length
20649+ lt = lits[li]
20650+ if rbs_inferred_incompatible?(rbs_ret, lt)
20651+ $stderr.puts "Spinel: RBS conflict on " + mname + " return: declared \"" + rbs_ret + "\" but a literal expression infers \"" + lt + "\""
20652+ exit(1)
20653+ end
20654+ li = li + 1
20655+ end
20656+ }
20657+ end
20658+
20659+ # Scan every CallNode in the program. For each call that targets
20660+ # an RBS-pinned method, check each positional arg: if the arg is
20661+ # a literal whose type is incompatible with the RBS-declared
20662+ # param at that index, error. Single full-tree walk; the cost is
20663+ # amortized across all RBS-pinned methods.
20664+ def arbitrate_rbs_method_params_definite_conflict
20665+ if @cls_meth_rbs_ptypes.length == 0 &&
20666+ @cls_cmeth_rbs_ptypes.length == 0 &&
20667+ @meth_rbs_ptypes.length == 0
20668+ return
20669+ end
20670+ rbs_scan_callsites(@root_id)
20671+ end
20672+
20673+ def rbs_scan_callsites(nid)
20674+ if nid < 0
20675+ return
20676+ end
20677+ if @nd_type[nid] == "CallNode"
20678+ rbs_check_callsite(nid)
20679+ end
20680+ cs = []
20681+ push_child_ids(nid, cs)
20682+ k = 0
20683+ while k < cs.length
20684+ rbs_scan_callsites(cs[k])
20685+ k = k + 1
20686+ end
20687+ end
20688+
20689+ def rbs_check_callsite(nid)
20690+ mname = @nd_name[nid]
20691+ if mname == ""
20692+ return
20693+ end
20694+ args_id = @nd_arguments[nid]
20695+ if args_id < 0
20696+ return
20697+ end
20698+ arg_ids = get_args(args_id)
20699+ if arg_ids.length == 0
20700+ return
20701+ end
20702+ # Build a (rbs_ptypes_token, label) pair from the matching shadow
20703+ # entry. Three lookups are tried in order; the first match wins.
20704+ rbs_pt = ""
20705+ label = ""
20706+ recv = @nd_receiver[nid]
20707+ if recv < 0
20708+ # Top-level method call.
20709+ mi = find_method_idx(mname)
20710+ if mi >= 0
20711+ key = mi.to_s
20712+ if @meth_rbs_ptypes.key?(key)
20713+ rbs_pt = @meth_rbs_ptypes[key]
20714+ label = mname
20715+ end
20716+ end
20717+ end
20718+ if rbs_pt == "" && recv >= 0
20719+ # Instance method call against a class-typed recv. Note:
20720+ # `infer_type(recv)` for a `LocalVariableReadNode` resolves
20721+ # through `find_var_type`, which needs a scope stack
20722+ # populated. The arbitration walker runs outside any scope
20723+ # context, so a `c.add(literal)` where `c` is a local variable
20724+ # falls through to the default "int" path and the RBS lookup
20725+ # misses. That case is the most common in practice but
20726+ # exercising it correctly requires the scope-aware walk
20727+ # `infer_main_call_types` / `scan_writer_calls` build up;
20728+ # adding that here would double the pass's footprint without
20729+ # changing the constant- and toplevel-recv coverage. Leaving
20730+ # it as a known gap for now.
20731+ rt = infer_type(recv)
20732+ if rt.length > 4 && rt[0, 4] == "obj_"
20733+ rcname = rt[4, rt.length - 4]
20734+ rci = find_class_idx(rcname)
20735+ if rci >= 0
20736+ rmidx = cls_find_method_direct(rci, mname)
20737+ if rmidx >= 0
20738+ key2 = rci.to_s + "|" + rmidx.to_s
20739+ if @cls_meth_rbs_ptypes.key?(key2)
20740+ rbs_pt = @cls_meth_rbs_ptypes[key2]
20741+ label = rcname + "#" + mname
20742+ end
20743+ end
20744+ end
20745+ end
20746+ end
20747+ if rbs_pt == "" && recv >= 0
20748+ # Class-method call against a ConstantReadNode recv (Foo.bar).
20749+ if @nd_type[recv] == "ConstantReadNode"
20750+ rcname = @nd_name[recv]
20751+ rci = find_class_idx(rcname)
20752+ if rci >= 0
20753+ mnames_c = @cls_cmeth_names[rci].split(";")
20754+ cmidx = -1
20755+ mc = 0
20756+ while mc < mnames_c.length
20757+ if mnames_c[mc] == mname
20758+ cmidx = mc
20759+ mc = mnames_c.length
20760+ else
20761+ mc = mc + 1
20762+ end
20763+ end
20764+ if cmidx >= 0
20765+ key3 = rci.to_s + "|" + cmidx.to_s
20766+ if @cls_cmeth_rbs_ptypes.key?(key3)
20767+ rbs_pt = @cls_cmeth_rbs_ptypes[key3]
20768+ label = rcname + "." + mname
20769+ end
20770+ end
20771+ end
20772+ end
20773+ end
20774+ if rbs_pt == ""
20775+ return
20776+ end
20777+ rbs_ptypes = rbs_pt.split(",")
20778+ ak = 0
20779+ while ak < arg_ids.length && ak < rbs_ptypes.length
20780+ arg_id = arg_ids[ak]
20781+ lt = rbs_literal_expr_type(arg_id)
20782+ rbs_t = rbs_ptypes[ak]
20783+ if lt != "" && rbs_t != "" && rbs_inferred_incompatible?(rbs_t, lt)
20784+ $stderr.puts "Spinel: RBS conflict on " + label + " param " + (ak + 1).to_s + ": declared \"" + rbs_t + "\" but a literal arg infers \"" + lt + "\""
20785+ exit(1)
20786+ end
20787+ ak = ak + 1
20788+ end
20789+ end
20790+
2047420791 def seed_class_method(ci, mname, ret, ptypes_token)
2047520792 midx = cls_find_method_direct(ci, mname)
2047620793 if midx < 0
@@ -20838,6 +21155,13 @@
2083821155 @analysis_frozen = 1
2083921156 precompute_all_scope_decls
2084021157 annotate_all_node_types
21158+ # Now that @nd_inferred_type is populated for every reachable
21159+ # node, we can resolve `recv.method(...)`'s receiver class
21160+ # cheaply. Check each RBS-pinned method's positional args at
21161+ # every call site: a literal arg whose type contradicts the
21162+ # RBS-declared param errors. The sibling return-conflict check
21163+ # already ran inside arbitrate_rbs_vs_inferred_methods.
21164+ arbitrate_rbs_method_params_definite_conflict
2084121165 infer_proc_blk_param_types
2084221166 if ENV["SP_POLY_REPORT"] == "1"
2084321167 emit_poly_report