Commit db381f3d

db381f3dd889Merged

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

Description

Interpolation of hashes and empty array literals

#{hash} interpolates via the hash variant's inspect, and a bare empty array literal #{[]} renders "[]".

Co-Authored-By: Claude Opus 4.8 (1M context) [email protected]

Details

Commit
db381f3dd889
Parent
ffc1416ac3b1

src/codegen.cmodifiedView file

@@ -3149,11 +3149,22 @@
31493149 buf_puts(&fmt, "%s"); buf_printf(&argbuf, "sp_%s_to_s(", c->classes[ty_object_class(t)].name);
31503150 EMIT_IV(); buf_puts(&argbuf, ")");
31513151 }
3152+ else if (ty_is_hash(t) && ty_hash_cname(t)) {
3153+ buf_puts(&fmt, "%s"); buf_printf(&argbuf, "sp_%sHash_inspect(", ty_hash_cname(t));
3154+ EMIT_IV(); buf_puts(&argbuf, ")");
3155+ }
3156+ else if (t == TY_UNKNOWN && ety && !strcmp(ety, "ArrayNode") &&
3157+ (nt_arr(nt, expr, "elements", (int[]){0}), 1)) {
3158+ /* a bare empty array literal interpolates as "[]" */
3159+ int en = 0; nt_arr(nt, expr, "elements", &en);
3160+ if (en == 0) { buf_puts(&fmt, "%s"); buf_puts(&argbuf, "\"[]\""); }
3161+ else { free(fmt.p); free(argbuf.p); unsupported(c, pid, "interpolation value"); }
3162+ }
31523163 else {
31533164 free(fmt.p); free(argbuf.p);
31543165 unsupported(c, pid, "interpolation value");
3155- #undef EMIT_IV
31563166 }
3167+ #undef EMIT_IV
31573168 nargs++;
31583169 }
31593170 else {