Commit 216bee0d

216bee0d3f7eMerged

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

Description

feat(analyze): close LocalVariableReadNode recv gap in RBS param conflict (#626)

Followup to fa12337. The known gap there was that arbitrate_rbs_method_params_definite_conflict walked the AST outside any scope context, so infer_type on a LocalVariableReadNode receiver fell through to the int default and the RBS-pinned-method lookup missed the most common case -- c = Foo.new; c.method(literal).

Replace the bare AST walk with a scope-aware variant (rbs_walk_with_scope) that mirrors the pattern in infer_main_call_types / scan_writer_calls:

  • At ProgramNode / DefNode bodies: push_scope, read precomputed locals from @nd_scope_names[bid] / @nd_scope_types[bid] (already populated by precompute_all_scope_decls), declare each var, recurse, pop_scope.
  • At ClassNode / ModuleNode: set @current_class_idx via enter_class_scope_from_node / enter_module_scope_from_node so any class-level constant resolution falls under the expected lexical scope, then recurse into the body's statements (each child DefNode handles its own push_scope).

With locals declared, infer_type(recv) on a LocalVariableReadNode resolves through find_var_type to the local's concrete obj_X type, the RBS-pinned-method lookup finds the matching @cls_meth_rbs_ptypes entry, and any literal positional arg incompatible with the RBS-declared param errors out.

Verified manually:

c = Calc.new; c.add(1, "two") against def add: (Integer, Integer) -> Integer → "RBS conflict on Calc#add param 2: declared int but a literal arg infers string"

Conforming caller still builds clean.

The three older cases (constant-recv class-method, top-level method, literal-return) all still error correctly.

Refs #626 (sub-issue 2).

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

Details

Commit
216bee0d3f7e
Parent
491f016d0fd1

spinel_analyze.rbmodifiedView file

@@ -20667,7 +20667,91 @@
2066720667 @meth_rbs_ptypes.length == 0
2066820668 return
2066920669 end
20670- rbs_scan_callsites(@root_id)
20670+ # Walk every body with its precomputed scope set up so
20671+ # `infer_type` on a `LocalVariableReadNode` resolves through
20672+ # `find_var_type` to the local's concrete type (e.g. `c = Foo.new`
20673+ # → `c` typed `obj_Foo`). Without the scope wrap, a
20674+ # `c.method(literal)` receiver would fall through to the int
20675+ # default and the RBS-pinned-method lookup miss.
20676+ rbs_walk_with_scope(@root_id)
20677+ end
20678+
20679+ # Push the body's precomputed locals into the scope stack, walk
20680+ # the body checking CallNodes, then pop. Mirrors the pattern in
20681+ # `infer_main_call_types` / `scan_writer_calls` but reuses
20682+ # `@nd_scope_names` / `@nd_scope_types` filled by
20683+ # `precompute_all_scope_decls` rather than recomputing.
20684+ def rbs_walk_body_scoped(bid)
20685+ if bid < 0
20686+ return
20687+ end
20688+ push_scope
20689+ sn = @nd_scope_names[bid]
20690+ st = @nd_scope_types[bid]
20691+ if sn != ""
20692+ lnames = sn.split("|", -1)
20693+ ltypes = st.split("|", -1)
20694+ li = 0
20695+ while li < lnames.length
20696+ if lnames[li] != "" && li < ltypes.length
20697+ declare_var(lnames[li], ltypes[li])
20698+ end
20699+ li = li + 1
20700+ end
20701+ end
20702+ rbs_scan_callsites(bid)
20703+ pop_scope
20704+ end
20705+
20706+ def rbs_walk_with_scope(nid)
20707+ if nid < 0
20708+ return
20709+ end
20710+ t = @nd_type[nid]
20711+ if t == "ProgramNode"
20712+ bid = @nd_body[nid]
20713+ if bid >= 0
20714+ rbs_walk_body_scoped(@root_id)
20715+ bsts = get_stmts(bid)
20716+ bsi = 0
20717+ while bsi < bsts.length
20718+ rbs_walk_with_scope(bsts[bsi])
20719+ bsi = bsi + 1
20720+ end
20721+ end
20722+ return
20723+ end
20724+ if t == "ClassNode" || t == "ModuleNode"
20725+ saved_ci = @current_class_idx
20726+ saved_scope = @current_lexical_scope
20727+ if t == "ClassNode"
20728+ enter_class_scope_from_node(nid)
20729+ else
20730+ enter_module_scope_from_node(nid)
20731+ end
20732+ bid = @nd_body[nid]
20733+ if bid >= 0
20734+ bsts = get_stmts(bid)
20735+ bsi = 0
20736+ while bsi < bsts.length
20737+ rbs_walk_with_scope(bsts[bsi])
20738+ bsi = bsi + 1
20739+ end
20740+ end
20741+ @current_class_idx = saved_ci
20742+ @current_lexical_scope = saved_scope
20743+ return
20744+ end
20745+ if t == "DefNode"
20746+ bid = @nd_body[nid]
20747+ if bid >= 0
20748+ rbs_walk_body_scoped(bid)
20749+ end
20750+ return
20751+ end
20752+ # Other top-level statements: scope already set up by main's
20753+ # body walk above. Recurse via the unscoped walker.
20754+ rbs_scan_callsites(nid)
2067120755 end
2067220756
2067320757 def rbs_scan_callsites(nid)
@@ -20716,18 +20800,12 @@
2071620800 end
2071720801 end
2071820802 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.
20803+ # Instance method call against a class-typed recv. The
20804+ # scope-aware walker (`rbs_walk_with_scope`) pushes the body's
20805+ # precomputed locals before reaching this check, so
20806+ # `infer_type` resolves a `LocalVariableReadNode` recv
20807+ # (`c = Foo.new; c.method(...)`) through `find_var_type` to
20808+ # the local's concrete type.
2073120809 rt = infer_type(recv)
2073220810 if rt.length > 4 && rt[0, 4] == "obj_"
2073320811 rcname = rt[4, rt.length - 4]