Commit 95572f7c

95572f7ccfd3Merged

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

Description

fix(analyze): back-propagate ptr / obj callee slot to caller param

A method param passed unmodified to a callee whose corresponding slot is a pointer-y concrete type (ptr for FFI void*-via-:ptr, or obj_<C> for a typed class pointer) used to stay at the default mrb_int even though the callee's signature already pinned the expected type. C compile then failed when the caller passed its mrb_int param into the void*-expecting slot.

Real-blog archive shape (runtime/db.rb):

def self.column_int(stmt, i) # stmt : void * via FFI SQL.sqlite3_column_int(stmt, i) end

def self.column_bool(stmt, idx) # stmt was mrb_int column_int(stmt, idx) != 0 # ← passes mrb_int to void * end

spinel's call-site widening only goes caller → callee (widens the callee's slot from the caller's arg). The reverse direction (callee's typed slot drives the caller's still-default param) was unhandled.

Add infer_param_type_from_callee_slot:

  • Walk each method body; for each param still typed "int", collect every (callee_mname, arg_pos) where the param appears as the arg_pos-th LVR arg of some CallNode.
  • Resolve callee_mname via find_method_idx (top-level) or the <lexical_scope>_cls_<mname> synthetic (sibling cls method), looking up the slot at arg_pos.
  • If every observation agrees on a ptr or obj_<C> slot, widen the param to that type. Disagreement leaves the param as int (defensive — same hygiene as is_string_only_method's "single signal" rule in #450 cascade 2).

A new collect_param_callee_slots helper accumulates the observations with the standard DefNode / ClassNode / ModuleNode boundary guards from the f251f77 audit so nested scopes don't leak shadowed-name observations into the outer set.

A callee_slot_type helper centralises the bare-mname / synthetic-cls-method resolution. infer_param_type_from_callee_slot derives the lexical scope from each method's own name (Db_cls_column_bool → "Db") so a bare sibling call inside the body resolves to the right Db_cls_* synthetic.

Runs in the iterative analyze loop right after the infer_string_param_from_body pass introduced for #450 cascade 2.

Real-blog archive: errors 57 → 56 (Db.column_bool closes; the remaining 56 are unrelated cascades). Tests 409 pass (+1 regression covering FFI-style ptr propagation and a three-deep chain widening), bench 52, optcarrot checksum 59662.

Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]

Details

Commit
95572f7ccfd3
Parent
d1264134d353

spinel_analyze.rbmodifiedView file

@@ -10888,6 +10888,144 @@
1088810888 # infer_cls_meth_param_from_body's class-promotion shape, but
1088910889 # specialised for the String case where the canonical receiver
1089010890 # is the primitive rather than a user class.
10891+ # Walk `nid`'s subtree collecting `(callee_mname, arg_pos)` pairs
10892+ # for every CallNode whose `arg_pos`-th positional arg is a
10893+ # LocalVariableReadNode named `pname`. Used by
10894+ # infer_param_type_from_callee_slot to back-propagate a callee's
10895+ # concrete slot type to the caller's still-`int` param.
10896+ def collect_param_callee_slots(nid, pname, acc)
10897+ if nid < 0
10898+ return
10899+ end
10900+ # Don't cross nested DefNode / ClassNode / ModuleNode bodies.
10901+ if @nd_type[nid] == "DefNode"
10902+ return
10903+ end
10904+ if @nd_type[nid] == "ClassNode" || @nd_type[nid] == "ModuleNode"
10905+ return
10906+ end
10907+ if @nd_type[nid] == "CallNode"
10908+ args_id = @nd_arguments[nid]
10909+ if args_id >= 0
10910+ aargs = get_args(args_id)
10911+ ai = 0
10912+ while ai < aargs.length
10913+ if @nd_type[aargs[ai]] == "LocalVariableReadNode" && @nd_name[aargs[ai]] == pname
10914+ acc.push(@nd_name[nid] + "\t" + ai.to_s)
10915+ end
10916+ ai = ai + 1
10917+ end
10918+ end
10919+ end
10920+ cs = []
10921+ push_child_ids(nid, cs)
10922+ k = 0
10923+ while k < cs.length
10924+ collect_param_callee_slots(cs[k], pname, acc)
10925+ k = k + 1
10926+ end
10927+ end
10928+
10929+ # Walk every method body. For each param still typed "int" (the
10930+ # placeholder default), look at where it's passed as an arg to
10931+ # another method. If every observed callee's matching slot is the
10932+ # same concrete pointer-y type (`ptr` / `obj_<C>` / specific
10933+ # array/hash), widen the param to that type. Mirrors the shape of
10934+ # `infer_string_param_from_body` (#450 cascade 2) but driven by
10935+ # callee-slot evidence rather than receiver-method evidence.
10936+ #
10937+ # Originally added for the Db.column_bool shape:
10938+ # def self.column_bool(stmt, idx); column_int(stmt, idx) != 0; end
10939+ # where column_int's slot is `void *` (ptr) via FFI inference
10940+ # but column_bool's `stmt` stayed mrb_int, breaking C compile at
10941+ # `sp_Db_cls_column_int(lv_stmt, ...)`.
10942+ def infer_param_type_from_callee_slot
10943+ mi = 0
10944+ while mi < @meth_names.length
10945+ bid = @meth_body_ids[mi]
10946+ if bid >= 0
10947+ pnames = @meth_param_names[mi].split(",")
10948+ ptypes = @meth_param_types[mi].split(",")
10949+ changed_cs = 0
10950+ # Derive the lexical scope from the method name itself —
10951+ # `Db_cls_column_bool` → "Db" — so a bare sibling call like
10952+ # `column_int(stmt, idx)` inside the body resolves to the
10953+ # `Db_cls_column_int` synthetic.
10954+ saved_scope_cs = @current_lexical_scope
10955+ mname_cs = @meth_names[mi]
10956+ cls_marker_cs = mname_cs.index("_cls_")
10957+ if cls_marker_cs != nil
10958+ @current_lexical_scope = mname_cs[0, cls_marker_cs]
10959+ end
10960+ pk = 0
10961+ while pk < pnames.length
10962+ if pk < ptypes.length && ptypes[pk] == "int"
10963+ obs = "".split(",")
10964+ collect_param_callee_slots(bid, pnames[pk], obs)
10965+ agreed = ""
10966+ disagree = 0
10967+ kk = 0
10968+ while kk < obs.length
10969+ tab = obs[kk].index("\t")
10970+ if tab >= 0
10971+ callee_name = obs[kk][0, tab]
10972+ pos_s = obs[kk][tab + 1, obs[kk].length - tab - 1]
10973+ pos = pos_s.to_i
10974+ callee_t = callee_slot_type(callee_name, pos)
10975+ if callee_t == "ptr" || is_obj_type(callee_t) == 1
10976+ if agreed == ""
10977+ agreed = callee_t
10978+ elsif agreed != callee_t
10979+ disagree = 1
10980+ end
10981+ end
10982+ end
10983+ kk = kk + 1
10984+ end
10985+ if agreed != "" && disagree == 0
10986+ ptypes[pk] = agreed
10987+ changed_cs = 1
10988+ end
10989+ end
10990+ pk = pk + 1
10991+ end
10992+ @current_lexical_scope = saved_scope_cs
10993+ if changed_cs == 1
10994+ @meth_param_types[mi] = ptypes.join(",")
10995+ end
10996+ end
10997+ mi = mi + 1
10998+ end
10999+ end
11000+
11001+ # Resolve `callee_name` (a bare-call mname or a `<Mod>_cls_<m>`
11002+ # synthetic) to its param-types array and return the slot at
11003+ # `pos`, or "" if unresolvable. Used by
11004+ # infer_param_type_from_callee_slot to look up evidence from a
11005+ # callee body without committing to receiver-resolution
11006+ # (`Db.column_int` vs sibling-call `column_int` lookups).
11007+ def callee_slot_type(callee_name, pos)
11008+ cmi = find_method_idx(callee_name)
11009+ if cmi >= 0
11010+ cpts = @meth_param_types[cmi].split(",")
11011+ if pos < cpts.length
11012+ return cpts[pos]
11013+ end
11014+ end
11015+ # Try sibling-class-method synth: `<CurrentScope>_cls_<callee_name>`.
11016+ if @current_lexical_scope != ""
11017+ synth = @current_lexical_scope + "_cls_" + callee_name
11018+ smi = find_method_idx(synth)
11019+ if smi >= 0
11020+ spts = @meth_param_types[smi].split(",")
11021+ if pos < spts.length
11022+ return spts[pos]
11023+ end
11024+ end
11025+ end
11026+ ""
11027+ end
11028+
1089111029 def infer_string_param_from_body
1089211030 mi = 0
1089311031 while mi < @meth_names.length
@@ -15546,6 +15684,7 @@
1554615684 infer_param_array_type_from_body
1554715685 narrow_param_types_from_body_method_calls
1554815686 infer_string_param_from_body
15687+ infer_param_type_from_callee_slot
1554915688 narrow_param_hash_types_from_body_writes
1555015689 # propagate hash-each block-arg types into
1555115690 # nested cmeth/method-call param widening. Runs inside