Commit 4c9077b4

4c9077b40653Merged

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

Description

codegen: fix setter dispatch for tail-position and base-class refs

Two setter dispatch gaps fixed:

  1. A setter call (obj.x = val) at tail position in a non-void method fell through emit_stmt_tail_inner to emit_expr, which can't handle it. Treat it as a side-effect statement so the mutation happens and the method returns the default value.

  2. When the receiver's static type is a base class that doesn't own the writer (e.g. a class method that calls new and then sets an attr on the result), emit a cls_id switch over all subclasses that do own the writer. Guard with comp_method_in_chain < 0 to avoid firing when an explicit setter method exists (those are dispatched by emit_expr).

Co-Authored-By: Claude Sonnet 4.6 [email protected]

Details

Commit
4c9077b40653
Parent
1b24399daca9

src/codegen.cmodifiedView file

@@ -14221,9 +14221,9 @@
1422114221 char base[256];
1422214222 if (ln - 1 < sizeof base) {
1422314223 memcpy(base, nm, ln - 1); base[ln - 1] = '\0';
14224+ int args = nt_ref(nt, id, "arguments");
14225+ int an = 0; const int *argv = args >= 0 ? nt_arr(nt, args, "arguments", &an) : NULL;
1422414226 if (comp_writer_in_chain(c, ty_object_class(rt), base, NULL)) {
14225- int args = nt_ref(nt, id, "arguments");
14226- int an = 0; const int *argv = args >= 0 ? nt_arr(nt, args, "arguments", &an) : NULL;
1422714227 if (an >= 1) {
1422814228 int rc = ty_object_class(rt);
1422914229 char ivn[256]; snprintf(ivn, sizeof ivn, "@%s", base);
@@ -14238,6 +14238,37 @@
1423814238 return;
1423914239 }
1424014240 }
14241+ else if (comp_method_in_chain(c, ty_object_class(rt), nm, NULL) < 0) {
14242+ /* writer not in chain and no explicit method: try subclass dispatch via cls_id */
14243+ int ncand = 0;
14244+ for (int k = 0; k < c->nclasses; k++)
14245+ if (comp_is_writer(&c->classes[k], base)) ncand++;
14246+ if (an >= 1 && ncand > 0) {
14247+ TyKind at = comp_ntype(c, argv[0]);
14248+ int tp = ++g_tmp, tval = ++g_tmp;
14249+ emit_indent(b, indent);
14250+ buf_printf(b, "{ sp_%s *_t%d = ", c->classes[ty_object_class(rt)].name, tp);
14251+ emit_expr(c, recv, b); buf_puts(b, "; ");
14252+ emit_ctype(c, at, b); buf_printf(b, " _t%d = ", tval);
14253+ emit_expr(c, argv[0], b); buf_puts(b, ";");
14254+ buf_printf(b, " switch (_t%d->cls_id) {", tp);
14255+ char src[32]; snprintf(src, sizeof src, "_t%d", tval);
14256+ for (int k = 0; k < c->nclasses; k++) {
14257+ if (!comp_is_writer(&c->classes[k], base)) continue;
14258+ char ivn[256]; snprintf(ivn, sizeof ivn, "@%s", base);
14259+ int iv = comp_ivar_index(&c->classes[k], ivn);
14260+ TyKind ivt = iv >= 0 ? c->classes[k].ivar_types[iv] : at;
14261+ if (at != ivt && at != TY_POLY && ivt != TY_POLY) continue;
14262+ buf_printf(b, " case %d: ((sp_%s *)_t%d)->iv_%s = ", k, c->classes[k].name, tp, base);
14263+ if (ivt == TY_POLY && at != TY_POLY) emit_boxed_text(c, at, src, b);
14264+ else if (at == TY_POLY && ivt != TY_POLY) emit_unbox_text(c, ivt, src, b);
14265+ else buf_puts(b, src);
14266+ buf_puts(b, "; break;");
14267+ }
14268+ buf_puts(b, " } }\n");
14269+ return;
14270+ }
14271+ }
1424114272 }
1424214273 }
1424314274 /* poly receiver: switch on cls_id and store into each candidate
@@ -15450,6 +15481,23 @@
1545015481 emit_iteration_stmt(c, id, b, indent))
1545115482 return;
1545215483
15484+ /* setter call at tail position (obj.x = v): side-effect only, no return value.
15485+ A setter name ends in a bare '=' that is not part of ==, !=, <=, >=. */
15486+ if (!strcmp(ty, "CallNode")) {
15487+ const char *_setnm = nt_str(nt, id, "name");
15488+ int _setrecv = nt_ref(nt, id, "receiver");
15489+ size_t _setlen = _setnm ? strlen(_setnm) : 0;
15490+ if (_setrecv >= 0 && _setnm && _setlen >= 2 && _setnm[_setlen - 1] == '=' &&
15491+ _setnm[_setlen - 2] != '=' && _setnm[_setlen - 2] != '!' &&
15492+ _setnm[_setlen - 2] != '<' && _setnm[_setlen - 2] != '>') {
15493+ TyKind _setrt = comp_ntype(c, _setrecv);
15494+ if (ty_is_object(_setrt) || _setrt == TY_POLY) {
15495+ emit_stmt(c, id, b, indent);
15496+ return;
15497+ }
15498+ }
15499+ }
15500+
1545315501 /* string << at tail position: mutate receiver, then return it */
1545415502 if (!strcmp(ty, "CallNode")) {
1545515503 int _srecv = nt_ref(nt, id, "receiver");