Commit 95a7e0e5
95a7e0e5e69dMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 1 file · ~1 modified
Description
Array#[]= in expression position
arr[i] = v used as an rvalue (e.g. b = arr[i] = v) now performs the
store and evaluates to the assigned value, matching Ruby's []= return.
The receiver, index, and value are each evaluated once. Covers typed and
poly arrays; the statement form is unchanged.
Co-Authored-By: Claude Opus 4.8 (1M context) [email protected]
Details
- Commit
- 95a7e0e5e69d
- Parent
- 3b79a9421070
src/codegen.cmodifiedView file
@@ -1912,6 +1912,24 @@
19121912
}19131913
}19141914
1915+
/* `arr[i] = v` in expression position: do the store, evaluate to the rhs1916+
(Ruby []= returns the assigned value). The statement form is emitted1917+
elsewhere; this covers rvalue chains like `b = arr[i] = v`. */1918+
if (recv >= 0 && ty_is_array(rt) && !strcmp(name, "[]=") && argc == 2) {1919+
const char *k = (rt == TY_POLY_ARRAY) ? "Poly" : array_kind(rt);1920+
if (k) {1921+
int t = ++g_tmp, ti = ++g_tmp, tv = ++g_tmp;1922+
buf_printf(b, "({ sp_%sArray *_t%d = ", k, t); emit_expr(c, recv, b);1923+
buf_printf(b, "; mrb_int _t%d = ", ti); emit_expr(c, argv[0], b); buf_puts(b, "; ");1924+
if (rt == TY_POLY_ARRAY) {1925+
buf_printf(b, "sp_RbVal _t%d = ", tv); emit_boxed(c, argv[1], b);1926+
}1927+
else { emit_ctype(c, ty_array_elem(rt), b); buf_printf(b, " _t%d = ", tv); emit_expr(c, argv[1], b); }1928+
buf_printf(b, "; sp_%sArray_set(_t%d, _t%d, _t%d); _t%d; })", k, t, ti, tv, tv);1929+
return;1930+
}1931+
}1932+
19151933
/* array value methods */19161934
if (recv >= 0 && ty_is_array(rt)) {19171935
/* values_at(i, j, ...) -> fresh same-kind array of the picked elements