Commit 85e22fcb

85e22fcba902Merged

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

Description

fix(analyze): empty Array literal pushed with :ptr promotes to ptr_ptr_array

arr = []; arr.push(<:ptr value>) left arr typed as int_array (the empty literal's default), so sp_IntArray_push(arr, <void *>) warned -Wint-conversion and silently round-tripped FFI pointers through mrb_int. Companion shape to #492 (which covered the [ptr, ptr, ...] literal case at construction time).

  • analyze: empty_array_promotion_for gets a "ptr" -> "ptr_ptr_array" arm. The scan_locals push handler that walks local LVs also picks up the new element type and widens arr's slot from int_array to ptr_ptr_array on the first :ptr push.

The promoted array compiles through the existing is_ptr_array_type codegen path (sp_PtrArray_new_noscan for raw ptr elems).

Tests: default 459/0/0, promote 459/0/0, optcarrot checksum 59662.

Closes #688

Co-Authored-By: Claude Opus 4.7 [email protected]

Details

Commit
85e22fcba902
Parent
b54d5d5020f3

spinel_analyze.rbmodifiedView file

@@ -16141,6 +16141,12 @@
1614116141 @needs_rb_value = 1
1614216142 return "poly_array"
1614316143 end
16144+ if elem_acc[0] == "ptr"
16145+ # Raw `:ptr` (FFI handles, dlopen returns, ...) elements: use the
16146+ # noscan PtrArray so the collector treats the slots as opaque
16147+ # word-sized values without walking element headers. Issue #688.
16148+ return "ptr_ptr_array"
16149+ end
1614416150 if is_obj_type(elem_acc[0]) == 1
1614516151 # Homogeneous obj array — use a typed `<obj>_ptr_array` so reads
1614616152 # return a typed pointer (no sp_RbVal unbox needed at the call
@@ -25608,6 +25614,22 @@
2560825614 end
2560925615 ki = ki + 1
2561025616 end
25617+ elsif arg_type == "ptr"
25618+ # Raw :ptr (FFI handles, dlopen returns, ...). The empty
25619+ # `[]` default of int_array can't hold opaque pointer-sized
25620+ # values without -Wint-conversion; promote to the noscan
25621+ # ptr_ptr_array so reads / pushes carry the void *. Issue
25622+ # #688.
25623+ @needs_gc = 1
25624+ ki = 0
25625+ while ki < names.length
25626+ if names[ki] == arr_name
25627+ if types[ki] == "int_array"
25628+ types[ki] = "ptr_ptr_array"
25629+ end
25630+ end
25631+ ki = ki + 1
25632+ end
2561125633 elsif is_hash_type(arg_type) == 1 || is_array_type(arg_type) == 1 || is_ptr_array_type(arg_type) == 1
2561225634 # Hash or typed-array element: spinel has no
2561325635 # <hash>_ptr_array / <array>_ptr_array_ptr_array slot,