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
ptrorobj_<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
# infer_cls_meth_param_from_body's class-promotion shape, but # specialised for the String case where the canonical receiver # is the primitive rather than a user class. # Walk `nid`'s subtree collecting `(callee_mname, arg_pos)` pairs # for every CallNode whose `arg_pos`-th positional arg is a # LocalVariableReadNode named `pname`. Used by # infer_param_type_from_callee_slot to back-propagate a callee's # concrete slot type to the caller's still-`int` param. def collect_param_callee_slots(nid, pname, acc) if nid < 0 return end # Don't cross nested DefNode / ClassNode / ModuleNode bodies. if @nd_type[nid] == "DefNode" return end if @nd_type[nid] == "ClassNode" || @nd_type[nid] == "ModuleNode" return end if @nd_type[nid] == "CallNode" args_id = @nd_arguments[nid] if args_id >= 0 aargs = get_args(args_id) ai = 0 while ai < aargs.length if @nd_type[aargs[ai]] == "LocalVariableReadNode" && @nd_name[aargs[ai]] == pname acc.push(@nd_name[nid] + "\t" + ai.to_s) end ai = ai + 1 end end end cs = [] push_child_ids(nid, cs) k = 0 while k < cs.length collect_param_callee_slots(cs[k], pname, acc) k = k + 1 end end # Walk every method body. For each param still typed "int" (the # placeholder default), look at where it's passed as an arg to # another method. If every observed callee's matching slot is the # same concrete pointer-y type (`ptr` / `obj_<C>` / specific # array/hash), widen the param to that type. Mirrors the shape of # `infer_string_param_from_body` (#450 cascade 2) but driven by # callee-slot evidence rather than receiver-method evidence. # # Originally added for the Db.column_bool shape: # def self.column_bool(stmt, idx); column_int(stmt, idx) != 0; end # where column_int's slot is `void *` (ptr) via FFI inference # but column_bool's `stmt` stayed mrb_int, breaking C compile at # `sp_Db_cls_column_int(lv_stmt, ...)`. def infer_param_type_from_callee_slot mi = 0 while mi < @meth_names.length bid = @meth_body_ids[mi] if bid >= 0 pnames = @meth_param_names[mi].split(",") ptypes = @meth_param_types[mi].split(",") changed_cs = 0 # Derive the lexical scope from the method name itself — # `Db_cls_column_bool` → "Db" — so a bare sibling call like # `column_int(stmt, idx)` inside the body resolves to the # `Db_cls_column_int` synthetic. saved_scope_cs = @current_lexical_scope mname_cs = @meth_names[mi] cls_marker_cs = mname_cs.index("_cls_") if cls_marker_cs != nil @current_lexical_scope = mname_cs[0, cls_marker_cs] end pk = 0 while pk < pnames.length if pk < ptypes.length && ptypes[pk] == "int" obs = "".split(",") collect_param_callee_slots(bid, pnames[pk], obs) agreed = "" disagree = 0 kk = 0 while kk < obs.length tab = obs[kk].index("\t") if tab >= 0 callee_name = obs[kk][0, tab] pos_s = obs[kk][tab + 1, obs[kk].length - tab - 1] pos = pos_s.to_i callee_t = callee_slot_type(callee_name, pos) if callee_t == "ptr" || is_obj_type(callee_t) == 1 if agreed == "" agreed = callee_t elsif agreed != callee_t disagree = 1 end end end kk = kk + 1 end if agreed != "" && disagree == 0 ptypes[pk] = agreed changed_cs = 1 end end pk = pk + 1 end @current_lexical_scope = saved_scope_cs if changed_cs == 1 @meth_param_types[mi] = ptypes.join(",") end end mi = mi + 1 end end # Resolve `callee_name` (a bare-call mname or a `<Mod>_cls_<m>` # synthetic) to its param-types array and return the slot at # `pos`, or "" if unresolvable. Used by # infer_param_type_from_callee_slot to look up evidence from a # callee body without committing to receiver-resolution # (`Db.column_int` vs sibling-call `column_int` lookups). def callee_slot_type(callee_name, pos) cmi = find_method_idx(callee_name) if cmi >= 0 cpts = @meth_param_types[cmi].split(",") if pos < cpts.length return cpts[pos] end end # Try sibling-class-method synth: `<CurrentScope>_cls_<callee_name>`. if @current_lexical_scope != "" synth = @current_lexical_scope + "_cls_" + callee_name smi = find_method_idx(synth) if smi >= 0 spts = @meth_param_types[smi].split(",") if pos < spts.length return spts[pos] end end end "" end def infer_string_param_from_body mi = 0 while mi < @meth_names.length infer_param_array_type_from_body narrow_param_types_from_body_method_calls infer_string_param_from_body infer_param_type_from_callee_slot narrow_param_hash_types_from_body_writes # propagate hash-each block-arg types into # nested cmeth/method-call param widening. Runs insidetest/param_ptr_from_callee_slot.rbaddedView file
Loading diff…
test/param_ptr_from_callee_slot.rb.expectedaddedView file
Loading diff…