Commit bbfed34f

bbfed34f4bc0Merged

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

Description

codegen: skip poly to_s/inspect fast-path when user classes define the method

When any user class defines to_s or inspect, the poly receiver dispatch must fall through to the per-class switch so user-defined methods are called instead of the generic sp_poly_to_s which returns empty string for user objects.

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

Details

Commit
bbfed34f4bc0
Parent
5a091cfa5748

src/codegen.cmodifiedView file

@@ -3928,8 +3928,13 @@
39283928 if (recv >= 0 && rt == TY_POLY && argc == 0) {
39293929 if (!strcmp(name, "nil?")) { buf_puts(b, "sp_poly_nil_p("); emit_expr(c, recv, b); buf_puts(b, ")"); return; }
39303930 if (!strcmp(name, "to_s") || !strcmp(name, "inspect")) {
3931- buf_printf(b, "%s(", !strcmp(name, "to_s") ? "sp_poly_to_s" : "sp_poly_inspect");
3932- emit_expr(c, recv, b); buf_puts(b, ")"); return;
3931+ int has_user_method = 0;
3932+ for (int k = 0; k < c->nclasses; k++)
3933+ if (comp_method_in_chain(c, k, name, NULL) >= 0) { has_user_method = 1; break; }
3934+ if (!has_user_method) {
3935+ buf_printf(b, "%s(", !strcmp(name, "to_s") ? "sp_poly_to_s" : "sp_poly_inspect");
3936+ emit_expr(c, recv, b); buf_puts(b, ")"); return;
3937+ }
39333938 }
39343939 if (!strcmp(name, "to_i")) { buf_puts(b, "sp_poly_to_i("); emit_expr(c, recv, b); buf_puts(b, ")"); return; }
39353940 if (!strcmp(name, "to_f")) { buf_puts(b, "sp_poly_to_f("); emit_expr(c, recv, b); buf_puts(b, ")"); return; }