Commit 2439f673

2439f673e5ccMerged

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

Description

fix(analyze): back-propagate callee ptr slot for real-class cmeths

#450 cascade follow-up. Two gaps in callee-slot back-propagation:

  1. infer_param_type_from_callee_slot iterated only @meth_* (top- level + module-class-method synthetics) and missed @cls_cmeth_* (real-class `def self.X`). A `def self.column_bool(stmt, idx)` inside `class Db` that forwards `stmt` to a sibling `column_int(stmt, ...)` whose slot is `void *` should pin `column_bool`'s stmt to `void *`, but the pass walked past the cmeth table entirely.

    Mirror the @meth_* walk over each class's cmeth table. callee_slot_type extended with a real-class sibling lookup: try @meth_*, then the `cls` synthetic, then the sibling cmeth resolved via @cls_cmeth_names + cls_cmeth_ptypes_get.

  2. unify_call_types collapsed literal-int-0 + pointer-typed param to poly. C's null-pointer-constant is exactly the literal 0 spelling, so a call site `column_bool(0, 0)` against a body- pinned `void *` slot was reverting the back-propagation to poly on the next iteration. Treat IntegerNode 0 against a nullable_pointer_type slot as nullable-of-old-type, mirroring the `at == "nil"` branch above for the literal-zero spelling.

Together they let real-blog's `Db.column_bool(stmt, idx) != 0` shape pin stmt to `void *` from body evidence alone — the issue reproducer compiles cleanly with -Werror=int-conversion.

Closes #453

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

Details

Commit
2439f673e5cc
Parent
158d35311aba

spinel_analyze.rbmodifiedView file

@@ -9631,6 +9631,18 @@
96319631 if old_pt == "float"
96329632 return "float"
96339633 end
9634+ # Literal `0` against a pointer-typed slot: C's null pointer
9635+ # constant. The literal narrows nothing; preserve the existing
9636+ # pointer type as nullable instead of collapsing to poly.
9637+ # Mirrors the `at == "nil"` branch above for the integer-zero
9638+ # spelling Ruby callers reach for when passing "no value" to
9639+ # an FFI / pointer slot.
9640+ if arg_is_literal == 1 && arg_id >= 0 && @nd_type[arg_id] == "IntegerNode" && @nd_value[arg_id].to_i == 0 && is_nullable_pointer_type(old_pt) == 1
9641+ if is_nullable_type(old_pt) == 1
9642+ return old_pt
9643+ end
9644+ return old_pt + "?"
9645+ end
96349646 # Literal int into a non-numeric concrete type: genuine poly.
96359647 if arg_is_literal == 1
96369648 @needs_rb_value = 1
@@ -10996,6 +11008,73 @@
1099611008 end
1099711009 mi = mi + 1
1099811010 end
11011+ # Real-class cmeths live in @cls_cmeth_*, not @meth_*. A
11012+ # `def self.column_bool(stmt, idx)` inside `class Db` whose
11013+ # body forwards `stmt` to a `void *`-slotted callee needs the
11014+ # same back-propagation as the module-class case. Mirror the
11015+ # @meth_* walk over each registered class's cmeth table.
11016+ cci_cs = 0
11017+ while cci_cs < @cls_names.length
11018+ cmnames_cs = @cls_cmeth_names[cci_cs].split(";")
11019+ cmbodies_cs = @cls_cmeth_bodies[cci_cs].split(";")
11020+ cls_changed_cs = 0
11021+ cmj_cs = 0
11022+ while cmj_cs < cmnames_cs.length
11023+ bid_cm_cs = -1
11024+ if cmj_cs < cmbodies_cs.length
11025+ bid_cm_cs = cmbodies_cs[cmj_cs].to_i
11026+ end
11027+ if bid_cm_cs >= 0
11028+ pnames_cm_cs = cls_cmeth_pnames_get(cci_cs, cmj_cs)
11029+ ptypes_cm_cs = cls_cmeth_ptypes_get(cci_cs, cmj_cs)
11030+ m_changed_cs = 0
11031+ saved_scope_cm_cs = @current_lexical_scope
11032+ @current_lexical_scope = @cls_names[cci_cs]
11033+ pk_cs = 0
11034+ while pk_cs < pnames_cm_cs.length
11035+ if pk_cs < ptypes_cm_cs.length && ptypes_cm_cs[pk_cs] == "int"
11036+ obs_cs = "".split(",")
11037+ collect_param_callee_slots(bid_cm_cs, pnames_cm_cs[pk_cs], obs_cs)
11038+ agreed_cs = ""
11039+ disagree_cs = 0
11040+ kk_cs = 0
11041+ while kk_cs < obs_cs.length
11042+ tab_cs = obs_cs[kk_cs].index("\t")
11043+ if tab_cs >= 0
11044+ cn_cs = obs_cs[kk_cs][0, tab_cs]
11045+ pos_s_cs = obs_cs[kk_cs][tab_cs + 1, obs_cs[kk_cs].length - tab_cs - 1]
11046+ pos_cs = pos_s_cs.to_i
11047+ ct_cs = callee_slot_type(cn_cs, pos_cs)
11048+ if ct_cs == "ptr" || is_obj_type(ct_cs) == 1
11049+ if agreed_cs == ""
11050+ agreed_cs = ct_cs
11051+ elsif agreed_cs != ct_cs
11052+ disagree_cs = 1
11053+ end
11054+ end
11055+ end
11056+ kk_cs = kk_cs + 1
11057+ end
11058+ if agreed_cs != "" && disagree_cs == 0
11059+ ptypes_cm_cs[pk_cs] = agreed_cs
11060+ m_changed_cs = 1
11061+ end
11062+ end
11063+ pk_cs = pk_cs + 1
11064+ end
11065+ @current_lexical_scope = saved_scope_cm_cs
11066+ if m_changed_cs == 1
11067+ cls_cmeth_ptypes_put(cci_cs, cmj_cs, ptypes_cm_cs)
11068+ cls_changed_cs = 1
11069+ end
11070+ end
11071+ cmj_cs = cmj_cs + 1
11072+ end
11073+ if cls_changed_cs == 1
11074+ @cls_cmeth_ptypes_version = @cls_cmeth_ptypes_version + 1
11075+ end
11076+ cci_cs = cci_cs + 1
11077+ end
1099911078 end
1100011079
1100111080 # Resolve `callee_name` (a bare-call mname or a `<Mod>_cls_<m>`
@@ -11022,6 +11101,24 @@
1102211101 return spts[pos]
1102311102 end
1102411103 end
11104+ # Real-class sibling cmeth: stored in @cls_cmeth_* (not the
11105+ # synthetic top-level @meth_*). A bare call from one
11106+ # `def self.foo` to a peer `def self.bar` inside `class X`
11107+ # resolves here.
11108+ sci = find_class_idx(@current_lexical_scope)
11109+ if sci >= 0
11110+ sc_cmnames = @cls_cmeth_names[sci].split(";")
11111+ smj = 0
11112+ while smj < sc_cmnames.length
11113+ if sc_cmnames[smj] == callee_name
11114+ sc_pts = cls_cmeth_ptypes_get(sci, smj)
11115+ if pos < sc_pts.length
11116+ return sc_pts[pos]
11117+ end
11118+ end
11119+ smj = smj + 1
11120+ end
11121+ end
1102511122 end
1102611123 ""
1102711124 end