Commit 844356c1

844356c115a3Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 1 file · ~1 modified

Description

Handle poly object_id and computed-receiver poly array element write

  • object_id on a poly receiver cast the sp_RbVal aggregate to uintptr_t; read the boxed payload (.v.p) instead (entry.object_id in PPU#setup_lut).
  • the int-key poly []= wrote widen_and_set's result back into the receiver, but a computed receiver (@nmt_mem[bank][idx]=v) is not an lvalue. Only write back for a simple local/ivar receiver; otherwise rely on in-place mutation. Coerce the index to int.

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

Details

Commit
844356c115a3
Parent
93ded8ff43d7

src/codegen_call.cmodifiedView file

@@ -3812,9 +3812,23 @@
38123812 buf_puts(b, ", "); emit_expr(c, argv[0], b);
38133813 }
38143814 else if (at == TY_INT) {
3815- emit_expr(c, recv, b);
3816- buf_puts(b, " = sp_poly_arr_widen_and_set("); emit_expr(c, recv, b);
3817- buf_puts(b, ", "); emit_expr(c, argv[0], b);
3815+ /* widen_and_set only returns a *different* boxed value when an IntArray is
3816+ promoted to a PolyArray; otherwise it mutates in place. We can only
3817+ write the result back when the receiver is a simple assignable lvalue
3818+ (a local or ivar). For a computed receiver (e.g. @nmt_mem[bank][idx]=v,
3819+ where the receiver is itself an array element), drop the write-back and
3820+ rely on in-place mutation. */
3821+ const char *rcvty = nt_type(nt, recv);
3822+ int recv_is_lvalue = rcvty && (!strcmp(rcvty, "LocalVariableReadNode") ||
3823+ !strcmp(rcvty, "InstanceVariableReadNode"));
3824+ if (recv_is_lvalue) {
3825+ emit_expr(c, recv, b);
3826+ buf_puts(b, " = sp_poly_arr_widen_and_set("); emit_expr(c, recv, b);
3827+ }
3828+ else {
3829+ buf_puts(b, "sp_poly_arr_widen_and_set("); emit_expr(c, recv, b);
3830+ }
3831+ buf_puts(b, ", "); emit_int_expr(c, argv[0], b);
38183832 }
38193833 else {
38203834 buf_printf(b, "sp_poly_set_poly("); emit_expr(c, recv, b);
@@ -3952,6 +3966,8 @@
39523966 if (rt == TY_INT) { buf_puts(b, "(2*("); emit_expr(c, recv, b); buf_puts(b, ")+1)"); }
39533967 else if (rt == TY_SYMBOL) { buf_puts(b, "((mrb_int)("); emit_expr(c, recv, b); buf_puts(b, ")*2)"); }
39543968 else if (rt == TY_BOOL || rt == TY_NIL) { buf_puts(b, "((void)("); emit_expr(c, recv, b); buf_puts(b, "), 0)"); }
3969+ /* a boxed value: its identity is the boxed payload (heap pointer / int) */
3970+ else if (rt == TY_POLY) { buf_puts(b, "((mrb_int)(uintptr_t)("); emit_expr(c, recv, b); buf_puts(b, ").v.p)"); }
39553971 else { buf_puts(b, "((mrb_int)(uintptr_t)("); emit_expr(c, recv, b); buf_puts(b, "))"); }
39563972 return;
39573973 }