Commit dde7f653

dde7f6533576Merged

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

Description

docs(codegen): rewrite issue-ref comments forward-looking (1/2)

Strip Issue #N: lead-ins from comments throughout spinel_codegen.rb and rewrite incident-shaped descriptions ("Without this, gcc errored", "Pre-fix this slot was ...") as forward-looking statements about what the code does. Issue numbers serve as poor entry points for readers after the incident is no longer fresh; the git log retains the trail for anyone who needs the original context.

This commit covers ~50 of ~110 references — the first half of the file. A follow-up will finish the rest. Tests + bootstrap confirmed clean between batches.

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

Details

Commit
dde7f6533576
Parent
4f61205bc61c

spinel_codegen.rbmodifiedView file

@@ -132,26 +132,25 @@
132132 @cls_parents = "".split(",")
133133 @cls_ivar_names = "".split(",")
134134 @cls_ivar_types = "".split(",")
135- # Issue #130: per-ivar flag — was the ivar's first scanned write a
136- # definite-literal expression (IntegerNode, FloatNode, StringNode, etc.)?
137- # `infer_ivar_init_type` falls back to "int" for non-recognized
138- # CallNodes / LocalVariableReadNodes, so trusting the inferred type
139- # alone produces false widening when the codegen widens to poly on
140- # disagreement. The flag distinguishes "concrete literal write" from
141- # "best-guess inference"; only when both old and new writes are
142- # definite-literal do we widen to poly on disagreement.
135+ # Per-ivar flag: was the ivar's first scanned write a definite
136+ # literal (IntegerNode / FloatNode / StringNode / ...)? Used to
137+ # distinguish concrete-literal writes from best-guess inference
138+ # so type unification only widens to poly when both writes are
139+ # definite — non-recognized CallNodes default to "int" through
140+ # infer_ivar_init_type and a naive trust of that produces
141+ # spurious disagreement.
143142 @cls_ivar_init_definite = "".split(",")
144- # Issue #247: per-(class, ivar) accumulator of distinct concrete
145- # writer types observed by scan_writer_calls. After all writer-scan
146- # iterations finish, if the list contains 2+ distinct entries the
147- # slot is widened to poly. Observations are recorded with the scope
148- # active inside scan_writer_calls (params declared with their
149- # iteratively-widened ptypes), so `value` in
150- # `def write_any(value); @id = value` resolves to its caller-pinned
151- # string type rather than the placeholder "int" it would default to
152- # outside that scope. Each entry is a comma-separated list of
153- # distinct types per ivar; the outer dimension is semicolon-
154- # separated and parallel to `@cls_ivar_names[ci]`.
143+ # Per-(class, ivar) accumulator of distinct concrete writer
144+ # types observed by scan_writer_calls. After all writer-scan
145+ # iterations finish, slots with 2+ distinct entries widen to
146+ # poly. Observations are recorded with the scope active inside
147+ # scan_writer_calls (params declared with their iteratively-
148+ # widened ptypes), so e.g. `value` in `def write_any(value);
149+ # @id = value` resolves to its caller-pinned type rather than
150+ # the placeholder "int" outside that scope. Each entry is a
151+ # comma-separated list of distinct types per ivar; the outer
152+ # dimension is semicolon-separated and parallel to
153+ # `@cls_ivar_names[ci]`.
155154 @cls_ivar_observed_types = "".split(",")
156155 # Memoization for `find_lv_ivar_alias_in_ast`. Keyed by
157156 # `"<class_idx>:<lv_name>"`, value is the resolved ivar name (or
@@ -249,12 +248,12 @@
249248 # such alias (or had a non-ivar write since).
250249 @scope_ivar_alias = "".split(",")
251250
252- # Issue #207: type narrowing introduced by `is_a?`/`kind_of?`
253- # guards. While walking the then-arm of `if v.is_a?(Hash)` or
254- # the truthy branch of `v.is_a?(Hash) ? a : b`, we push the
255- # narrowed `(var_name, narrowed_type)` here; find_var_type's
256- # top-down lookup picks it up and infer_type / scan / codegen
257- # see the narrowed type without per-pass plumbing.
251+ # Type-narrow stack for `is_a?`/`kind_of?` guards. While walking
252+ # the then-arm of `if v.is_a?(Hash)` or the truthy branch of
253+ # `v.is_a?(Hash) ? a : b`, the narrowed `(var_name,
254+ # narrowed_type)` is pushed here; find_var_type's top-down
255+ # lookup picks it up so infer_type / scan / codegen see the
256+ # narrowed type without per-pass plumbing.
258257 @type_narrow_names = "".split(",")
259258 @type_narrow_types = "".split(",")
260259
@@ -267,9 +266,9 @@
267266 # binding (instance method, constructor synthesis). 0 for
268267 # class methods, module class methods, and top-level free
269268 # functions. Drives the bare-return-with-obj_<C>-return shape
270- # in compile_return_stmt; without it a class method whose
271- # inferred return is obj_<C> emits `return self;` and gcc
272- # complains about an undeclared identifier.
269+ # in compile_return_stmt: only when has_self == 1 does a bare
270+ # `return` whose function returns obj_<C> lower to `return
271+ # self;`; otherwise it emits the type's default value.
273272 @current_method_has_self = 0
274273 @in_main = 0
275274 @in_loop = 0
@@ -1390,9 +1389,9 @@
13901389 end
13911390
13921391 # ---- is_a? type narrowing ----
1393- # Issue #207: `<expr>.is_a?(<Class>)` (or kind_of?) used as the
1394- # predicate of an if / ternary lets us treat <expr> as <Class>'s
1395- # type inside the then-arm. The four entry points (push, pop,
1392+ # `<expr>.is_a?(<Class>)` (or kind_of?) used as the predicate of
1393+ # an if / ternary lets us treat <expr> as <Class>'s type inside
1394+ # the then-arm. The four entry points (push, pop,
13961395 # narrow_type_for_class, parse_is_a_predicate) are called from
13971396 # both inference and codegen sides — keep the API tiny so the
13981397 # narrow context can be plumbed through without leaking state.
@@ -1458,7 +1457,7 @@
14581457 ""
14591458 end
14601459
1461- # Issue #207: static evaluation of `<expr>.is_a?(<Class>)` /
1460+ # Static evaluation of `<expr>.is_a?(<Class>)` /
14621461 # `.kind_of?(<Class>)` when expr's static type already proves the
14631462 # answer. Returns "TRUE" / "FALSE" / "" (= dynamic). Used at
14641463 # IfNode emit sites to skip the dead arm so the C compiler doesn't
@@ -2084,11 +2083,10 @@
20842083 -1
20852084 end
20862085
2087- # Get method return type from class
2088- # Issue #208: walk the parent chain looking for a class method
2086+ # Walk the parent chain looking for a class method
20892087 # (`def self.<mname>`) named `mname`. Returns the class index that
2090- # defines it, or -1 if not found. Used so `Leaf.all` resolves to
2091- # `Base.all` (and emits `sp_Base_cls_all(...)`) when Leaf inherits
2088+ # defines it, or -1 if not found. Lets `Leaf.all` resolve to
2089+ # `Base.all` (and emit `sp_Base_cls_all(...)`) when Leaf inherits
20922090 # from Base without overriding `.all`. Mirrors cls_method_return's
20932091 # parent walk for instance methods.
20942092 def cls_cmethod_owner(ci, mname)
@@ -2435,13 +2433,14 @@
24352433 if @current_class_idx >= 0
24362434 return cls_ivar_type(@current_class_idx, @nd_name[nid])
24372435 end
2438- # Issue #303: inside a module class method (`def self.foo` in
2439- # `module M`, compiled as the top-level `M_cls_foo`), an ivar
2440- # read like `@slots` resolves to `cst_M_slots` — already handled
2441- # by compile_expr's matching arm. Mirror that resolution here so
2442- # infer_type returns the slot's recorded type instead of falling
2443- # through to "int", which would route `@slots[k]` through the
2444- # int-bit-extract codegen even though the storage is a hash.
2436+ # Inside a module class method (`def self.foo` in `module M`,
2437+ # compiled as the top-level `M_cls_foo`), an ivar read like
2438+ # `@slots` resolves to `cst_M_slots` — already handled by
2439+ # compile_expr's matching arm. Mirror that resolution here so
2440+ # infer_type returns the slot's recorded hash/array type
2441+ # instead of the "int" default, which would otherwise route
2442+ # `@slots[k]` through the int-bit-extract codegen even though
2443+ # the storage is a hash.
24452444 mi3 = 0
24462445 while mi3 < @module_names.length
24472446 mmod = @module_names[mi3]
@@ -2864,11 +2863,12 @@
28642863 if all_sym_keys == 1 && (first_vt == "int" || first_vt == "bool" || first_vt == "nil")
28652864 return "sym_int_hash"
28662865 end
2867- # Issue #287: every value already inferred as poly (the slot
2868- # was widened upstream — typically an ivar that #247's
2866+ # Every value already inferred as poly (the slot was
2867+ # widened upstream — typically an ivar that
28692868 # finalize_ivar_heterogeneity widened on a sibling-writer
2870- # disagreement). Same poly-hash storage as the mixed-types
2871- # `else` branch — every value carries its own tag.
2869+ # disagreement). Use the same poly-hash storage as the
2870+ # mixed-types `else` branch — every value carries its
2871+ # own tag.
28722872 if first_vt == "poly"
28732873 if all_sym_keys == 1
28742874 return "sym_poly_hash"
@@ -2963,10 +2963,10 @@
29632963 end
29642964 end
29652965
2966- # Issue #126: chain return type for `Module.accessor.<method>`.
2967- # All resolved candidates' class methods should agree on a return
2968- # type; if they disagree the chain becomes poly. Returning early
2969- # only when we have a confident answer means the existing
2966+ # Chain return type for `Module.accessor.<method>`. All resolved
2967+ # candidates' class methods should agree on a return type; if
2968+ # they disagree the chain becomes poly. Returning early only
2969+ # when we have a confident answer means the existing
29702970 # operator/comparison/etc paths still get to chime in for shapes
29712971 # that don't match this chain.
29722972 if recv >= 0 && @nd_type[recv] == "CallNode"
@@ -3473,18 +3473,15 @@
34733473 # used to live next to nan?/infinite? — folded in here for one place
34743474 # to update.)
34753475 #
3476- # Issue #314: gate on a Float receiver. `ViewHelpers.truncate(s,
3477- # length: 100)` is a user-defined module method that returns a
3478- # string; without this gate it matched here purely by name and
3479- # returned "float", so the call site boxed truncate's `const char *`
3480- # return as `sp_box_float(...)` — invalid C.
3481- #
3482- # The gate is `rt == "float"` exclusively, not "int": a module
3483- # ConstantReadNode's infer_type falls back to "int" by default,
3484- # and we don't want module method calls to slide through. Integer
3485- # ceil/floor/round/truncate with an arg are rare in practice — if
3486- # the codebase needs them later, a future pass on call-site type
3487- # narrowing can claim it more carefully.
3476+ # Gate on a Float receiver only — `rt == "float"` exclusively,
3477+ # not "int". A user-defined module method like
3478+ # `ViewHelpers.truncate(s, length: 100)` reaches infer_type
3479+ # with the module's ConstantReadNode receiver, which falls
3480+ # back to "int", so a permissive gate would match purely by
3481+ # name and emit `sp_box_float(...)` against a `const char *`
3482+ # return value. Integer ceil/floor/round/truncate-with-arg are
3483+ # rare in practice; if the codebase needs them later, a
3484+ # future call-site-type-narrowing pass can claim them.
34883485 if mname == "ceil" || mname == "floor" || mname == "round" || mname == "truncate"
34893486 if recv >= 0 && infer_type(recv) == "float"
34903487 if @nd_arguments[nid] >= 0
@@ -3732,9 +3729,9 @@
37323729 return "string"
37333730 end
37343731 end
3735- # Issue #204: don't claim "int" for fetch on receivers we don't
3736- # recognize as a built-in collection — let later dispatch resolve
3737- # a user-defined `def fetch` against the receiver class.
3732+ # Don't claim "int" for fetch on receivers we don't recognize
3733+ # as a built-in collection — let later dispatch resolve a
3734+ # user-defined `def fetch` against the receiver class.
37383735 return ""
37393736 end
37403737 if mname == "dig"
@@ -3888,10 +3885,10 @@
38883885 return "string"
38893886 end
38903887 end
3891- # Issue #204: same as fetch above — fall through so a user-
3892- # defined `def find` (canonical ActiveRecord finder shape) wins
3893- # over the built-in collection dispatch when the receiver isn't
3894- # a built-in collection.
3888+ # Same fall-through logic as fetch above: a user-defined
3889+ # `def find` (the canonical ActiveRecord finder shape) wins
3890+ # over the built-in collection dispatch when the receiver
3891+ # isn't a recognized built-in collection.
38953892 return ""
38963893 end
38973894 if mname == "keys"
@@ -4337,12 +4334,11 @@
43374334 end
43384335 if mname == "[]"
43394336 if recv >= 0
4340- # Issue #129: ENV["X"] dispatches to sp_str_dup_external(getenv(...))
4341- # which returns const char *. Without this early check, ConstantReadNode
4342- # for "ENV" infers as int (the default for unknown constants), the
4343- # receiver-type dispatch below misses every branch, and the function
4344- # returns int — making `infer_constant_recv_type`'s ENV branch (3019)
4345- # unreachable for `[]`. Mirrors the dispatch site's ENV check.
4337+ # ENV["X"] returns `const char *` (sp_str_dup_external of
4338+ # getenv). The plain receiver-type dispatch below would
4339+ # leave ENV at the default "int" (unknown constant) and
4340+ # miss every branch, so claim string here directly.
4341+ # Mirrors the codegen site's ENV check.
43464342 if @nd_type[recv] == "ConstantReadNode" && @nd_name[recv] == "ENV"
43474343 return "string"
43484344 end
@@ -4468,10 +4464,10 @@
44684464 if rt == "lambda"
44694465 return "lambda"
44704466 end
4471- # Issue #219: user-defined `def [](k)` on an obj recv. Walk
4472- # the class's method table the same way infer_recv_method_type
4473- # does for arbitrary mname — without this, the fallback at the
4474- # tail of this branch returns "int" and downstream `.to_i` /
4467+ # User-defined `def [](k)` on an obj recv. Walk the class's
4468+ # method table the same way infer_recv_method_type does for
4469+ # arbitrary mname; otherwise the fallback at the tail of
4470+ # this branch returns "int" and downstream `.to_i` /
44754471 # `.length` etc. dispatch on the wrong recv type.
44764472 if is_obj_type(rt) == 1
44774473 bt = base_type(rt)
@@ -4506,9 +4502,9 @@
45064502
45074503 def infer_constructor_type(nid, mname, recv)
45084504 if mname == "new"
4509- # Issue #207: implicit recv-less `new` in a class method body
4510- # resolves to obj_<CurrentClass> so subsequent attr_writer
4511- # calls and ivar widening see the right type.
4505+ # Implicit recv-less `new` in a class method body resolves
4506+ # to `obj_<CurrentClass>` so subsequent attr_writer calls
4507+ # and ivar widening see the right type.
45124508 if recv < 0
45134509 implicit = current_class_method_owning_class
45144510 if implicit != ""
@@ -4599,12 +4595,9 @@
45994595 return "str_int_hash"
46004596 end
46014597 if rn == "String"
4602- # Issue #203: `String.new` / `String.new("...")` returns
4603- # a fresh mutable string buffer (sp_String *), the same
4604- # type that `s = ""; s << ...` widens a string local
4605- # into. Without this branch, the receiver dispatch
4606- # treated `String` as an unknown obj-class and the call
4607- # warned-and-emitted-0.
4598+ # `String.new` / `String.new("...")` returns a fresh
4599+ # mutable string buffer (sp_String *), the same type
4600+ # that `s = ""; s << ...` widens a string local into.
46084601 return "mutable_str"
46094602 end
46104603 if rn == "Proc"
@@ -4706,7 +4699,7 @@
47064699 end
47074700 end
47084701 end
4709- # Issue #208: walk the parent chain so an inherited
4702+ # Walk the parent chain so an inherited
47104703 # `def self.<mname>` on a base class resolves correctly
47114704 # when called on the subclass (e.g. `Leaf.all` →
47124705 # `Base.all`'s return type).
@@ -4715,12 +4708,11 @@
47154708 return inherited_rt
47164709 end
47174710 end
4718- # Issue #127: same lookup for module class methods. They live in
4719- # the top-level @meth_* table as `<Mod>_cls_<method>`, not in
4720- # @cls_cmeth_* (which is class-only). Without this branch every
4721- # `Module.cls_method` call inferred as int regardless of return
4722- # type, so `s = M.greet` declared `lv_s` as mrb_int even though
4723- # `sp_M_cls_greet()` returns `const char *`.
4711+ # Same lookup for module class methods. They live in the
4712+ # top-level @meth_* table as `<Mod>_cls_<method>`, not in
4713+ # @cls_cmeth_* (which is class-only) — so `Module.cls_method`
4714+ # call sites need this branch to find the method's return
4715+ # type and assign call-site locals correctly.
47244716 if module_name_exists(rcname) == 1
47254717 mfi = find_method_idx(rcname + "_cls_" + mname)
47264718 if mfi >= 0 && mfi < @meth_return_types.length
@@ -5330,10 +5322,11 @@
53305322 0
53315323 end
53325324
5333- # Issue #58: empty `[]` literal needs deferred element-type
5334- # resolution. This helper distinguishes `[]` from `[1, 2, 3]` so the
5335- # promotion machinery can know "writes haven't fixed the element type
5336- # yet, so a later push can still pick it".
5325+ # Empty `[]` / `{}` literals need deferred element-type resolution
5326+ # — the type can only be settled by later writes. This helper
5327+ # distinguishes `[]` from `[1, 2, 3]` so the promotion machinery
5328+ # can recognize "writes haven't fixed the element type yet, so a
5329+ # later push can still pick it".
53375330 def is_empty_hash_literal(nid)
53385331 if nid < 0
53395332 return 0
@@ -6050,15 +6043,16 @@
60506043 -1
60516044 end
60526045
6053- # Issue #126: walk the AST for `Module.accessor = RHS` writes where
6046+ # Walk the AST for `Module.accessor = RHS` writes where
60546047 # (Module, accessor) was registered in `collect_module` as a
6055- # singleton accessor. Accumulate the set of distinct ConstantReadNode
6056- # RHSes; the lowering paths read this list to choose:
6048+ # singleton accessor. Accumulates the set of distinct
6049+ # ConstantReadNode RHSes; the lowering paths read this list to
6050+ # choose:
60576051 # - 0 entries: never written, falls through (un-folded)
60586052 # - 1 entry: Stage 1, inline `<resolved>.<method>` directly
60596053 # - 2+ entries: Stage 2, sentinel switch over the union
6060- # A non-constant RHS poisons the slot with a `?` sentinel marker —
6061- # the lowering paths treat that as un-folded.
6054+ # A non-constant RHS poisons the slot with a `?` sentinel marker
6055+ # so the lowering paths treat it as un-folded.
60626056 def resolve_module_singleton_accessors
60636057 if @module_acc_keys.length == 0
60646058 return
@@ -6118,7 +6112,8 @@
61186112 # Sentinel value for Stage 2 switch dispatch. Each module's index in
61196113 # `@module_names` doubles as its sentinel id; reading `Module` as a
61206114 # value lowers to this integer.
6121- # Issue #304: look up the return type of a `<class_or_module>.<mname>`
6115+
6116+ # Look up the return type of a `<class_or_module>.<mname>`
61226117 # singleton method, walking @meth_* (module / synthetic top-level
61236118 # form) and @cls_cmeth_* (in-class `def self.X`). Returns "" when
61246119 # the method isn't registered. Used by the module-dispatch ternary
@@ -6212,11 +6207,12 @@
62126207 # a fatal program error: bail with a clear message instead of letting
62136208 # the recursive helpers loop forever. Self-inheritance (`class A < A`)
62146209 # is detected as the trivial 1-step cycle.
6215- # Issue #224: copy each inherited class method (def self.<m> on a
6216- # parent class) into every subclass's @cls_cmeth_* tables so the
6210+
6211+ # Copy each inherited class method (def self.<m> on a parent
6212+ # class) into every subclass's @cls_cmeth_* tables so the
62176213 # subclass gets its own synthetic copy. Each copy reuses the
62186214 # parent's AST body id; emit_class_methods then re-compiles the
6219- # body under the subclass's @current_class_idx, and a bare `new`
6215+ # body under the subclass's @current_class_idx, so a bare `new`
62206216 # inside the body resolves to the subclass's constructor.
62216217 #
62226218 # Run after class collection so all parents are populated, and
@@ -7977,12 +7973,13 @@
79777973 end
79787974 end
79797975
7980- # Issue #247: record `at` as a distinct observation for the slot if
7981- # it's a concrete (non-fallback) type. "Concrete" means either
7982- # `at != "int"` and `at != "nil"` (those are infer_type's catch-all
7983- # placeholders), or the rhs is a definite-literal AST. The dedup
7984- # keeps the list short — a slot written with int from twenty
7985- # different `obj.length` call sites still records "int" once.
7976+ # Record `at` as a distinct observation for the (class, ivar)
7977+ # slot if it's a concrete (non-fallback) type. "Concrete" means
7978+ # either `at != "int"` and `at != "nil"` (those are infer_type's
7979+ # catch-all placeholders), or the rhs is a definite-literal AST.
7980+ # The dedup keeps the list short — a slot written with int from
7981+ # twenty different `obj.length` call sites still records "int"
7982+ # once.
79867983 def record_ivar_observation(ci, iname, at, expr_id)
79877984 is_concrete = 0
79887985 if at != "int" && at != "nil"
@@ -8036,17 +8033,17 @@
80368033 end
80378034 end
80388035
8039- # Issue #130: was the AST expression a definite-literal that
8040- # `infer_ivar_init_type` types unambiguously? Used by scan_ivars to
8041- # decide when to widen a multi-write ivar slot to poly.
8036+ # Was the AST expression a definite-literal that
8037+ # `infer_ivar_init_type` types unambiguously? Used by scan_ivars
8038+ # to decide when to widen a multi-write ivar slot to poly.
80428039 #
8043- # Issue #247: also accepts typed-hash[key] reads — when the receiver
8044- # is a hash whose value type is statically known
8045- # (str_int_hash / sym_int_hash / *_str_hash / int_str_hash), the call
8046- # always produces that value type. Letting the heterogeneity check
8047- # in finalize_ivar_heterogeneity record this writer's "int" (or
8048- # "string") as a *concrete* observation lets a sibling-method writer
8049- # of a different concrete type trigger the poly widen, instead of
8040+ # Also accepts typed-hash[key] reads — when the receiver is a
8041+ # hash whose value type is statically known (str_int_hash /
8042+ # sym_int_hash / *_str_hash / int_str_hash), the call always
8043+ # produces that value type. Recording this writer's "int" (or
8044+ # "string") as a *concrete* observation in
8045+ # finalize_ivar_heterogeneity lets a sibling-method writer of a
8046+ # different concrete type trigger the poly widen, instead of
80508047 # silently narrowing the slot to whichever sibling won the type
80518048 # race.
80528049 def is_definite_ivar_init(nid)
@@ -8069,9 +8066,10 @@
80698066 end
80708067 end
80718068 end
8072- # Issue #131: a ternary whose branches are themselves definite is
8073- # definite. Lets the #130 multi-write widening rule still fire when
8074- # a later concrete write disagrees with an IfNode-typed slot.
8069+ # A ternary whose branches are themselves definite is itself
8070+ # definite. Lets the multi-write poly-widening rule still fire
8071+ # when a later concrete write disagrees with an IfNode-typed
8072+ # slot.
80758073 if t == "IfNode"
80768074 then_d = 0
80778075 body = @nd_body[nid]
@@ -8142,14 +8140,14 @@
81428140 vtype = infer_ivar_init_type(expr_first)
81438141 add_ivar(ci, iname, vtype, is_definite_ivar_init(expr_first))
81448142 else
8145- # Issue #130: when the new write is a definite-literal AND the
8146- # ivar's first scanned write was also a definite-literal AND the
8147- # types disagree, widen to poly. The dual definite-literal gate
8148- # avoids false widening on `infer_ivar_init_type`'s "int" fallback
8149- # for non-recognized expressions (CallNodes, LocalVariableReadNodes).
8150- # Without the gate, spinel_codegen's own ivars (e.g.,
8151- # `@current_method_name = "x" + n.to_s`) would falsely widen and
8152- # break the bootstrap.
8143+ # When the new write is a definite-literal AND the ivar's
8144+ # first scanned write was also a definite-literal AND the
8145+ # types disagree, widen to poly. The dual definite-literal
8146+ # gate avoids false widening on `infer_ivar_init_type`'s
8147+ # "int" fallback for non-recognized expressions (CallNodes,
8148+ # LocalVariableReadNodes) — spinel_codegen's own ivars
8149+ # (e.g. `@current_method_name = "x" + n.to_s`) would
8150+ # otherwise widen and break the bootstrap.
81538151 expr = @nd_expression[nid]
81548152 if expr >= 0
81558153 if @nd_type[expr] != "NilNode"
@@ -8504,13 +8502,13 @@
85048502 return vt
85058503 end
85068504 end
8507- # Issue #131: ternary / if-as-expression RHS. Recurse into both
8508- # branches' last statements and unify with strict comparison.
8509- # Cannot delegate to unify_return_type here — that helper has an
8510- # "int is default/unresolved" escape hatch (`int + T → T`) which
8511- # is correct for method-return inference but exactly the
8512- # conflation that bit us in #130: mixing concrete int and
8513- # concrete non-int in a ternary needs to widen to poly, not
8505+ # Ternary / if-as-expression RHS: recurse into both branches'
8506+ # last statements and unify with strict comparison. Cannot
8507+ # delegate to unify_return_type — that helper has an "int is
8508+ # default/unresolved" escape hatch (`int + T → T`) which is
8509+ # correct for method-return inference but the wrong rule for
8510+ # ivar-write inference: mixing concrete int and concrete
8511+ # non-int in a ternary needs to widen to poly here, not
85148512 # silently pick the non-int side. nil branches still defer to
85158513 # the other type so existing nullable widening
85168514 # (string + nil → string?) flows through update_ivar_type.
@@ -8764,12 +8762,11 @@
87648762 @meth_return_types.push("int")
87658763 @meth_body_ids.push(@nd_body[sid])
87668764 @meth_has_yield.push(0)
8767- # Issue #239: capture default-arg expressions so call
8768- # sites that omit trailing args get them filled in by
8769- # compile_call_args_with_defaults. Pre-fix this slot
8770- # was hardcoded to "0", which `compile_expr` lowered to
8771- # the literal `0` regardless of the actual default
8772- # (string literals etc.).
8765+ # Capture default-arg expressions so call sites that
8766+ # omit trailing args get them filled in by
8767+ # compile_call_args_with_defaults — the actual default
8768+ # value is required (not just literal 0) for string
8769+ # default args etc.
87738770 @meth_has_defaults.push(collect_defaults_str(sid))
87748771 @meth_rest_index.push(collect_rest_index(sid))
87758772 end
@@ -8828,11 +8825,11 @@
88288825 }
88298826 end
88308827
8831- # Issue #303: refine module-ivar types for empty-container literals
8828+ # Refine module-ivar types for empty-container literals
88328829 # (`@h = {}` / `@arr = []`) by walking the module's class-method
88338830 # bodies for `@h[k] = v` / `@h << v` style writes and picking the
88348831 # most specific hash / array shape from the observed key + value
8835- # types. Without this the empty-hash default `str_int_hash` stays
8832+ # types. Otherwise the empty-hash default `str_int_hash` stays
88368833 # frozen and a sym-key / string-value write site emits a typed-
88378834 # mismatch sp_StrIntHash_set call.
88388835 #
@@ -8860,10 +8857,9 @@
88608857 # constants named `<Mod>_<iname>`)
88618858 # LOG = [] (ConstantWriteNode — registered directly as
88628859 # `<Mod>_<cname>`)
8863- # Issue #303 covered the ivar shape; #333 added the bare-
8864- # constant shape, where `LOG << some_hash` writes never
8865- # propagated back to flip the constant off the empty-array
8866- # `int_array` default.
8860+ # The bare-constant shape needs the same refinement so
8861+ # `LOG << some_hash` writes flip the constant off the
8862+ # empty-array `int_array` default.
88678863 iv_write = @nd_type[sid] == "InstanceVariableWriteNode"
88688864 const_write = @nd_type[sid] == "ConstantWriteNode"
88698865 next unless iv_write || const_write
@@ -8912,14 +8908,14 @@
89128908 declare_var(pnames3[k3], pt3)
89138909 k3 = k3 + 1
89148910 end
8915- # Issue #333: declare body locals too. Without this, a
8916- # `LOG << entry` write where `entry = { ... }` was assigned
8917- # earlier in the body has the LocalVariableReadNode read on
8918- # `entry` resolve via infer_type's "int" default — and
8919- # val_t_set ends up `["int"]` so pick_array_class returns
8920- # the same int_array we started with. The ivar shape
8921- # (`@slots[k] = v` where k/v are params) didn't need this
8922- # because params were already declared above.
8911+ # Declare body locals too: a `LOG << entry` write where
8912+ # `entry = { ... }` was assigned earlier in the body needs
8913+ # the LocalVariableReadNode read on `entry` to resolve
8914+ # against the local's actual type, not infer_type's "int"
8915+ # default. Without this val_t_set ends up `["int"]` and
8916+ # pick_array_class returns the same int_array we started
8917+ # with. The ivar shape (`@slots[k] = v` where k/v are
8918+ # params) only needs the params declared above.
89238919 local_names = "".split(",")
89248920 local_types = "".split(",")
89258921 scan_locals(bid, local_names, local_types, pnames3)
@@ -8991,11 +8987,11 @@
89918987 end
89928988
89938989 # Walk `nid` accumulating distinct key + value types observed at
8994- # `@<iname>[k] = v` / `@<iname> << v` style writes.
8995- # Issue #333: parallel to scan_module_ivar_writes for module-level
8996- # constants. `LOG << v` / `LOG[k] = v` / `LOG.push(v)` where LOG is
8997- # a ConstantReadNode (resolving via the enclosing module's lexical
8998- # scope) contributes its key/value types to the same set the
8990+ # `LOG << v` / `LOG[k] = v` / `LOG.push(v)` writes against a
8991+ # module-level constant `LOG`. Parallel to
8992+ # scan_module_ivar_writes for the ivar / hoisted shape; this
8993+ # variant tracks ConstantReadNode recv (resolving via the
8994+ # enclosing module's lexical scope) and feeds the same set the
89998995 # refinement uses to pick the typed-array / typed-hash shape.
90008996 def scan_module_const_writes(nid, cname, key_t_set, val_t_set)
90018997 if nid < 0
@@ -10299,22 +10295,18 @@
1029910295 end
1030010296 end
1030110297
10302- # Issue #314: widen a callee's `ptypes` array from a single call
10303- # site's argument list, correctly handling keyword args. Positional
10304- # args unify by index; a `KeywordHashNode` (kwargs) unifies each
10305- # `key: value` pair into the slot whose param-name matches the key.
10306- # Mutates `ptypes` in place; the caller is responsible for joining
10307- # the result back into the storage table (`@meth_param_types[mi]`,
10298+ # Widen a callee's `ptypes` array from a single call site's
10299+ # argument list, correctly handling keyword args. Positional args
10300+ # unify by index; a `KeywordHashNode` (kwargs) unifies each
10301+ # `key: value` pair into the slot whose param-name matches the
10302+ # key. Mutates `ptypes` in place; the caller joins the result
10303+ # back into the storage table (`@meth_param_types[mi]`,
1030810304 # `@cls_cmeth_ptypes[ci]`, etc.).
1030910305 #
10310- # Without kwargs handling, the existing positional-only loop
10311- # unifies a `KeywordHashNode` into `ptypes[0]` (because the AST
10312- # presents kwargs as a single trailing hash arg), so a callee's
10313- # required kwargs default-fall-through-to-int never widens — every
10314- # kwarg-only call site through a module class method (e.g.
10315- # `M.render(model: <obj>, label: "x")`) lands as
10316- # `sp_M_cls_render(mrb_int, mrb_int)` regardless of what the call
10317- # site actually passes.
10306+ # The kwargs branch is essential: a positional-only loop would
10307+ # unify a `KeywordHashNode` into `ptypes[0]` (the AST presents
10308+ # kwargs as a single trailing hash arg), leaving the callee's
10309+ # actual kwarg slots un-widened.
1031810310 def widen_ptypes_from_args(arg_ids, pnames, ptypes)
1031910311 pos_idx = 0
1032010312 ai = 0
@@ -10389,12 +10381,12 @@
1038910381 @current_lexical_scope = saved_scope2
1039010382 return
1039110383 end
10392- # Issue #207: IfNode (incl. ternary). When the predicate is
10384+ # IfNode (incl. ternary). When the predicate is
1039310385 # `var.is_a?(C)` / `kind_of?(C)`, push a narrow on `var` while
1039410386 # walking the then-arm so a recursive call inside the arm sees
1039510387 # the narrowed type and unify_call_types widens the callee's
10396- # param accordingly. The else-arm walks unchanged (we don't
10397- # currently model "type minus C").
10388+ # param accordingly. The else-arm walks unchanged — we don't
10389+ # currently model "type minus C" (see POLY-AS-SET.md).
1039810390 if @nd_type[nid] == "IfNode"
1039910391 pred = @nd_predicate[nid]
1040010392 if pred >= 0
@@ -10506,14 +10498,14 @@
1050610498 @meth_param_types[mi] = ptypes.join(",")
1050710499 end
1050810500 end
10509- # Issue #286: bare call inside a class method body that
10510- # resolves to an inherited instance method. find_method_idx
10511- # above only finds *top-level* methods; an `assert_not_nil(x)`
10512- # inside `T2 < T`'s body needs to widen T's `assert_not_nil`
10513- # ptypes from this call's args. Mirror the obj.method() walk
10514- # in the recv >= 0 branch below — find_method_owner finds the
10515- # ancestor that actually defines the method, then we update
10516- # *that* class's @cls_meth_ptypes.
10501+ # Bare call inside a class method body that resolves to an
10502+ # inherited instance method. find_method_idx above only
10503+ # finds *top-level* methods; an `assert_not_nil(x)` inside
10504+ # `T2 < T`'s body needs to widen T's `assert_not_nil`
10505+ # ptypes from this call's args. Mirror the obj.method()
10506+ # walk in the recv >= 0 branch below — find_method_owner
10507+ # finds the ancestor that actually defines the method, then
10508+ # we update *that* class's @cls_meth_ptypes.
1051710509 if @current_class_idx >= 0
1051810510 cls_ci_286 = @current_class_idx
1051910511 cls_owner_286 = cls_ci_286
@@ -10671,18 +10663,16 @@
1067110663 end
1067210664 end
1067310665 end
10674- # Issue #341: when the receiver is poly (e.g. a method param
10675- # widened to accept multiple user classes), the per-class
10676- # arms emitted by compile_poly_method_call call each class's
10677- # `<mname>` C function with the same arg expressions. If
10678- # callers pass a String key but the class methods' params
10679- # are still at the default `int`, the arms produce
10680- # `Wint-conversion` warnings or hard `incompatible pointer
10681- # to integer conversion` errors (sp_A__aref(sp_A *, mrb_int)
10682- # called with const char *). Walk every user class that
10683- # defines mname and unify its ptypes with the call site's
10684- # arg types; unify_call_types collapses to `poly` if any
10685- # other call site disagrees.
10666+ # Poly-receiver widening: when the receiver is poly (e.g. a
10667+ # method param widened to accept multiple user classes),
10668+ # the per-class arms emitted by compile_poly_method_call
10669+ # call each class's `<mname>` C function with the same arg
10670+ # expressions. Walk every user class that defines mname and
10671+ # unify its ptypes with the call site's arg types so the
10672+ # arm signatures match — otherwise a String key passed to a
10673+ # default-`int`-typed param produces a Wint-conversion or
10674+ # incompatible-pointer error. unify_call_types collapses to
10675+ # `poly` if another call site disagrees.
1068610676 if rt == "poly"
1068710677 args_id_p = @nd_arguments[nid]
1068810678 if args_id_p >= 0
@@ -10711,10 +10701,10 @@
1071110701 end
1071210702 end
1071310703 end
10714- # Issue #207: `<Class>.cls_method(args)` — widen class
10715- # method parameter types from call-site argument types.
10716- # Same shape as the receiver-method unify above but
10717- # operating on @cls_cmeth_ptypes for class-constant recvs.
10704+ # `<Class>.cls_method(args)` — widen class method parameter
10705+ # types from call-site argument types. Same shape as the
10706+ # receiver-method unify above but operating on
10707+ # @cls_cmeth_ptypes for class-constant recvs.
1071810708 if @nd_type[nid] == "CallNode" && @nd_receiver[nid] >= 0
1071910709 crecv = @nd_receiver[nid]
1072010710 if @nd_type[crecv] == "ConstantReadNode" || @nd_type[crecv] == "ConstantPathNode"
@@ -10747,16 +10737,15 @@
1074710737 cmidx = cmidx + 1
1074810738 end
1074910739 end
10750- # Issue #239: module class methods (`module M; def
10751- # self.greet(...); end; end`) live in the top-level
10752- # `@meth_*` tables under the synthetic name
10753- # `<Mod>_cls_<m>`, not in `@cls_cmeth_*`. The widening
10754- # branch above doesn't reach them because `find_class_idx`
10755- # returns -1 for module names. Add a parallel branch
10756- # that widens `@meth_param_types` for the prefixed name
10757- # so a `M.greet("a")` call site teaches the synthetic
10758- # function to accept `const char *` instead of the
10759- # default `mrb_int`.
10740+ # Module class methods (`module M; def self.greet(...);
10741+ # end; end`) live in the top-level `@meth_*` tables
10742+ # under the synthetic name `<Mod>_cls_<m>`, not in
10743+ # `@cls_cmeth_*` — `find_class_idx` returns -1 for
10744+ # module names so the widening branch above misses
10745+ # them. Parallel branch widens `@meth_param_types` for
10746+ # the prefixed name so a `M.greet("a")` call site
10747+ # teaches the synthetic function to accept the actual
10748+ # arg type instead of the default `mrb_int`.
1076010749 if module_name_exists(rcname) == 1
1076110750 mfn239 = rcname + "_cls_" + @nd_name[nid]
1076210751 mi239 = find_method_idx(mfn239)
@@ -10774,14 +10763,14 @@
1077410763 end
1077510764 end
1077610765 end
10777- # Issue #304: module-dispatch ternary call sites
10778- # (`Disp.adapter.method(args)` where Disp.adapter resolves to N
10779- # candidate classes via the module-singleton-accessor table).
10780- # The dispatch ternary calls every candidate's class method, so
10781- # all of them need the args' types unified into their per-class
10782- # @cls_cmeth_ptypes entry. Without this the ternary arms type-
10783- # mismatch when the caller passes a poly value but the targets'
10784- # params are still mrb_int.
10766+ # Module-dispatch ternary call sites
10767+ # (`Disp.adapter.method(args)` where Disp.adapter resolves to
10768+ # N candidate classes via the module-singleton-accessor
10769+ # table). The dispatch ternary calls every candidate's class
10770+ # method, so all of them need the args' types unified into
10771+ # their per-class @cls_cmeth_ptypes entry — otherwise the
10772+ # ternary arms type-mismatch when the caller passes a poly
10773+ # value but the targets' params are still mrb_int.
1078510774 if @nd_type[nid] == "CallNode" && @nd_receiver[nid] >= 0
1078610775 outer_recv_304 = @nd_receiver[nid]
1078710776 if @nd_type[outer_recv_304] == "CallNode"
@@ -10820,12 +10809,13 @@
1082010809 cmidx_304 = cmidx_304 + 1
1082110810 end
1082210811 end
10823- # Issue #314 follow-up: when the candidate is a
10824- # module (not a class), its `def self.method` is
10825- # stored in `@meth_*` as `<Mod>_cls_<m>` rather
10826- # than in `@cls_cmeth_*`. Mirror #239's parallel
10827- # branch so a `Mod.accessor.method(args)` call site
10828- # widens the synthetic top-level function's params.
10812+ # When the candidate is a module (not a class),
10813+ # its `def self.method` is stored in `@meth_*`
10814+ # as `<Mod>_cls_<m>` rather than in
10815+ # `@cls_cmeth_*`. Mirrors the module-class-
10816+ # method widening branch above so a
10817+ # `Mod.accessor.method(args)` call site widens
10818+ # the synthetic top-level function's params.
1082910819 if module_name_exists(cn_304) == 1
1083010820 mfn_304m = cn_304 + "_cls_" + outer_mname_304
1083110821 mi_304m = find_method_idx(mfn_304m)
@@ -10911,23 +10901,22 @@
1091110901 end
1091210902 end
1091310903
10914- # Issue #224: scan_new_calls' `new` handler only widens an
10915- # `initialize`'s ptypes when the call has an explicit class
10916- # receiver (`Article.new(attrs)`). The bare form `new(attrs)`
10917- # inside a class method body has no receiver, so the ptypes
10918- # never propagate — leaving e.g. `sp_Article_new(mrb_int)`
10919- # called with `sp_SymStrHash *` and a C-level type mismatch.
10904+ # Bare `new(args)` inside a class method body widens the
10905+ # subclass's `initialize` ptypes from this call's args.
10906+ # scan_new_calls' generic `new` handler only fires when the
10907+ # receiver is an explicit class constant (`Article.new(attrs)`);
10908+ # the bare form has no receiver and would otherwise leave
10909+ # `sp_Article_new(mrb_int)` called with `sp_SymStrHash *`.
1092010910 #
10921- # propagate_inherited_class_methods has already given each
10922- # subclass its own copy of the inherited cls method, with
10923- # @cls_cmeth_ptypes widened from explicit call sites by issue
10924- # #207's logic at scan_new_calls' tail. This pass walks each
10925- # cls method body for bare `new(args)` and unifies the matching
10926- # `find_init_class(ci).initialize` ptypes — args that read a
10927- # cls-method local resolve via the cls method's pnames/ptypes
10928- # (which carry the call-site type), other args fall through
10929- # to infer_type with @current_class_idx pinned so @ivar refs
10930- # in the args resolve against the right class.
10911+ # By the time this runs, propagate_inherited_class_methods has
10912+ # already given each subclass its own copy of the inherited
10913+ # cls method, with @cls_cmeth_ptypes widened from explicit
10914+ # call sites. Walking each cls method body for bare `new(args)`
10915+ # and unifying the matching `find_init_class(ci).initialize`
10916+ # ptypes fills the gap. Args that read a cls-method local
10917+ # resolve via the cls method's pnames/ptypes; other args fall
10918+ # through to infer_type with @current_class_idx pinned so
10919+ # @ivar refs in the args resolve against the right class.
1093110920 def propagate_bare_new_to_subclass_initialize
1093210921 ci = 0
1093310922 while ci < @cls_names.length
@@ -11315,14 +11304,13 @@
1131511304 if @nd_receiver[nid] >= 0
1131611305 collect_param_methods(@nd_receiver[nid], pname, acc)
1131711306 end
11318- # Issue #314 (A): an IfNode's predicate / else-branch and a
11319- # CaseNode's predicate / when-conditions used to be invisible to
11320- # this walk. `def update(p); if p.title.nil?; ...; else self.title
11321- # = p.title; end; end` had `p.title` only inside the predicate +
11322- # else, so collect returned []; body-side type inference then
11323- # left `p` at "int" and the int-class fallback (or its companion
11324- # in infer_recv_method_type) silently picked an arbitrary user
11325- # class.
11307+ # Walk into IfNode's predicate / else-branch and CaseNode's
11308+ # predicate / when-conditions. Without these arms, `def update(p);
11309+ # if p.title.nil?; ...; else self.title = p.title; end; end`
11310+ # has `p.title` only inside the predicate + else — collect
11311+ # would return [] and body-side type inference would leave `p`
11312+ # at "int" while the int-class fallback silently picked an
11313+ # arbitrary user class.
1132611314 if @nd_predicate[nid] >= 0
1132711315 collect_param_methods(@nd_predicate[nid], pname, acc)
1132811316 end
@@ -11358,8 +11346,8 @@
1135811346 end
1135911347 end
1136011348
11361- # Issue #58: collect every element type seen in `pname.push(elem)`
11362- # or `pname << elem` patterns under nid. The deferred-element-type
11349+ # Collect every element type seen in `pname.push(elem)` or
11350+ # `pname << elem` patterns under nid. The deferred-element-type
1136311351 # promotion pass uses this to decide what concrete typed-array a
1136411352 # parameter should be promoted to when callers all passed empty
1136511353 # `[]` literals.
@@ -11430,14 +11418,15 @@
1143011418 end
1143111419 end
1143211420
11433- # Issue #58: promote each top-level method parameter from int_array
11434- # to a concrete typed-array (str_array, float_array, sym_array)
11435- # when (a) every caller passed an empty `[]` literal — guarded by
11436- # @meth_param_empty[mi][k] == "1" — and (b) the body's pushes on
11421+ # Promote each top-level method parameter from int_array to a
11422+ # concrete typed-array (str_array, float_array, sym_array) when
11423+ # (a) every caller passed an empty `[]` literal (guarded by
11424+ # @meth_param_empty[mi][k] == "1") and (b) the body's pushes on
1143711425 # that parameter all agree on a single concrete element type.
11438- # Without (a), a caller passing a real int_array would be silently
11439- # miscompiled. Without (b), a body that pushes mixed types should
11440- # surface as a type error rather than picking one arbitrarily.
11426+ # Both gates are required: a real-int_array caller without (a)
11427+ # would be silently miscompiled, and a mixed-element body
11428+ # without (b) should surface as a type error rather than pick
11429+ # one arbitrarily.
1144111430 def infer_param_array_type_from_body
1144211431 iter = 0
1144311432 changed = 1
@@ -11856,13 +11845,13 @@
1185611845 ""
1185711846 end
1185811847
11859- # Issue #133: block-param-aware lookup used by scan_locals's
11860- # empty-hash promotion. When `nid` reads a local variable that
11861- # scan_locals itself has just collected (e.g. a block param like
11862- # `|k, v|`), prefer the type recorded in `types[]` over
11863- # `infer_type` — the block param hasn't been `declare_var`'d in
11864- # scope yet, so a bare infer_type falls back to "int" and breaks
11865- # the promotion of the surrounding `out[k] = v` write.
11848+ # Block-param-aware lookup used by scan_locals's empty-hash
11849+ # promotion. When `nid` reads a local variable that scan_locals
11850+ # itself has just collected (e.g. a block param like `|k, v|`),
11851+ # prefer the type recorded in `types[]` over `infer_type` —
11852+ # the block param hasn't been `declare_var`'d in scope yet, so
11853+ # a bare infer_type would fall back to "int" and break the
11854+ # promotion of the surrounding `out[k] = v` write.
1186611855 def scan_locals_arg_type(nid, names, types, params)
1186711856 if nid >= 0 && @nd_type[nid] == "LocalVariableReadNode"
1186811857 lname = @nd_name[nid]
@@ -11926,22 +11915,25 @@
1192611915 return 1
1192711916 end
1192811917
11929- # Issue #266: when picking a user class for a parameter from "what
11930- # methods does the body call on it", reject sets that consist only
11931- # of methods also available on built-in container types
11918+ # When picking a user class for a parameter from "what methods
11919+ # does the body call on it", reject sets that consist only of
11920+ # methods also available on built-in container types
1193211921 # (int_array / float_array / str_array / sym_array / ptr_array).
11933- # Otherwise a user class that happens to define `def length` (with
11934- # nothing else) gets picked for any param the body calls `.length`
11935- # on — even when the actual call site passes an IntArray. The body
11936- # signal is too weak to commit; leave the param at "int" so call-
11937- # site type unification (scan_new_calls) decides.
11938- # Issue #305: is `mname` a method that's also defined on a primitive
11939- # type (String / Array / Hash / Integer) AND on at least one user
11940- # class? When both are true, an int-typed receiver shouldn't pick
11941- # the user class on the auto-cast fallback path — that's almost
11942- # certainly the wrong dispatch (a param that flowed in as mrb_int
11943- # because no upstream call site pinned it, with the user's actual
11944- # intent being the primitive method).
11922+ # A user class that happens to define `def length` (and nothing
11923+ # else) would otherwise get picked for any param the body calls
11924+ # `.length` on — even when the actual call site passes an
11925+ # IntArray. The body signal is too weak to commit; leave the
11926+ # param at "int" so call-site unification (scan_new_calls)
11927+ # decides.
11928+ #
11929+ # Specifically: is `mname` a method that's also defined on a
11930+ # primitive type (String / Array / Hash / Integer) AND on at
11931+ # least one user class? When both are true, an int-typed
11932+ # receiver shouldn't pick the user class on the auto-cast
11933+ # fallback path — that's almost certainly the wrong dispatch
11934+ # (a param that flowed in as mrb_int because no upstream call
11935+ # site pinned it, with the user's actual intent being the
11936+ # primitive method).
1194511937 def primitive_method_shared_with_user_class(mname)
1194611938 return 0 unless is_primitive_shared_method(mname) == 1
1194711939 # Cheap guard: the user must define a class with this method.
@@ -12040,13 +12032,14 @@
1204012032 m != "&" && m != "|" && m != "^" && m != "~" &&
1204112033 m != "<" && m != ">" && m != "<=" && m != ">=" && m != "<=>" &&
1204212034 m != "===" && m != "!" &&
12043- # Issue #302: methods shared across String / Hash / Integer
12044- # primitive types that the body-side inference must NOT use as
12035+ # Methods shared across String / Hash / Integer primitive
12036+ # types that the body-side inference must NOT use as
1204512037 # evidence of a user-class receiver. The Rails pattern of
12046- # `def index` / `def show` / `def create` on every controller
12047- # collides with `String#index` etc., and the canonical caller
12048- # (`s.index("[")` on a string) was being routed to whichever
12049- # user-class `index` the inference picked first.
12038+ # `def index` / `def show` / `def create` on every
12039+ # controller collides with `String#index` etc.; the
12040+ # canonical caller `s.index("[")` on a string would
12041+ # otherwise be routed to whichever user-class `index` the
12042+ # inference picked first.
1205012043 m != "index" && m != "rindex" && m != "match" && m != "match?" &&
1205112044 m != "scan" && m != "sub" && m != "gsub" && m != "tr" &&
1205212045 m != "split" && m != "chars" && m != "bytes" && m != "lines" &&
@@ -12178,8 +12171,8 @@
1217812171 end
1217912172 bj = bj + 1
1218012173 end
12181- # Issue #207: also scan class method bodies (def self.<m>) so
12182- # an attr_writer call on a freshly-`new`'d instance inside a
12174+ # Also scan class method bodies (def self.<m>) so an
12175+ # attr_writer call on a freshly-`new`'d instance inside a
1218312176 # `def self.from_raw(...)` factory widens the ivar's type.
1218412177 cm_bodies = @cls_cmeth_bodies[ci].split(";")
1218512178 cm_names = @cls_cmeth_names[ci].split(";")
@@ -12234,9 +12227,9 @@
1223412227 # Scan main-level code
1223512228 scan_writer_calls(@root_id)
1223612229 pop_scope
12237- # Issue #247: now that every writer's concrete type observation is
12238- # recorded into @cls_ivar_observed_types[ci] (deduped per slot),
12239- # widen any slot with 2+ distinct concrete types to poly. The
12230+ # Every writer's concrete type observation has been recorded
12231+ # into @cls_ivar_observed_types[ci] (deduped per slot); widen
12232+ # any slot with 2+ distinct concrete types to poly. The
1224012233 # narrow-then-overwrite path in update_ivar_type can otherwise
1224112234 # leave the slot pinned to whichever writer's update_ivar_type
1224212235 # call ran last, causing the loser's emit site to type-mismatch.
@@ -12333,12 +12326,12 @@
1233312326 # widen the promoted type to poly on the next iteration.
1233412327 if is_empty_hash_literal(bottom) == 0 && is_empty_array_literal(bottom) == 0
1233512328 at = infer_type(bottom)
12336- # Issue #247: record this writer's concrete type observation
12337- # while the local-scope context is set up (params declared at
12338- # their iteratively-widened ptypes). After all writer-scans
12339- # finish, finalize_ivar_heterogeneity reads the accumulated
12340- # per-slot list and widens to poly when 2+ distinct concrete
12341- # types appear.
12329+ # Record this writer's concrete type observation while
12330+ # the local-scope context is set up (params declared at
12331+ # their iteratively-widened ptypes). After all
12332+ # writer-scans finish, finalize_ivar_heterogeneity reads
12333+ # the accumulated per-slot list and widens to poly when
12334+ # 2+ distinct concrete types appear.
1234212335 record_ivar_observation(@current_class_idx, iname, at, bottom)
1234312336 # Chain participants bypass the `int` guard so that
1234412337 # `@string_slot = @int_slot = expr_returning_int` widens
@@ -12997,13 +12990,13 @@
1299712990 infer_cls_meth_param_from_body
1299812991 # Pre-pass: scan for .new calls to infer constructor param types
1299912992 infer_constructor_types
13000- # Issue #224: bare `new(args)` in an inherited class method body
13001- # widens the subclass's `initialize` ptypes via the cls method's
13002- # already-widened ptypes. Runs after infer_constructor_types so
13003- # the cls method ptypes (set by issue #207's logic in
13004- # scan_new_calls) are current; runs inside the iterative loop
13005- # so subsequent rounds pick up cls method widening from new
13006- # call sites.
12993+ # Bare `new(args)` in an inherited class method body widens
12994+ # the subclass's `initialize` ptypes via the cls method's
12995+ # already-widened ptypes. Runs after infer_constructor_types
12996+ # so the cls method ptypes (set by scan_new_calls' caller-side
12997+ # widening) are current; runs inside the iterative loop so
12998+ # subsequent rounds pick up cls method widening from new call
12999+ # sites.
1300713000 propagate_bare_new_to_subclass_initialize
1300813001 # Update ivar types from constructor params
1300913002 update_ivar_types_from_params
@@ -13067,9 +13060,9 @@
1306713060 set_var_type(lnames[mk], ltypes2[lk])
1306813061 end
1306913062 elsif (ltypes2[lk] == "str_poly_hash" || ltypes2[lk] == "sym_poly_hash") && ltypes[mk] != ltypes2[lk]
13070- # Issue #133: 2nd pass discovered the iterated value is
13071- # poly (each-rebuild over a poly_hash whose source type
13072- # wasn't known on 1st pass). Upgrade.
13063+ # The 2nd pass discovered the iterated value is
13064+ # poly (each-rebuild over a poly_hash whose source
13065+ # type wasn't known on the 1st pass). Upgrade.
1307313066 ltypes[mk] = ltypes2[lk]
1307413067 set_var_type(lnames[mk], ltypes2[lk])
1307513068 end
@@ -13953,17 +13946,15 @@
1395313946 end
1395413947 if mname == "to_sym" || mname == "intern"
1395513948 if @nd_receiver[nid] >= 0
13956- # Issue #314 follow-up: also fire for poly receivers and
13957- # un-yet-typed locals (default `int` from
13958- # LocalVariableReadNode against an empty scope). Block-
13959- # param receivers in shapes like
13960- # `attrs.each { |k, v| row[k.to_sym] = v }` only resolve
13961- # to "string" through later yield-arg unification, but
13962- # scan_features runs once on @root_id before that
13963- # fixpoint completes — and compile_int_method_expr later
13964- # emits `sp_sym_intern(...)` regardless. Without this
13965- # flag, the call site references a function whose
13966- # definition was conditionally skipped, linker error.
13949+ # Fire for poly receivers and not-yet-typed locals
13950+ # (default `int` from LocalVariableReadNode against an
13951+ # empty scope) too, not just statically-known strings.
13952+ # scan_features runs once on @root_id before the
13953+ # yield-arg-type fixpoint resolves block-param receivers,
13954+ # so a shape like `attrs.each { |k, v| row[k.to_sym] = v
13955+ # }` would otherwise miss the flag while
13956+ # compile_int_method_expr still emits `sp_sym_intern(...)`
13957+ # — leaving a linker error against a missing definition.
1396713958 rt = infer_type(@nd_receiver[nid])
1396813959 if rt == "string" || rt == "poly" || rt == "int"
1396913960 @needs_sym_intern = 1
@@ -14514,11 +14505,11 @@
1451414505 k = k + 1
1451514506 next
1451614507 end
14517- # Issue #58: an empty `[]` literal at the call site is
14518- # compatible with any concrete typed-array param type.
14519- # Without this, `foo([])` against a body-promoted
14520- # `str_array` param triggers the ct != at mismatch and
14521- # bumps the param back to poly.
14508+ # An empty `[]` literal at the call site is
14509+ # compatible with any concrete typed-array param.
14510+ # Skip the ct != at mismatch check so `foo([])`
14511+ # against a body-promoted `str_array` param
14512+ # doesn't bump the param back to poly.
1452214513 if is_empty_array_literal(arg_ids[k]) == 1
1452314514 if ct == "str_array" || ct == "float_array" || ct == "sym_array" || is_ptr_array_type(ct) == 1
1452414515 k = k + 1
@@ -14700,14 +14691,12 @@
1470014691 if bid >= 0
1470114692 # Build local scope for this function
1470214693 push_scope
14703- # Issue #314: pin @current_method_name so
14704- # current_lexical_scope_name's `<Mod>_cls_<m>` peel resolves
14705- # bare class refs in the body. Without this, scan_new_calls'
14706- # constructor branch on `Inner.new(x)` inside `M.make` does
14707- # find_class_idx("Inner") (returning -1, since the class is
14708- # registered as `M_Inner`) and skips param widening — so a
14709- # poly arg flowing in via the kwargs widening above never
14710- # reaches the inner class's initialize ptypes.
14694+ # Pin @current_method_name so current_lexical_scope_name's
14695+ # `<Mod>_cls_<m>` peel resolves bare class refs in the
14696+ # body. Without this, `Inner.new(x)` inside `M.make`
14697+ # would find_class_idx("Inner") (returning -1; the class
14698+ # is registered as `M_Inner`) and skip param widening —
14699+ # leaving the inner class's initialize ptypes un-widened.
1471114700 saved_method_name = @current_method_name
1471214701 @current_method_name = @meth_names[mi]
1471314702 pnames = @meth_param_names[mi].split(",")
@@ -14940,9 +14929,9 @@
1494014929 end
1494114930 mi = mi + 1
1494214931 end
14943- # Issue #207: also iterate this class's class methods
14944- # (def self.<m>) so a `params.fetch(:k, "")` call inside a
14945- # `def self.from_raw` widens P.fetch's default param via the
14932+ # Also iterate this class's class methods (def self.<m>)
14933+ # so a `params.fetch(:k, "")` call inside a `def
14934+ # self.from_raw` widens P.fetch's default param via the
1494614935 # receiver-method unify path inside scan_new_calls.
1494714936 cm_names = @cls_cmeth_names[ci].split(";")
1494814937 cm_bodies = @cls_cmeth_bodies[ci].split(";")
@@ -15003,8 +14992,9 @@
1500314992 if nid < 0
1500414993 return
1500514994 end
15006- # Issue #207: same is_a? narrow as scan_new_calls so a self
15007- # call inside the then-arm sees the narrowed receiver type.
14995+ # Apply the same is_a? narrow that scan_new_calls uses, so a
14996+ # self call inside the then-arm sees the narrowed receiver
14997+ # type.
1500814998 if @nd_type[nid] == "IfNode"
1500914999 pred = @nd_predicate[nid]
1501015000 if pred >= 0