Commit 14befe65

14befe654c5dMerged

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

Description

User-operator op-assign + Comparable == dispatch

emit_op_assign: when the slot type is a user object and the class defines the binary operator as a method, dispatch to it instead of emitting raw C arithmetic. Fixes b += 5 where b is Box and Box#+ is user-defined.

InstanceVariableOperatorWriteNode: same fix for ivar slots -- @b += 5 inside a method body now dispatches through the user method.

Equality (== / !=) for object receivers: try a direct == method first; if absent, fall back to (<=> dispatch) == 0 so Comparable classes get correct semantics without requiring an explicit == override.

Fixes op_assign_user_operator and multi_assign_ivar_type tests.

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

Details

Commit
14befe654c5d
Parent
a941f7f6cbc0

src/codegen.cmodifiedView file

@@ -4156,6 +4156,58 @@
41564156 return;
41574157 }
41584158 }
4159+ /* object == / != : try direct method, then fall back to <=> == 0 */
4160+ if (recv >= 0 && ty_is_object(rt)) {
4161+ int ecid = ty_object_class(rt);
4162+ int emi = comp_method_in_chain(c, ecid, name, NULL);
4163+ if (emi >= 0) {
4164+ char selfptr[64];
4165+ const char *rty2 = nt_type(nt, recv);
4166+ if (rty2 && (!strcmp(rty2, "LocalVariableReadNode") ||
4167+ !strcmp(rty2, "InstanceVariableReadNode") ||
4168+ !strcmp(rty2, "SelfNode"))) {
4169+ Buf rb; memset(&rb, 0, sizeof rb); emit_expr(c, recv, &rb);
4170+ snprintf(selfptr, sizeof selfptr, "%s", rb.p ? rb.p : "");
4171+ free(rb.p);
4172+ }
4173+ else {
4174+ int t2 = ++g_tmp;
4175+ Buf rb; memset(&rb, 0, sizeof rb); emit_expr(c, recv, &rb);
4176+ emit_indent(g_pre, g_indent);
4177+ emit_ctype(c, rt, g_pre);
4178+ buf_printf(g_pre, " _t%d = %s;\n", t2, rb.p ? rb.p : "");
4179+ free(rb.p);
4180+ snprintf(selfptr, sizeof selfptr, "_t%d", t2);
4181+ }
4182+ emit_dispatch(c, ecid, name, selfptr, nt_ref(nt, id, "arguments"), b);
4183+ return;
4184+ }
4185+ /* no direct == : use <=> == 0 when the class supports Comparable */
4186+ if (comp_method_in_chain(c, ecid, "<=>", NULL) >= 0) {
4187+ char selfptr[64];
4188+ const char *rty2 = nt_type(nt, recv);
4189+ if (rty2 && (!strcmp(rty2, "LocalVariableReadNode") ||
4190+ !strcmp(rty2, "InstanceVariableReadNode") ||
4191+ !strcmp(rty2, "SelfNode"))) {
4192+ Buf rb; memset(&rb, 0, sizeof rb); emit_expr(c, recv, &rb);
4193+ snprintf(selfptr, sizeof selfptr, "%s", rb.p ? rb.p : "");
4194+ free(rb.p);
4195+ }
4196+ else {
4197+ int t3 = ++g_tmp;
4198+ Buf rb; memset(&rb, 0, sizeof rb); emit_expr(c, recv, &rb);
4199+ emit_indent(g_pre, g_indent);
4200+ emit_ctype(c, rt, g_pre);
4201+ buf_printf(g_pre, " _t%d = %s;\n", t3, rb.p ? rb.p : "");
4202+ free(rb.p);
4203+ snprintf(selfptr, sizeof selfptr, "_t%d", t3);
4204+ }
4205+ buf_puts(b, "(");
4206+ emit_dispatch(c, ecid, "<=>", selfptr, nt_ref(nt, id, "arguments"), b);
4207+ buf_printf(b, " %s 0)", eq ? "==" : "!=");
4208+ return;
4209+ }
4210+ }
41594211 unsupported(c, id, "equality");
41604212 }
41614213
@@ -8865,6 +8917,25 @@
88658917 buf_printf(b, "lv_%s %s= ", en, op); emit_expr(c, v, b); buf_puts(b, ";\n");
88668918 return;
88678919 }
8920+ if (ty_is_object(t)) {
8921+ int defcls2 = -1;
8922+ int cid2 = ty_object_class(t);
8923+ int mi2 = comp_method_in_chain(c, cid2, op, &defcls2);
8924+ if (mi2 >= 0) {
8925+ Scope *ms2 = &c->scopes[mi2];
8926+ LocalVar *p2 = ms2->nparams >= 1 ? scope_local(ms2, ms2->pnames[0]) : NULL;
8927+ int atmp2 = ++g_tmp;
8928+ emit_indent(g_pre, g_indent);
8929+ emit_ctype(c, p2 ? p2->type : comp_ntype(c, v), g_pre);
8930+ buf_printf(g_pre, " _t%d = ", atmp2);
8931+ emit_expr(c, v, g_pre);
8932+ buf_puts(g_pre, ";\n");
8933+ buf_printf(b, "lv_%s = sp_%s_%s((sp_%s *)lv_%s, _t%d);\n",
8934+ en, c->classes[defcls2].name, mc(ms2->name),
8935+ c->classes[defcls2].name, en, atmp2);
8936+ return;
8937+ }
8938+ }
88688939 unsupported(c, id, "operator assignment");
88698940 }
88708941
@@ -9863,6 +9934,29 @@
98639934 buf_printf(b, "%s = sp_str_concat(%s, ", ref, ref);
98649935 emit_expr(c, nt_ref(nt, id, "value"), b); buf_puts(b, ");\n");
98659936 }
9937+ else if (op && ty_is_object(vt)) {
9938+ int idefcls = -1;
9939+ int icid = ty_object_class(vt);
9940+ int imi = comp_method_in_chain(c, icid, op, &idefcls);
9941+ if (imi >= 0) {
9942+ Scope *ims = &c->scopes[imi];
9943+ LocalVar *ip = ims->nparams >= 1 ? scope_local(ims, ims->pnames[0]) : NULL;
9944+ int iatmp = ++g_tmp;
9945+ int ival = nt_ref(nt, id, "value");
9946+ emit_indent(g_pre, g_indent);
9947+ emit_ctype(c, ip ? ip->type : comp_ntype(c, ival), g_pre);
9948+ buf_printf(g_pre, " _t%d = ", iatmp);
9949+ emit_expr(c, ival, g_pre);
9950+ buf_puts(g_pre, ";\n");
9951+ buf_printf(b, "%s = sp_%s_%s((sp_%s *)%s, _t%d);\n",
9952+ ref, c->classes[idefcls].name, mc(ims->name),
9953+ c->classes[idefcls].name, ref, iatmp);
9954+ }
9955+ else {
9956+ buf_printf(b, "%s %s= ", ref, op);
9957+ emit_expr(c, nt_ref(nt, id, "value"), b); buf_puts(b, ";\n");
9958+ }
9959+ }
98669960 else {
98679961 buf_printf(b, "%s %s= ", ref, op ? op : "+");
98689962 emit_expr(c, nt_ref(nt, id, "value"), b); buf_puts(b, ";\n");