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.
-
is_sized_empty_array_defaultwas 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 incompile_expr_for_expected_type,emit_constructor, andcompile_stmtall multiply by N (the*multiplier) and fill with NULL/0. So[nil, nil] * Nwould emitlength=Ninstead oflength=2*N, and[1] * Nwould emit N zeros instead of N ones — silent value loss. Restricted to the canonical[nil] * Nand[0] * Nshapes only. -
Missing
@needs_gc = 1on ptr_array promotion. The<</pushand[]=widening branches set@needs_gconly when promoting topoly_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. Addedis_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
return 0 end recv_elems = parse_id_list(@nd_elements[arr_recv]) if recv_elems.length == 0 # Restrict to single-element [nil] or [0] literals — the emit # paths in compile_expr_for_expected_type / emit_constructor / # compile_stmt all multiply by N (the multiplier) and fill with # NULL/0, so a multi-element literal would produce length=N # instead of length=N*elements, and a non-zero integer literal # would silently lose its value. The Gemini review on PR #241 # caught both gaps. Stay tight: only the canonical # `[nil] * N` and `[0] * N` shapes. if recv_elems.length != 1 return 0 end ei = 0 while ei < recv_elems.length ti = recv_elems[ei] if ti < 0 return 0 end if @nd_type[ti] != "NilNode" && @nd_type[ti] != "IntegerNode" return 0 end ei = ei + 1 ti = recv_elems[0] if ti < 0 return 0 end if @nd_type[ti] == "NilNode" return 1 end 1 if @nd_type[ti] == "IntegerNode" && @nd_value[ti].to_i == 0 return 1 end 0 end def base_type(t) elsif promoted == "poly_array" @needs_rb_value = 1 @needs_gc = 1 elsif is_ptr_array_type(promoted) == 1 # ptr_array slots hold object pointers, so the # owning class needs gc_scan emitted; without # this flag the captured pointers leak and the # collector misses them. Gemini #241 review. @needs_gc = 1 end end end elsif promoted == "poly_array" @needs_rb_value = 1 @needs_gc = 1 elsif is_ptr_array_type(promoted) == 1 # ptr_array slots hold object pointers, so the # owning class needs gc_scan emitted; without # this flag the captured pointers leak and the # collector misses them. Gemini #241 review. @needs_gc = 1 end end end