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:

  1. New []= branch in scan_writer_calls mirroring the existing push/<< branch.
  2. update_ivar_type no longer collapses an already-promoted <obj>_ptr_array slot to poly_array when a later int_array (i.e. the [nil] * N reseed) 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) == 1
6246+ # Don't widen a typed `<obj>_ptr_array` slot back to
6247+ # poly when the disagreement is just `int_array`. That
6248+ # disagreement comes from the `[nil] * N` empty default
6249+ # — the writer-scan `[]=` / `<<` widening is more
6250+ # specific and should win.
6251+ if is_ptr_array_type(old) == 1 && new_type == "int_array"
6252+ k = k + 1
6253+ next
6254+ end
62466255 types[k] = "poly_array"
62476256 @needs_rb_value = 1
62486257 @cls_ivar_types[ci] = types.join(";")
@@ -8554,8 +8563,11 @@
85548563 return "poly_array"
85558564 end
85568565 if is_obj_type(elem_acc[0]) == 1
8557- @needs_rb_value = 1
8558- return "poly_array"
8566+ # Homogeneous obj array — use a typed `<obj>_ptr_array` so reads
8567+ # return a typed pointer (no sp_RbVal unbox needed at the call
8568+ # site). Falls back to poly_array via normal widening when a
8569+ # later mismatched-type write happens.
8570+ return base_type(elem_acc[0]) + "_ptr_array"
85598571 end
85608572 ""
85618573 end
@@ -9003,6 +9015,39 @@
90039015 end
90049016 end
90059017 end
9018+ # `@arr[i] = v` against an ivar typed `int_array` (the
9019+ # `[nil] * N` empty default) should widen to match the value
9020+ # type the same way `<<` / `push` does. Without this, optcarrot's
9021+ # `@fetch[addr] = method(:peek_X)` writes a Method into an
9022+ # 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 >= 0
9030+ ai = get_args(args_id)
9031+ if ai.length >= 2
9032+ vt = infer_type(ai[ai.length - 1])
9033+ promoted = empty_array_promotion_for([vt])
9034+ if promoted != "" && promoted != cur_t
9035+ replace_ivar_type(@current_class_idx, iname, promoted)
9036+ if promoted == "str_array"
9037+ @needs_str_array = 1
9038+ elsif promoted == "float_array"
9039+ @needs_float_array = 1
9040+ elsif promoted == "sym_array"
9041+ @needs_int_array = 1
9042+ elsif promoted == "poly_array"
9043+ @needs_rb_value = 1
9044+ @needs_gc = 1
9045+ end
9046+ end
9047+ end
9048+ end
9049+ end
9050+ end
90069051 end
90079052 # Recurse via the centralized child walker (push_child_ids covers
90089053 # the full set of AST slots — visiting a few extra slots is a