Commit b7be1300

b7be1300eeadMerged

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

Description

codegen: empty [] literal uses g_ret_type context for array kind

When an empty ArrayNode reaches emit_expr in a return position (e.g. fresh.empty? ? [] : fresh as method tail), the node's cached type is TY_UNKNOWN because analyze has no elements to infer from. Before falling back to sp_IntArray_new(), check g_ret_type: if the method returns a typed array, use that kind instead.

Fixes poly_each_receiver_gc_root (method returning sp_PolyArray* was emitting sp_IntArray_new() for the empty branch).

Co-Authored-By: Claude Sonnet 4.6 [email protected]

Details

Commit
b7be1300eead
Parent
4c45b7974ede

src/codegen.cmodifiedView file

@@ -6017,7 +6017,9 @@
60176017 TyKind at = comp_ntype(c, id);
60186018 /* an empty `[]` literal carries no element type of its own; it is
60196019 emitted via the target's type in emit_assign. If we reach here for
6020- an empty literal, fall back to an int array. */
6020+ an empty literal, use g_ret_type context (e.g. tail position in a
6021+ poly_array-returning method) before falling back to int array. */
6022+ if (n == 0 && at == TY_UNKNOWN && ty_is_array(g_ret_type)) at = g_ret_type;
60216023 const char *k = array_kind(at);
60226024 if (n == 0 && !k && at != TY_POLY_ARRAY) { buf_puts(b, "sp_IntArray_new()"); return; }
60236025 /* poly (mixed-element) array: build an sp_PolyArray of boxed elements */