Commit abe5219a
abe5219a9d46Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 3 files · +2 added · ~1 modified
Description
fix(analyze): walk parent chain for ivar lazy-init push widening
#430 wired up "push through getter" widening for the same-class
case: def errors; @errors = [] if @errors.nil?; @errors; end
plus errors << "bad" from a sibling method on the SAME class
promoted @errors from IntArray to StrArray. The cross-class case
where the getter lives on a parent class and the push happens
from a child method went unhandled — the parent's @errors stayed
IntArray and the child's String push failed C compile.
scan_writer_calls' push-through-getter arm consulted @current_class_idx exclusively for both the getter lookup and the ivar-type update. Widen the resolution: walk @cls_parents[]-up from the current class, asking each ancestor whether it defines the getter and the getter returns a bare ivar. The first match wins. Apply replace_ivar_type against the owner class so the struct-emit (which walks ancestors for fields) picks up the promoted type in BOTH the parent struct and every subclass that inherits the field.
Hits the canonical Rails ActiveRecord::Base#errors shape: each
model subclass's validate method pushes strings into the
inherited errors accessor. The parent's @errors field now
widens to sp_StrArray * based on the observed subclass pushes,
same as #430 already did for the same-class shape.
Tests: 404 pass (+1 regression covering parent-defined getter, two-level inheritance, plus a same-class case to confirm the #430 path still fires). bench 52, optcarrot checksum 59662.
Fixes #451
Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]
Details
- Commit
- abe5219a9d46
- Parent
- b3e278f17a9b
spinel_analyze.rbmodifiedView file
# the IntArray default (from `[]`) promotes to the correct # element-typed array when the push args are non-int. push_alias_iname = "" push_alias_owner_ci = -1 if (mname == "push" || mname == "<<") && @current_class_idx >= 0 && recv >= 0 getter_mname_x = "" if @nd_type[recv] == "CallNode" && @nd_receiver[recv] < 0 getter_mname = @nd_name[recv] push_alias_iname = method_returns_ivar_in_class(@current_class_idx, getter_mname) getter_mname_x = @nd_name[recv] end if @nd_type[recv] == "CallNode" && @nd_receiver[recv] >= 0 && @nd_type[@nd_receiver[recv]] == "SelfNode" getter_mname_s = @nd_name[recv] push_alias_iname = method_returns_ivar_in_class(@current_class_idx, getter_mname_s) getter_mname_x = @nd_name[recv] end if getter_mname_x != "" # Walk the parent chain so a getter defined on a parent # class (Rails-style `def errors; @errors = [] if # @errors.nil?; @errors; end` on Base) is matched when the # push happens from a subclass method. Without the walk # (#430 only inspected @current_class_idx) the parent's # ivar stays IntArray and the subclass's String push fails # C compile (#451). search_ci = @current_class_idx while search_ci >= 0 iname_try = method_returns_ivar_in_class(search_ci, getter_mname_x) if iname_try != "" push_alias_iname = iname_try push_alias_owner_ci = search_ci break end parent_name = @cls_parents[search_ci] if parent_name == "" search_ci = -1 else search_ci = find_class_idx(parent_name) end end end end if (mname == "push" || mname == "<<") && @current_class_idx >= 0 && push_alias_iname != "" if (mname == "push" || mname == "<<") && push_alias_owner_ci >= 0 && push_alias_iname != "" iname = push_alias_iname cur_t = cls_ivar_type(@current_class_idx, iname) cur_t = cls_ivar_type(push_alias_owner_ci, iname) if cur_t == "int_array" args_id = @nd_arguments[nid] if args_id >= 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) replace_ivar_type(push_alias_owner_ci, iname, promoted) if promoted == "str_array" @needs_str_array = 1 elsif promoted == "float_array"test/ivar_lazy_init_push_via_inherited_getter.rbaddedView file
Loading diff…
test/ivar_lazy_init_push_via_inherited_getter.rb.expectedaddedView file
Loading diff…