Commit 884f68c1

884f68c19288Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 4 files · +2 added · ~2 modified

Description

fix(analyze,codegen): [String.new] infers as mutable_str_ptr_array

Array literal element-type inference scanned for specific witness types (string / symbol / float / obj_X / proc / lambda) but not mutable_str, so [String.new] (and [s] where s = String.new) fell through to the int_array default. Codegen then emitted sp_IntArray_push(arr, sp_String_new("")) -- an int-from-pointer C compile error.

Add a mutable_str arm in both spinel_analyze.rb and spinel_codegen.rb's infer_array_elem_type_from_ids that lowers an all-mutable_str literal to mutable_str_ptr_array: a sp_PtrArray of sp_String*. The generic <X>_ptr_array codegen path (length / push / pop / [] / first / last / delete_at / empty?) already handles the new slot via is_ptr_array_type without further plumbing. Mixed mutable_str + other types widens to poly_array, mirroring the proc/lambda arm's shape.

The map-block path (xs.map { String.new }) builds its result array via compile_map_expr rather than this helper, so the same fall-through still happens there; left for a separate change.

Closes #519

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

Details

Commit
884f68c19288
Parent
c82a5f6dfd4f

spinel_analyze.rbmodifiedView file

@@ -2421,6 +2421,30 @@
24212421 @needs_rb_value = 1
24222422 return "poly_array"
24232423 end
2424+ # `String.new` (and locals widened to mutable_str via `s = ""; s << ...`)
2425+ # produces sp_String* values. Lower an all-mutable_str literal to a
2426+ # mutable_str_ptr_array — a sp_PtrArray of sp_String* — so the generic
2427+ # `<X>_ptr_array` codegen path (length/push/pop/[]) works without
2428+ # truncating the pointer to int. Issue #519: previously fell through
2429+ # to the int_array default and `sp_IntArray_push(arr, sp_String_new(""))`
2430+ # failed the int-from-pointer check.
2431+ if et == "mutable_str"
2432+ all_same = 1
2433+ k = 1
2434+ while k < elems.length
2435+ if infer_type(elems[k]) != "mutable_str"
2436+ all_same = 0
2437+ end
2438+ k = k + 1
2439+ end
2440+ if all_same == 1
2441+ @needs_gc = 1
2442+ return "mutable_str_ptr_array"
2443+ end
2444+ @needs_gc = 1
2445+ @needs_rb_value = 1
2446+ return "poly_array"
2447+ end
24242448 # Check if elements have mixed types
24252449 k = 1
24262450 while k < elems.length