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
@needs_rb_value = 1 return "poly_array" end # `String.new` (and locals widened to mutable_str via `s = ""; s << ...`) # produces sp_String* values. Lower an all-mutable_str literal to a # mutable_str_ptr_array — a sp_PtrArray of sp_String* — so the generic # `<X>_ptr_array` codegen path (length/push/pop/[]) works without # truncating the pointer to int. Issue #519: previously fell through # to the int_array default and `sp_IntArray_push(arr, sp_String_new(""))` # failed the int-from-pointer check. if et == "mutable_str" all_same = 1 k = 1 while k < elems.length if infer_type(elems[k]) != "mutable_str" all_same = 0 end k = k + 1 end if all_same == 1 @needs_gc = 1 return "mutable_str_ptr_array" end @needs_gc = 1 @needs_rb_value = 1 return "poly_array" end # Check if elements have mixed types k = 1 while k < elems.lengthspinel_codegen.rbmodifiedView file
Loading diff…
test/string_new_array_literal.rbaddedView file
Loading diff…
test/string_new_array_literal.rb.expectedaddedView file
Loading diff…