Commit b82298af

b82298afb7efMerged

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

Description

fix(codegen): tighten ptr_array empty-default detection + GC flag

Follow-up to #241 — Gemini's two high-priority concerns.

  1. is_sized_empty_array_default was too permissive. The original predicate accepted any array literal of NilNode/IntegerNode elements (including multi-element literals and non-zero ints), but the emit paths in compile_expr_for_expected_type, emit_constructor, and compile_stmt all multiply by N (the * multiplier) and fill with NULL/0. So [nil, nil] * N would emit length=N instead of length=2*N, and [1] * N would emit N zeros instead of N ones — silent value loss. Restricted to the canonical [nil] * N and [0] * N shapes only.

  2. Missing @needs_gc = 1 on ptr_array promotion. The <</push and []= widening branches set @needs_gc only when promoting to poly_array. ptr_array slots also hold object pointers and need gc_scan emitted on the owning class — without the flag, captured pointers leak through GC. Added is_ptr_array_type(promoted) arms to both branches.

295 pass / bootstrap clean.

Refs #241

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

Details

Commit
b82298afb7ef
Parent
84b46acfc919

spinel_codegen.rbmodifiedView file

@@ -4284,21 +4284,28 @@
42844284 return 0
42854285 end
42864286 recv_elems = parse_id_list(@nd_elements[arr_recv])
4287- if recv_elems.length == 0
4287+ # Restrict to single-element [nil] or [0] literals — the emit
4288+ # paths in compile_expr_for_expected_type / emit_constructor /
4289+ # compile_stmt all multiply by N (the multiplier) and fill with
4290+ # NULL/0, so a multi-element literal would produce length=N
4291+ # instead of length=N*elements, and a non-zero integer literal
4292+ # would silently lose its value. The Gemini review on PR #241
4293+ # caught both gaps. Stay tight: only the canonical
4294+ # `[nil] * N` and `[0] * N` shapes.
4295+ if recv_elems.length != 1
42884296 return 0
42894297 end
4290- ei = 0
4291- while ei < recv_elems.length
4292- ti = recv_elems[ei]
4293- if ti < 0
4294- return 0
4295- end
4296- if @nd_type[ti] != "NilNode" && @nd_type[ti] != "IntegerNode"
4297- return 0
4298- end
4299- ei = ei + 1
4298+ ti = recv_elems[0]
4299+ if ti < 0
4300+ return 0
4301+ end
4302+ if @nd_type[ti] == "NilNode"
4303+ return 1
43004304 end
4301- 1
4305+ if @nd_type[ti] == "IntegerNode" && @nd_value[ti].to_i == 0
4306+ return 1
4307+ end
4308+ 0
43024309 end
43034310
43044311 def base_type(t)
@@ -9096,6 +9103,12 @@
90969103 elsif promoted == "poly_array"
90979104 @needs_rb_value = 1
90989105 @needs_gc = 1
9106+ elsif is_ptr_array_type(promoted) == 1
9107+ # ptr_array slots hold object pointers, so the
9108+ # owning class needs gc_scan emitted; without
9109+ # this flag the captured pointers leak and the
9110+ # collector misses them. Gemini #241 review.
9111+ @needs_gc = 1
90999112 end
91009113 end
91019114 end
@@ -9129,6 +9142,12 @@
91299142 elsif promoted == "poly_array"
91309143 @needs_rb_value = 1
91319144 @needs_gc = 1
9145+ elsif is_ptr_array_type(promoted) == 1
9146+ # ptr_array slots hold object pointers, so the
9147+ # owning class needs gc_scan emitted; without
9148+ # this flag the captured pointers leak and the
9149+ # collector misses them. Gemini #241 review.
9150+ @needs_gc = 1
91329151 end
91339152 end
91349153 end