Commit 82032962
820329620540Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 1 file · ~1 modified
Description
codegen: fix default empty [] param to use parameter's array type
emit_arg_or_default already handled the provided-arg case (e.g. f([])) by using the param's type to pick sp_PolyArray_new() vs sp_IntArray_new(). Apply the same logic to default-expression empty arrays so that Node.new (no args, default children=[]) passes sp_PolyArray_new() when the param type is TY_POLY_ARRAY.
Co-Authored-By: Claude Sonnet 4.6 [email protected]
Details
- Commit
- 820329620540
- Parent
- 26f137a906ae
src/codegen.cmodifiedView file
@@ -1554,8 +1554,18 @@
15541554
else buf_puts(out, pt == TY_RANGE ? "(sp_Range){0}" : default_value(pt));15551555
}15561556
else if (pt == TY_POLY) emit_boxed(c, dv, out);1557-
else1558-
emit_expr(c, dv, out);1557+
else {1558+
/* Default empty `[]` literal: emit the correct array constructor for1559+
the parameter type rather than always sp_IntArray_new(). */1560+
int den = 0;1561+
int is_empty_arr_dv = dty && !strcmp(dty, "ArrayNode") &&1562+
(nt_arr(c->nt, dv, "elements", &den), den == 0);1563+
if (is_empty_arr_dv && ty_is_array(pt) && pt != TY_INT_ARRAY) {1564+
if (pt == TY_POLY_ARRAY) buf_puts(out, "sp_PolyArray_new()");1565+
else { const char *k = array_kind(pt); if (k) buf_printf(out, "sp_%sArray_new()", k); else emit_expr(c, dv, out); }1566+
}1567+
else emit_expr(c, dv, out);1568+
}15591569
}15601570
15611571
/* Emit a comma-separated argument list filling defaults for omitted