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
end1806218062
1806318063
# Collect every element type seen in `pname.push(elem)` or18064+
# True (1) when `name` is assigned an empty `[]` array literal18065+
# somewhere under `nid`. Used to gate the empty-array-local18066+
# promotion (mirrors the empty-default-param gate): only a local18067+
# that started as `[]` has a deferred element type that a later18068+
# `<<` / push should resolve. A `name = [1, 2]` non-empty literal18069+
# is a genuine int_array and must not be re-promoted from its18070+
# object pushes.18071+
# Promote empty-array locals (`x = []`) in the current scope from18072+
# their `[]` seed (int_array) to the concrete typed-array implied by18073+
# their pushes. scan_locals_first_type doesn't track the18074+
# deferred-element-then-push widening that scan_locals / codegen18075+
# apply, so an `x = []; x << obj` local is otherwise left at18076+
# int_array. That under-typing flows through scan_new_calls ->18077+
# widen_ptypes_from_args: a local passed as a call argument widens18078+
# the callee's param to int_array instead of the real18079+
# `<obj>_ptr_array`, and the value (a real PtrArray at runtime) is18080+
# later read back as an IntArray -- e.g. `@cache = list` then18081+
# `@cache.size` returns garbage. Mirrors18082+
# 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 a18085+
# promoted receiver (`promoted.each { |r| ... }`) re-derive their18086+
# element type on the next pass, letting the widening propagate18087+
# transitively (e.g. loaded -> r -> group).18088+
def promote_empty_array_locals_in_scope(bid, names, types)18089+
li = 018090+
while li < names.length18091+
if li < types.length && types[li] == "int_array" && local_initialized_empty_array(bid, names[li]) == 118092+
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+
end18098+
end18099+
li = li + 118100+
end18101+
end18102+
18103+
def local_initialized_empty_array(nid, name)18104+
if nid < 018105+
return 018106+
end18107+
if @nd_type[nid] == "DefNode" || @nd_type[nid] == "ClassNode" || @nd_type[nid] == "ModuleNode"18108+
return 018109+
end18110+
if @nd_type[nid] == "LocalVariableWriteNode" && @nd_name[nid] == name18111+
rhs = @nd_expression[nid]18112+
if rhs >= 0 && @nd_type[rhs] == "ArrayNode" && parse_id_list(@nd_elements[rhs]).length == 018113+
return 118114+
end18115+
end18116+
cs = []18117+
push_child_ids(nid, cs)18118+
k = 018119+
while k < cs.length18120+
if local_initialized_empty_array(cs[k], name) == 118121+
return 118122+
end18123+
k = k + 118124+
end18125+
018126+
end18127+
1806418128
# `pname << elem` patterns under nid. The deferred-element-type1806518129
# promotion pass uses this to decide what concrete typed-array a1806618130
# parameter should be promoted to when callers all passed empty@@ -22929,6 +22993,10 @@
2292922993
declare_var(lnames[lk], ltypes[lk])2293022994
lk = lk + 12293122995
end22996+
# Promote empty-array locals before the second scan so a22997+
# `promoted.each { |r| ... }` block param re-derives its22998+
# 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 inference2293323001
lnames2 = "".split(",", -1)2293423002
ltypes2 = "".split(",", -1)@@ -22940,6 +23008,11 @@
2294023008
end2294123009
lk2 = lk2 + 12294223010
end23011+
# Re-promote after the second scan: locals whose pushes feed23012+
# 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 class2294423017
scan_cls_method_calls(ci, bid)2294523018
# Also scan for constructor calls to infer param typestest/empty_array_local_arg_widen.rbaddedView file
Loading diff…
test/empty_array_local_arg_widen.rb.expectedaddedView file
Loading diff…