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

@@ -12519,19 +12519,43 @@
1251912519 # the IntArray default (from `[]`) promotes to the correct
1252012520 # element-typed array when the push args are non-int.
1252112521 push_alias_iname = ""
12522+ push_alias_owner_ci = -1
1252212523 if (mname == "push" || mname == "<<") && @current_class_idx >= 0 && recv >= 0
12524+ getter_mname_x = ""
1252312525 if @nd_type[recv] == "CallNode" && @nd_receiver[recv] < 0
12524- getter_mname = @nd_name[recv]
12525- push_alias_iname = method_returns_ivar_in_class(@current_class_idx, getter_mname)
12526+ getter_mname_x = @nd_name[recv]
1252612527 end
1252712528 if @nd_type[recv] == "CallNode" && @nd_receiver[recv] >= 0 && @nd_type[@nd_receiver[recv]] == "SelfNode"
12528- getter_mname_s = @nd_name[recv]
12529- push_alias_iname = method_returns_ivar_in_class(@current_class_idx, getter_mname_s)
12529+ getter_mname_x = @nd_name[recv]
12530+ end
12531+ if getter_mname_x != ""
12532+ # Walk the parent chain so a getter defined on a parent
12533+ # class (Rails-style `def errors; @errors = [] if
12534+ # @errors.nil?; @errors; end` on Base) is matched when the
12535+ # push happens from a subclass method. Without the walk
12536+ # (#430 only inspected @current_class_idx) the parent's
12537+ # ivar stays IntArray and the subclass's String push fails
12538+ # C compile (#451).
12539+ search_ci = @current_class_idx
12540+ while search_ci >= 0
12541+ iname_try = method_returns_ivar_in_class(search_ci, getter_mname_x)
12542+ if iname_try != ""
12543+ push_alias_iname = iname_try
12544+ push_alias_owner_ci = search_ci
12545+ break
12546+ end
12547+ parent_name = @cls_parents[search_ci]
12548+ if parent_name == ""
12549+ search_ci = -1
12550+ else
12551+ search_ci = find_class_idx(parent_name)
12552+ end
12553+ end
1253012554 end
1253112555 end
12532- if (mname == "push" || mname == "<<") && @current_class_idx >= 0 && push_alias_iname != ""
12556+ if (mname == "push" || mname == "<<") && push_alias_owner_ci >= 0 && push_alias_iname != ""
1253312557 iname = push_alias_iname
12534- cur_t = cls_ivar_type(@current_class_idx, iname)
12558+ cur_t = cls_ivar_type(push_alias_owner_ci, iname)
1253512559 if cur_t == "int_array"
1253612560 args_id = @nd_arguments[nid]
1253712561 if args_id >= 0
@@ -12540,7 +12564,7 @@
1254012564 et = infer_type(ai[0])
1254112565 promoted = empty_array_promotion_for([et])
1254212566 if promoted != "" && promoted != cur_t
12543- replace_ivar_type(@current_class_idx, iname, promoted)
12567+ replace_ivar_type(push_alias_owner_ci, iname, promoted)
1254412568 if promoted == "str_array"
1254512569 @needs_str_array = 1
1254612570 elsif promoted == "float_array"