Commit 59fc3525

59fc3525f550Merged

Sam Ruby <[email protected]> · 5 months ago · 3 files · +2 added · ~1 modified

Description

Widen empty-array locals from their pushes when passed as call args

The class-body call-type pass (infer_class_body_call_types) sets up local variable types via scan_locals_first_type, which records an x = [] local from its [] seed alone (int_array) and ignores the later x << <obj> pushes -- unlike scan_locals / codegen, which track the deferred-element-then-push widening.

That under-typing escapes the scope: when such a local is passed as a call argument, scan_new_calls -> widen_ptypes_from_args widens the callee's parameter from the arg's (stale int_array) type, so a parameter that actually receives an object array is typed int_array. If the callee stores it in an ivar (@cache = list), the ivar becomes int_array too; a real PtrArray is then stored and later read back as an IntArray, and .size / .length returns garbage.

Surfaced in the roundhouse blog benchmark: the articles index builds a per-article comment group into a fresh local array (inside nested each blocks) and hands it to Article#_preload_comments, whose @comments_cache ivar collapsed to int_array. The index then rendered a bogus comment count (e.g. "(16 comments)" instead of "(0 comments)"), and the generated C carried an incompatible-pointer warning (sp_IntArray * param fed a sp_PtrArray * arg).

Fix: after each scan_locals_first_type pass, promote empty-array locals to the concrete typed-array implied by their pushes (promote_empty_array_locals_in_scope), gated on an actual empty-[] initializer so a genuine [1, 2] int_array is never re-promoted. Running it both before and after the second scan lets the widening propagate transitively through block params -- e.g. loaded (a local) -> r in loaded.each { |r| ... } -> group << r -> the call arg. Mirrors infer_param_array_type_from_body's empty-default-param promotion, applied to locals.

Test: empty_array_local_arg_widen.rb reproduces the transitive nested-block shape; pre-fix it prints garbage counts (16/16), post-fix it matches the CRuby reference (2/1). Full blog scripts/compare spinel now matches on all 7 paths (/ and /articles previously differed). Suite 705 pass / 0 fail.

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

Details

Commit
59fc3525f550
Parent
f6a6d35cbbd0

spinel_analyze.rbmodifiedView file

@@ -17979,6 +17979,70 @@
1797917979 end
1798017980
1798117981 # Collect every element type seen in `pname.push(elem)` or
17982+ # True (1) when `name` is assigned an empty `[]` array literal
17983+ # somewhere under `nid`. Used to gate the empty-array-local
17984+ # promotion (mirrors the empty-default-param gate): only a local
17985+ # that started as `[]` has a deferred element type that a later
17986+ # `<<` / push should resolve. A `name = [1, 2]` non-empty literal
17987+ # is a genuine int_array and must not be re-promoted from its
17988+ # object pushes.
17989+ # Promote empty-array locals (`x = []`) in the current scope from
17990+ # their `[]` seed (int_array) to the concrete typed-array implied by
17991+ # their pushes. scan_locals_first_type doesn't track the
17992+ # deferred-element-then-push widening that scan_locals / codegen
17993+ # apply, so an `x = []; x << obj` local is otherwise left at
17994+ # int_array. That under-typing flows through scan_new_calls ->
17995+ # widen_ptypes_from_args: a local passed as a call argument widens
17996+ # the callee's param to int_array instead of the real
17997+ # `<obj>_ptr_array`, and the value (a real PtrArray at runtime) is
17998+ # later read back as an IntArray -- e.g. `@cache = list` then
17999+ # `@cache.size` returns garbage. Mirrors
18000+ # infer_param_array_type_from_body's empty-default-param promotion,
18001+ # applied to locals and gated on an actual empty-`[]` initializer.
18002+ # Run after each scan_locals_first_type pass so block params of a
18003+ # promoted receiver (`promoted.each { |r| ... }`) re-derive their
18004+ # element type on the next pass, letting the widening propagate
18005+ # transitively (e.g. loaded -> r -> group).
18006+ def promote_empty_array_locals_in_scope(bid, names, types)
18007+ li = 0
18008+ while li < names.length
18009+ if li < types.length && types[li] == "int_array" && local_initialized_empty_array(bid, names[li]) == 1
18010+ elem_acc_pl = "".split(",", -1)
18011+ collect_param_push_elem_types(bid, names[li], elem_acc_pl)
18012+ promoted_pl = empty_array_promotion_for(elem_acc_pl)
18013+ if promoted_pl != "" && promoted_pl != "int_array"
18014+ set_var_type(names[li], promoted_pl)
18015+ end
18016+ end
18017+ li = li + 1
18018+ end
18019+ end
18020+
18021+ def local_initialized_empty_array(nid, name)
18022+ if nid < 0
18023+ return 0
18024+ end
18025+ if @nd_type[nid] == "DefNode" || @nd_type[nid] == "ClassNode" || @nd_type[nid] == "ModuleNode"
18026+ return 0
18027+ end
18028+ if @nd_type[nid] == "LocalVariableWriteNode" && @nd_name[nid] == name
18029+ rhs = @nd_expression[nid]
18030+ if rhs >= 0 && @nd_type[rhs] == "ArrayNode" && parse_id_list(@nd_elements[rhs]).length == 0
18031+ return 1
18032+ end
18033+ end
18034+ cs = []
18035+ push_child_ids(nid, cs)
18036+ k = 0
18037+ while k < cs.length
18038+ if local_initialized_empty_array(cs[k], name) == 1
18039+ return 1
18040+ end
18041+ k = k + 1
18042+ end
18043+ 0
18044+ end
18045+
1798218046 # `pname << elem` patterns under nid. The deferred-element-type
1798318047 # promotion pass uses this to decide what concrete typed-array a
1798418048 # parameter should be promoted to when callers all passed empty
@@ -22847,6 +22911,10 @@
2284722911 declare_var(lnames[lk], ltypes[lk])
2284822912 lk = lk + 1
2284922913 end
22914+ # Promote empty-array locals before the second scan so a
22915+ # `promoted.each { |r| ... }` block param re-derives its
22916+ # element type from the promoted receiver below.
22917+ promote_empty_array_locals_in_scope(bid, lnames, ltypes)
2285022918 # Second pass: rescan with locals now in scope for better inference
2285122919 lnames2 = "".split(",", -1)
2285222920 ltypes2 = "".split(",", -1)
@@ -22858,6 +22926,11 @@
2285822926 end
2285922927 lk2 = lk2 + 1
2286022928 end
22929+ # Re-promote after the second scan: locals whose pushes feed
22930+ # off a now-typed block param (e.g. `group << r` where `r`
22931+ # only became concrete once its receiver was promoted above)
22932+ # resolve on this pass.
22933+ promote_empty_array_locals_in_scope(bid, lnames2, ltypes2)
2286122934 # Scan for calls to other methods in same class
2286222935 scan_cls_method_calls(ci, bid)
2286322936 # Also scan for constructor calls to infer param types