Commit c85fd4a6

c85fd4a6f4c4Merged

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

Description

fix(analyze): ivar lazy-init plus push-through-getter widening (#430)

`@ivar = [] if @ivar.nil?` followed by pushes routed through a getter method that returns the ivar left the ivar inferred as IntArray (from the empty `[]` default) regardless of what the sibling method pushed in. Strings, symbols, etc. then mismatched `sp_IntArray_push`'s mrb_int param at C compile.

scan_writer_calls' direct-ivar arm (`@x.push(v)` / `@x << v` on a recv that is itself an InstanceVariableReadNode) caught the in-method push. The cross-method case where the push reads through a getter -- `errors << "bad"` calling `def errors; ... @errors; end` -- fell through because the `<<` recv is a CallNode, not a direct ivar read.

Fix: extend scan_writer_calls to also recognise the bare-call shape `(*args) << v` and the self-prefixed `self. << v`. A new method_returns_ivar_in_class helper looks at the named method's body and returns the `@` when the body's last expression is a bare InstanceVariableReadNode -- the canonical "lazy init + return ivar" pattern. The push observation then lands on the underlying ivar and the existing empty-array promotion (`int_array` -> `str_array` / `poly_array` / etc.) fires.

The Rails `ActiveRecord::Base#errors` pattern -- lazy-init `@errors` via nil guard, push error messages via `errors << "..."` from validation methods -- compiles cleanly now.

Bootstrap 4-way fixpoint OK; optcarrot checksum 59662 unchanged; 386 tests pass.

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

Details

Commit
c85fd4a6f4c4
Parent
0b501e4f4758

spinel_analyze.rbmodifiedView file

@@ -12073,6 +12073,40 @@
1207312073 end
1207412074 end
1207512075
12076+ # Issue #430: when `mname` is an instance method on class `ci`
12077+ # whose body returns a bare `@<iname>` as its last expression,
12078+ # return that ivar name. Lets scan_writer_calls treat
12079+ # `<getter>.push(v)` and `<getter> << v` as if the push were
12080+ # against the ivar directly. Returns "" when the method
12081+ # doesn't match the simple getter shape or isn't defined on
12082+ # the class.
12083+ def method_returns_ivar_in_class(ci, mname)
12084+ if ci < 0
12085+ return ""
12086+ end
12087+ midx = cls_find_method_direct(ci, mname)
12088+ if midx < 0
12089+ return ""
12090+ end
12091+ bodies = @cls_meth_bodies[ci].split(";")
12092+ if midx >= bodies.length
12093+ return ""
12094+ end
12095+ bid = bodies[midx].to_i
12096+ if bid < 0
12097+ return ""
12098+ end
12099+ stmts = get_stmts(bid)
12100+ if stmts.length == 0
12101+ return ""
12102+ end
12103+ last = stmts[stmts.length - 1]
12104+ if @nd_type[last] == "InstanceVariableReadNode"
12105+ return @nd_name[last]
12106+ end
12107+ ""
12108+ end
12109+
1207612110 def scan_writer_calls(nid)
1207712111 bname = ""
1207812112 if nid < 0
@@ -12311,6 +12345,58 @@
1231112345 end
1231212346 end
1231312347 end
12348+ # Issue #430: push through a getter method that returns the
12349+ # ivar. Recognise the canonical "lazy init + return" shape:
12350+ #
12351+ # def errors
12352+ # @errors = [] if @errors.nil?
12353+ # @errors
12354+ # end
12355+ # ...
12356+ # errors << "bad"
12357+ #
12358+ # The recv of `<<` is a bare call to `errors`, not the ivar
12359+ # directly. Resolve through that to the underlying ivar so
12360+ # the IntArray default (from `[]`) promotes to the correct
12361+ # element-typed array when the push args are non-int.
12362+ push_alias_iname = ""
12363+ if (mname == "push" || mname == "<<") && @current_class_idx >= 0 && recv >= 0
12364+ if @nd_type[recv] == "CallNode" && @nd_receiver[recv] < 0
12365+ getter_mname_430 = @nd_name[recv]
12366+ push_alias_iname = method_returns_ivar_in_class(@current_class_idx, getter_mname_430)
12367+ end
12368+ if @nd_type[recv] == "CallNode" && @nd_receiver[recv] >= 0 && @nd_type[@nd_receiver[recv]] == "SelfNode"
12369+ getter_mname_430s = @nd_name[recv]
12370+ push_alias_iname = method_returns_ivar_in_class(@current_class_idx, getter_mname_430s)
12371+ end
12372+ end
12373+ if (mname == "push" || mname == "<<") && @current_class_idx >= 0 && push_alias_iname != ""
12374+ iname = push_alias_iname
12375+ cur_t = cls_ivar_type(@current_class_idx, iname)
12376+ if cur_t == "int_array"
12377+ args_id = @nd_arguments[nid]
12378+ if args_id >= 0
12379+ ai = get_args(args_id)
12380+ if ai.length > 0
12381+ et = infer_type(ai[0])
12382+ promoted = empty_array_promotion_for([et])
12383+ if promoted != "" && promoted != cur_t
12384+ replace_ivar_type(@current_class_idx, iname, promoted)
12385+ if promoted == "str_array"
12386+ @needs_str_array = 1
12387+ elsif promoted == "float_array"
12388+ @needs_float_array = 1
12389+ elsif promoted == "sym_array"
12390+ @needs_sym_intern = 1
12391+ elsif promoted == "poly_array"
12392+ @needs_poly_array = 1
12393+ @needs_rb_value = 1
12394+ end
12395+ end
12396+ end
12397+ end
12398+ end
12399+ end
1231412400 if (mname == "push" || mname == "<<") && @current_class_idx >= 0 && recv >= 0 && @nd_type[recv] == "InstanceVariableReadNode"
1231512401 iname = @nd_name[recv]
1231612402 cur_t = cls_ivar_type(@current_class_idx, iname)