Commit 62f0fa42

62f0fa423d6fMerged

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

Description

Operator methods on objects + nested object-receiver g_pre fix

A user class defining an arithmetic/bitwise/unary operator (def %, def |, def -@, ...) now dispatches to that method: the int-arith and unary emission arms skip object receivers so the object method dispatch runs.

Also fixed a g_pre re-entrancy bug: when an object-method receiver had to be stashed in a temp, the temp's declaration prefix was written before the (possibly nested) receiver expression flushed its own setup, interleaving the two. The receiver is now emitted first.

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

Details

Commit
62f0fa423d6f
Parent
74709165bb61

src/codegen.cmodifiedView file

@@ -1236,7 +1236,7 @@
12361236 }
12371237 }
12381238
1239- if ((!strcmp(name, "-@") || !strcmp(name, "+@")) && recv >= 0 && argc == 0) {
1239+ if ((!strcmp(name, "-@") || !strcmp(name, "+@")) && recv >= 0 && argc == 0 && !ty_is_object(rt)) {
12401240 buf_puts(b, name[0] == '-' ? "(-" : "(+");
12411241 emit_expr(c, recv, b); buf_puts(b, ")");
12421242 return;
@@ -1273,7 +1273,7 @@
12731273 }
12741274 }
12751275
1276- if (recv >= 0 && argc == 1 && int_arith_fn(name)) {
1276+ if (recv >= 0 && argc == 1 && int_arith_fn(name) && !ty_is_object(rt)) {
12771277 if (rt == TY_STRING && !strcmp(name, "+")) {
12781278 buf_puts(b, "sp_str_concat(");
12791279 emit_expr(c, recv, b); buf_puts(b, ", "); emit_expr(c, argv[0], b);
@@ -1654,10 +1654,12 @@
16541654 }
16551655 else {
16561656 int t = ++g_tmp;
1657+ /* emit the receiver first so any setup it pushes into g_pre is fully
1658+ flushed before we write this temp's declaration line */
1659+ Buf rb; memset(&rb, 0, sizeof rb); emit_expr(c, recv, &rb);
16571660 emit_indent(g_pre, g_indent);
16581661 emit_ctype(c, rt, g_pre);
16591662 buf_printf(g_pre, " _t%d = ", t);
1660- Buf rb; memset(&rb, 0, sizeof rb); emit_expr(c, recv, &rb);
16611663 buf_puts(g_pre, rb.p ? rb.p : ""); buf_puts(g_pre, ";\n"); free(rb.p);
16621664 snprintf(selfptr, sizeof selfptr, "_t%d", t);
16631665 }