Commit 0ac0e865
0ac0e8657b40Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 1 file · ~1 modified
Description
Coerce poly operand when pushing into a typed array
arr.push(x) / arr << x / arr.append(x) on a typed array (str/int/float)
emitted sp_<Kind>Array_push(arr, x) with no coercion, so a poly x (which
holds the element type at runtime) produced an incompatible-pointer error in
the generated C. Coerce poly -> element type via sp_poly_to_s / sp_poly_to_i /
sp_poly_to_f, matching the existing arr[i] = x ([]=) coercion. Applied to
both the expression-form push (returns the array) and the statement-form push.
Surfaced compiling the legacy compiler: type-tag strings flow through methods whose return inference is poly, so arrays of those strings receive poly pushes. Emitted-C type errors on the generated legacy compiler: 35 -> 7.
Gates: 845 test, 57 bench, optcarrot checksum 59662 -- all green.
Co-Authored-By: Claude Opus 4.8 (1M context) [email protected]
Details
- Commit
- 0ac0e8657b40
- Parent
- e29cda4cb6be
src/codegen_call.cmodifiedView file
int t = ++g_tmp; buf_puts(b, "({ "); emit_ctype(c, art, b); buf_printf(b, " _t%d = ", t); emit_expr(c, recv, b); buf_puts(b, "; "); TyKind elem = ty_array_elem(art); for (int a = 0; a < argc; a++) { buf_printf(b, "sp_%sArray_push(_t%d, ", k, t); if (art == TY_POLY_ARRAY) emit_boxed(c, argv[a], b); else emit_expr(c, argv[a], b); if (art == TY_POLY_ARRAY) emit_boxed(c, argv[a], b); else if (comp_ntype(c, argv[a]) == TY_POLY && elem == TY_STRING) { /* a poly value (holds a string at runtime) into a str_array: coerce */ buf_puts(b, "sp_poly_to_s("); emit_expr(c, argv[a], b); buf_puts(b, ")"); } else if (comp_ntype(c, argv[a]) == TY_POLY && elem == TY_INT) { buf_puts(b, "sp_poly_to_i("); emit_expr(c, argv[a], b); buf_puts(b, ")"); } else if (comp_ntype(c, argv[a]) == TY_POLY && elem == TY_FLOAT) { buf_puts(b, "sp_poly_to_f("); emit_expr(c, argv[a], b); buf_puts(b, ")"); } else emit_expr(c, argv[a], b); buf_puts(b, "); "); } buf_printf(b, "_t%d; })", t); return 1; } if ((!strcmp(name, "push") || !strcmp(name, "<<") || !strcmp(name, "append")) && argc >= 1) { TyKind et = ty_array_elem(rt); for (int a = 0; a < argc; a++) { emit_indent(b, indent); buf_printf(b, "sp_%sArray_push(", k); emit_expr(c, recv, b); buf_puts(b, ", "); emit_expr(c, argv[a], b); buf_puts(b, ");\n"); /* coerce a poly value (holds the element type at runtime) to the typed array's element representation */ TyKind vt = comp_ntype(c, argv[a]); if (vt == TY_POLY && et == TY_STRING) { buf_puts(b, "sp_poly_to_s("); emit_expr(c, argv[a], b); buf_puts(b, ")"); } else if (vt == TY_POLY && et == TY_INT) { buf_puts(b, "sp_poly_to_i("); emit_expr(c, argv[a], b); buf_puts(b, ")"); } else if (vt == TY_POLY && et == TY_FLOAT) { buf_puts(b, "sp_poly_to_f("); emit_expr(c, argv[a], b); buf_puts(b, ")"); } else emit_expr(c, argv[a], b); buf_puts(b, ");\n"); } return 1; }