Commit e497084a

e497084a1b80Merged

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

Description

Merge pull request #1195 from rubys/fix/empty-array-local-arg-widen

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

Details

Commit
e497084a1b80
Parent
75a053431aa3
Merge parent
59fc3525f550

spinel_analyze.rbmodifiedView file

@@ -18061,6 +18061,70 @@
1806118061 end
1806218062
1806318063 # Collect every element type seen in `pname.push(elem)` or
18064+ # True (1) when `name` is assigned an empty `[]` array literal
18065+ # somewhere under `nid`. Used to gate the empty-array-local
18066+ # promotion (mirrors the empty-default-param gate): only a local
18067+ # that started as `[]` has a deferred element type that a later
18068+ # `<<` / push should resolve. A `name = [1, 2]` non-empty literal
18069+ # is a genuine int_array and must not be re-promoted from its
18070+ # object pushes.
18071+ # Promote empty-array locals (`x = []`) in the current scope from
18072+ # their `[]` seed (int_array) to the concrete typed-array implied by
18073+ # their pushes. scan_locals_first_type doesn't track the
18074+ # deferred-element-then-push widening that scan_locals / codegen
18075+ # apply, so an `x = []; x << obj` local is otherwise left at
18076+ # int_array. That under-typing flows through scan_new_calls ->
18077+ # widen_ptypes_from_args: a local passed as a call argument widens
18078+ # the callee's param to int_array instead of the real
18079+ # `<obj>_ptr_array`, and the value (a real PtrArray at runtime) is
18080+ # later read back as an IntArray -- e.g. `@cache = list` then
18081+ # `@cache.size` returns garbage. Mirrors
18082+ # infer_param_array_type_from_body's empty-default-param promotion,
18083+ # applied to locals and gated on an actual empty-`[]` initializer.
18084+ # Run after each scan_locals_first_type pass so block params of a
18085+ # promoted receiver (`promoted.each { |r| ... }`) re-derive their
18086+ # element type on the next pass, letting the widening propagate
18087+ # transitively (e.g. loaded -> r -> group).
18088+ def promote_empty_array_locals_in_scope(bid, names, types)
18089+ li = 0
18090+ while li < names.length
18091+ if li < types.length && types[li] == "int_array" && local_initialized_empty_array(bid, names[li]) == 1
18092+ elem_acc_pl = "".split(",", -1)
18093+ collect_param_push_elem_types(bid, names[li], elem_acc_pl)
18094+ promoted_pl = empty_array_promotion_for(elem_acc_pl)
18095+ if promoted_pl != "" && promoted_pl != "int_array"
18096+ set_var_type(names[li], promoted_pl)
18097+ end
18098+ end
18099+ li = li + 1
18100+ end
18101+ end
18102+
18103+ def local_initialized_empty_array(nid, name)
18104+ if nid < 0
18105+ return 0
18106+ end
18107+ if @nd_type[nid] == "DefNode" || @nd_type[nid] == "ClassNode" || @nd_type[nid] == "ModuleNode"
18108+ return 0
18109+ end
18110+ if @nd_type[nid] == "LocalVariableWriteNode" && @nd_name[nid] == name
18111+ rhs = @nd_expression[nid]
18112+ if rhs >= 0 && @nd_type[rhs] == "ArrayNode" && parse_id_list(@nd_elements[rhs]).length == 0
18113+ return 1
18114+ end
18115+ end
18116+ cs = []
18117+ push_child_ids(nid, cs)
18118+ k = 0
18119+ while k < cs.length
18120+ if local_initialized_empty_array(cs[k], name) == 1
18121+ return 1
18122+ end
18123+ k = k + 1
18124+ end
18125+ 0
18126+ end
18127+
1806418128 # `pname << elem` patterns under nid. The deferred-element-type
1806518129 # promotion pass uses this to decide what concrete typed-array a
1806618130 # parameter should be promoted to when callers all passed empty
@@ -22929,6 +22993,10 @@
2292922993 declare_var(lnames[lk], ltypes[lk])
2293022994 lk = lk + 1
2293122995 end
22996+ # Promote empty-array locals before the second scan so a
22997+ # `promoted.each { |r| ... }` block param re-derives its
22998+ # element type from the promoted receiver below.
22999+ promote_empty_array_locals_in_scope(bid, lnames, ltypes)
2293223000 # Second pass: rescan with locals now in scope for better inference
2293323001 lnames2 = "".split(",", -1)
2293423002 ltypes2 = "".split(",", -1)
@@ -22940,6 +23008,11 @@
2294023008 end
2294123009 lk2 = lk2 + 1
2294223010 end
23011+ # Re-promote after the second scan: locals whose pushes feed
23012+ # off a now-typed block param (e.g. `group << r` where `r`
23013+ # only became concrete once its receiver was promoted above)
23014+ # resolve on this pass.
23015+ promote_empty_array_locals_in_scope(bid, lnames2, ltypes2)
2294323016 # Scan for calls to other methods in same class
2294423017 scan_cls_method_calls(ci, bid)
2294523018 # Also scan for constructor calls to infer param types