Commit c3e7e0a8

c3e7e0a8d3ffMerged

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

Description

fix(analyze): poly-recv narrowing recognizes module-scoped Class.new

Sibling to #513 (af55659). walk_call_sites_for_class_method, the global AST walker that powers recover_concrete_classes_for_ivar's poison-vs-narrow decision, matched only ConstantReadNode receivers (Cname.new(...)). Module-scoped classes whose call sites are Mod::Cname.new(...) parse as ConstantPathNode receivers and silently fell through to the not-matched branch, so the recovery walk returned empty / "?" and the poly-recv widening fell back to all-classes. Server got pulled into M::Holder.use's cls_id dispatch table even though no M::Server was ever assigned to @w, and M::Server#run's port widened back to sp_RbVal.

Add a ConstantPathNode arm in the receiver match: flatten via const_ref_flat_name (which uses the same Mod_Class shape that @cls_names stores) and compare against the target class's flattened name. M::Holder.new now matches when ci is the M_Holder entry; the same applies to any module-scoped class targeted by the recovery walk.

The repro is structurally identical to #513's top-level case, just wrapped in module M; after the fix M::Server#run keeps mrb_int lv_port and the dispatch table at the M::Holder.use emit drops to two arms (cls_id 21 + 22) -- same C shape the #513 test fixture verifies for the top-level form.

Closes #531

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

Details

Commit
c3e7e0a8d3ff
Parent
70cbad41262b

spinel_analyze.rbmodifiedView file

@@ -22060,7 +22060,24 @@
2206022060 recv_cs = @nd_receiver[nid]
2206122061 cs_mname = @nd_name[nid]
2206222062 match = 0
22063- if recv_cs >= 0 && @nd_type[recv_cs] == "ConstantReadNode" && @nd_name[recv_cs] == cname
22063+ # Direct `Cname.new(...)` (top-level class) vs module-scoped
22064+ # `Mod::Cname.new(...)`. cname is the flattened storage form
22065+ # (e.g. `M_Holder` for `module M; class Holder`), so the
22066+ # ConstantPathNode receiver has to be flattened the same way
22067+ # to compare. Without this arm the walker bailed on any
22068+ # module-scoped class and the poly-recv recovery (af55659)
22069+ # fell back to all-classes -- M::Server got pulled into the
22070+ # M::Holder.use dispatch table even though no M::Server is
22071+ # ever assigned to @w. Issue #531, sibling to #513.
22072+ recv_match = 0
22073+ if recv_cs >= 0
22074+ if @nd_type[recv_cs] == "ConstantReadNode" && @nd_name[recv_cs] == cname
22075+ recv_match = 1
22076+ elsif @nd_type[recv_cs] == "ConstantPathNode" && const_ref_flat_name(recv_cs) == cname
22077+ recv_match = 1
22078+ end
22079+ end
22080+ if recv_match == 1
2206422081 if want_new && cs_mname == "new"
2206522082 match = 1
2206622083 elsif !want_new && cs_mname == meth_name