Commit dfa83a22

dfa83a22bd62Merged

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

Description

codegen: unbox the receiver for a reopened-primitive method in poly dispatch

A poly value dispatching to a method reopened on a primitive (Integer/Float/ String/Symbol) emitted sp_Integer_to_pair((sp_Integer *)_t.v.p) -- but sp_Integer is not a struct and the function takes the unboxed value. Read the matching union field (.v.i / .v.f / .v.s, sp_sym) as the receiver instead. Fixes class_method_open_class_call.

Co-Authored-By: Claude Opus 4.8 [email protected]

Details

Commit
dfa83a22bd62
Parent
275f5e3bee74

src/codegen_call.cmodifiedView file

@@ -8248,12 +8248,21 @@
82488248 /* Build the call; append default values for any optional params
82498249 not provided by the (zero-arg) call site. */
82508250 Buf cb; memset(&cb, 0, sizeof cb);
8251- buf_printf(&cb, "sp_%s_%s((sp_%s *)_t%d.v.p",
8252- c->classes[defcls].name, mc(c->scopes[mi].name), c->classes[defcls].name, tv);
8251+ /* A reopened primitive (Integer/Float/String/Symbol) method takes the
8252+ unboxed value, not a struct pointer -- read the matching union field
8253+ instead of casting .v.p to a non-existent sp_<Prim> struct. */
8254+ const char *_dcn = c->classes[defcls].name;
8255+ char _dself[64];
8256+ if (!strcmp(_dcn, "Integer") || !strcmp(_dcn, "Numeric")) snprintf(_dself, sizeof _dself, "_t%d.v.i", tv);
8257+ else if (!strcmp(_dcn, "Float")) snprintf(_dself, sizeof _dself, "_t%d.v.f", tv);
8258+ else if (!strcmp(_dcn, "String")) snprintf(_dself, sizeof _dself, "_t%d.v.s", tv);
8259+ else if (!strcmp(_dcn, "Symbol")) snprintf(_dself, sizeof _dself, "(sp_sym)_t%d.v.i", tv);
8260+ else snprintf(_dself, sizeof _dself, "(sp_%s *)_t%d.v.p", _dcn, tv);
8261+ buf_printf(&cb, "sp_%s_%s(%s", _dcn, mc(c->scopes[mi].name), _dself);
82538262 if (c->scopes[mi].nparams > 0) {
82548263 const char *saved_self = g_self;
82558264 static char selfpbuf[64];
8256- snprintf(selfpbuf, sizeof selfpbuf, "(sp_%s *)_t%d.v.p", c->classes[defcls].name, tv);
8265+ snprintf(selfpbuf, sizeof selfpbuf, "%s", _dself);
82578266 g_self = selfpbuf;
82588267 for (int a = 0; a < c->scopes[mi].nparams; a++) {
82598268 buf_puts(&cb, ", "); emit_arg_or_default(c, &c->scopes[mi], a, -1, &cb);