Commit 5a091cfa
5a091cfa5748Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 1 file · ~1 modified
Description
Skip fast poly-[] path when user classes define their own []
When no user class defines [], the single-call sp_poly_get_sym / sp_poly_get_str / sp_poly_arr_get helpers are the right fast path. But when user classes do define [], the fast path intercepts the call before the per-class poly dispatch fires, so user-defined [] methods were never called — the receiver was silently treated as a hash.
Guard the fast path on has_user_aref so calls fall through to the per-class switch that properly handles both user methods and builtin hash/array arms.
Co-Authored-By: Claude Sonnet 4.6 [email protected]
Details
- Commit
- 5a091cfa5748
- Parent
- cf9ece6d0f65
src/codegen.cmodifiedView file
@@ -3964,20 +3964,28 @@
39643964
/* poly receiver: [] with symbol or string key -> runtime dispatch */39653965
if (recv >= 0 && rt == TY_POLY && !strcmp(name, "[]") && argc == 1) {39663966
TyKind at = comp_ntype(c, argv[0]);3967-
if (at == TY_SYMBOL) {3968-
buf_puts(b, "sp_poly_get_sym("); emit_expr(c, recv, b);3969-
buf_puts(b, ", "); emit_expr(c, argv[0], b); buf_puts(b, ")");3970-
return;3971-
}3972-
if (at == TY_STRING) {3973-
buf_puts(b, "sp_poly_get_str("); emit_expr(c, recv, b);3974-
buf_puts(b, ", "); emit_expr(c, argv[0], b); buf_puts(b, ")");3975-
return;3976-
}3977-
if (at == TY_INT) {3978-
buf_puts(b, "sp_poly_arr_get("); emit_expr(c, recv, b);3979-
buf_puts(b, ", "); emit_expr(c, argv[0], b); buf_puts(b, ")");3980-
return;3967+
/* Only use the fast single-call path when no user class defines [].3968+
If any user class has its own [] method, fall through to the per-class3969+
poly dispatch (line ~4640) which generates both user and builtin arms. */3970+
int has_user_aref = 0;3971+
for (int k = 0; k < c->nclasses; k++)3972+
if (comp_method_in_chain(c, k, "[]", NULL) >= 0) { has_user_aref = 1; break; }3973+
if (!has_user_aref) {3974+
if (at == TY_SYMBOL) {3975+
buf_puts(b, "sp_poly_get_sym("); emit_expr(c, recv, b);3976+
buf_puts(b, ", "); emit_expr(c, argv[0], b); buf_puts(b, ")");3977+
return;3978+
}3979+
if (at == TY_STRING) {3980+
buf_puts(b, "sp_poly_get_str("); emit_expr(c, recv, b);3981+
buf_puts(b, ", "); emit_expr(c, argv[0], b); buf_puts(b, ")");3982+
return;3983+
}3984+
if (at == TY_INT) {3985+
buf_puts(b, "sp_poly_arr_get("); emit_expr(c, recv, b);3986+
buf_puts(b, ", "); emit_expr(c, argv[0], b); buf_puts(b, ")");3987+
return;3988+
}39813989
}39823990
}39833991
/* poly receiver: join */