Commit 5dcdfe2e
5dcdfe2e9687Merged
Yusuke Endoh <[email protected]> · 6 months ago · 1 file · ~1 modified
Description
fix(codegen): widen int_array ivar to ptr_array on []= write of obj
@arr[i] = m against an ivar typed int_array (the [nil] * N
empty default) now widens the slot to <obj>_ptr_array when the
value is an obj-typed expression, matching the existing widening
for << / push.
Two-part fix:
- New
[]=branch inscan_writer_callsmirroring the existing push/<<branch. update_ivar_typeno longer collapses an already-promoted<obj>_ptr_arrayslot topoly_arraywhen a laterint_array(i.e. the[nil] * Nreseed) disagrees — the typed promotion is more specific.
Without this, an [nil] * N-initialised ivar that's later
filled via @arr[i] = obj keeps int_array typing, the obj
pointer is silently truncated through mrb_int, and reads
through any dispatch path that expects pointer storage return
garbage.
Details
- Commit
- 5dcdfe2e9687
- Parent
- 15f7e5f356d7
spinel_codegen.rbmodifiedView file
@@ -6243,6 +6243,15 @@
62436243
@cls_ivar_types[ci] = types.join(";")62446244
elsif old != new_type && old != "poly"62456245
if is_array_type(old) == 1 && is_array_type(new_type) == 16246+
# Don't widen a typed `<obj>_ptr_array` slot back to6247+
# poly when the disagreement is just `int_array`. That6248+
# disagreement comes from the `[nil] * N` empty default6249+
# — the writer-scan `[]=` / `<<` widening is more6250+
# specific and should win.6251+
if is_ptr_array_type(old) == 1 && new_type == "int_array"6252+
k = k + 16253+
next6254+
end62466255
types[k] = "poly_array"62476256
@needs_rb_value = 162486257
@cls_ivar_types[ci] = types.join(";")@@ -8554,8 +8563,11 @@
85548563
return "poly_array"85558564
end85568565
if is_obj_type(elem_acc[0]) == 18557-
@needs_rb_value = 18558-
return "poly_array"8566+
# Homogeneous obj array — use a typed `<obj>_ptr_array` so reads8567+
# return a typed pointer (no sp_RbVal unbox needed at the call8568+
# site). Falls back to poly_array via normal widening when a8569+
# later mismatched-type write happens.8570+
return base_type(elem_acc[0]) + "_ptr_array"85598571
end85608572
""85618573
end@@ -9003,6 +9015,39 @@
90039015
end90049016
end90059017
end9018+
# `@arr[i] = v` against an ivar typed `int_array` (the9019+
# `[nil] * N` empty default) should widen to match the value9020+
# type the same way `<<` / `push` does. Without this, optcarrot's9021+
# `@fetch[addr] = method(:peek_X)` writes a Method into an9022+
# int_array slot — the read side then can't recover and `[].call`9023+
# dispatches against int.9024+
if mname == "[]=" && @current_class_idx >= 0 && recv >= 0 && @nd_type[recv] == "InstanceVariableReadNode"9025+
iname = @nd_name[recv]9026+
cur_t = cls_ivar_type(@current_class_idx, iname)9027+
if cur_t == "int_array"9028+
args_id = @nd_arguments[nid]9029+
if args_id >= 09030+
ai = get_args(args_id)9031+
if ai.length >= 29032+
vt = infer_type(ai[ai.length - 1])9033+
promoted = empty_array_promotion_for([vt])9034+
if promoted != "" && promoted != cur_t9035+
replace_ivar_type(@current_class_idx, iname, promoted)9036+
if promoted == "str_array"9037+
@needs_str_array = 19038+
elsif promoted == "float_array"9039+
@needs_float_array = 19040+
elsif promoted == "sym_array"9041+
@needs_int_array = 19042+
elsif promoted == "poly_array"9043+
@needs_rb_value = 19044+
@needs_gc = 19045+
end9046+
end9047+
end9048+
end9049+
end9050+
end90069051
end90079052
# Recurse via the centralized child walker (push_child_ids covers90089053
# the full set of AST slots — visiting a few extra slots is a