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

@@ -893,9 +893,21 @@
893893 int t = ++g_tmp;
894894 buf_puts(b, "({ ");
895895 emit_ctype(c, art, b); buf_printf(b, " _t%d = ", t); emit_expr(c, recv, b); buf_puts(b, "; ");
896+ TyKind elem = ty_array_elem(art);
896897 for (int a = 0; a < argc; a++) {
897898 buf_printf(b, "sp_%sArray_push(_t%d, ", k, t);
898- if (art == TY_POLY_ARRAY) emit_boxed(c, argv[a], b); else emit_expr(c, argv[a], b);
899+ if (art == TY_POLY_ARRAY) emit_boxed(c, argv[a], b);
900+ else if (comp_ntype(c, argv[a]) == TY_POLY && elem == TY_STRING) {
901+ /* a poly value (holds a string at runtime) into a str_array: coerce */
902+ buf_puts(b, "sp_poly_to_s("); emit_expr(c, argv[a], b); buf_puts(b, ")");
903+ }
904+ else if (comp_ntype(c, argv[a]) == TY_POLY && elem == TY_INT) {
905+ buf_puts(b, "sp_poly_to_i("); emit_expr(c, argv[a], b); buf_puts(b, ")");
906+ }
907+ else if (comp_ntype(c, argv[a]) == TY_POLY && elem == TY_FLOAT) {
908+ buf_puts(b, "sp_poly_to_f("); emit_expr(c, argv[a], b); buf_puts(b, ")");
909+ }
910+ else emit_expr(c, argv[a], b);
899911 buf_puts(b, "); ");
900912 }
901913 buf_printf(b, "_t%d; })", t);
@@ -8454,11 +8466,19 @@
84548466 return 1;
84558467 }
84568468 if ((!strcmp(name, "push") || !strcmp(name, "<<") || !strcmp(name, "append")) && argc >= 1) {
8469+ TyKind et = ty_array_elem(rt);
84578470 for (int a = 0; a < argc; a++) {
84588471 emit_indent(b, indent);
84598472 buf_printf(b, "sp_%sArray_push(", k);
84608473 emit_expr(c, recv, b); buf_puts(b, ", ");
8461- emit_expr(c, argv[a], b); buf_puts(b, ");\n");
8474+ /* coerce a poly value (holds the element type at runtime) to the typed
8475+ array's element representation */
8476+ TyKind vt = comp_ntype(c, argv[a]);
8477+ if (vt == TY_POLY && et == TY_STRING) { buf_puts(b, "sp_poly_to_s("); emit_expr(c, argv[a], b); buf_puts(b, ")"); }
8478+ else if (vt == TY_POLY && et == TY_INT) { buf_puts(b, "sp_poly_to_i("); emit_expr(c, argv[a], b); buf_puts(b, ")"); }
8479+ else if (vt == TY_POLY && et == TY_FLOAT) { buf_puts(b, "sp_poly_to_f("); emit_expr(c, argv[a], b); buf_puts(b, ")"); }
8480+ else emit_expr(c, argv[a], b);
8481+ buf_puts(b, ");\n");
84628482 }
84638483 return 1;
84648484 }