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
end end # Issue #430: when `mname` is an instance method on class `ci` # whose body returns a bare `@<iname>` as its last expression, # return that ivar name. Lets scan_writer_calls treat # `<getter>.push(v)` and `<getter> << v` as if the push were # against the ivar directly. Returns "" when the method # doesn't match the simple getter shape or isn't defined on # the class. def method_returns_ivar_in_class(ci, mname) if ci < 0 return "" end midx = cls_find_method_direct(ci, mname) if midx < 0 return "" end bodies = @cls_meth_bodies[ci].split(";") if midx >= bodies.length return "" end bid = bodies[midx].to_i if bid < 0 return "" end stmts = get_stmts(bid) if stmts.length == 0 return "" end last = stmts[stmts.length - 1] if @nd_type[last] == "InstanceVariableReadNode" return @nd_name[last] end "" end def scan_writer_calls(nid) bname = "" if nid < 0 end end end # Issue #430: push through a getter method that returns the # ivar. Recognise the canonical "lazy init + return" shape: # # def errors # @errors = [] if @errors.nil? # @errors # end # ... # errors << "bad" # # The recv of `<<` is a bare call to `errors`, not the ivar # directly. Resolve through that to the underlying ivar so # the IntArray default (from `[]`) promotes to the correct # element-typed array when the push args are non-int. push_alias_iname = "" if (mname == "push" || mname == "<<") && @current_class_idx >= 0 && recv >= 0 if @nd_type[recv] == "CallNode" && @nd_receiver[recv] < 0 getter_mname_430 = @nd_name[recv] push_alias_iname = method_returns_ivar_in_class(@current_class_idx, getter_mname_430) end if @nd_type[recv] == "CallNode" && @nd_receiver[recv] >= 0 && @nd_type[@nd_receiver[recv]] == "SelfNode" getter_mname_430s = @nd_name[recv] push_alias_iname = method_returns_ivar_in_class(@current_class_idx, getter_mname_430s) end end if (mname == "push" || mname == "<<") && @current_class_idx >= 0 && push_alias_iname != "" iname = push_alias_iname cur_t = cls_ivar_type(@current_class_idx, iname) if cur_t == "int_array" args_id = @nd_arguments[nid] if args_id >= 0 ai = get_args(args_id) if ai.length > 0 et = infer_type(ai[0]) promoted = empty_array_promotion_for([et]) if promoted != "" && promoted != cur_t replace_ivar_type(@current_class_idx, iname, promoted) if promoted == "str_array" @needs_str_array = 1 elsif promoted == "float_array" @needs_float_array = 1 elsif promoted == "sym_array" @needs_sym_intern = 1 elsif promoted == "poly_array" @needs_poly_array = 1 @needs_rb_value = 1 end end end end end end if (mname == "push" || mname == "<<") && @current_class_idx >= 0 && recv >= 0 && @nd_type[recv] == "InstanceVariableReadNode" iname = @nd_name[recv] cur_t = cls_ivar_type(@current_class_idx, iname)test/ivar_lazy_init_push_via_getter.rbaddedView file
Loading diff…
test/ivar_lazy_init_push_via_getter.rb.expectedaddedView file
Loading diff…