Commit 02462f86
02462f86b48dMerged
Yusuke Endoh <[email protected]> · 6 months ago · 3 files · +2 added · ~1 modified
Description
fix(codegen): infer + lower <X>_ptr_array.first/.last element type
compile_bracket_assign already had a same-prefix slice-assign path
for int_array[i, n] = int_array, but the rhs form <arr>.first
(or .last) returning the element of a typed ptr_array was
untyped: infer_type's .first / .last arm fell through to the
default "int" for any receiver outside the str_array / sym_array
/ float_array set, so the slice-assign branch's
infer_type(arg_ids[2]) == "int_array" test missed and the call
silently lowered to arr[start] = len (using arg_ids[1] as the
value, dropping arg_ids[2] entirely).
Two coordinated changes:
-
infer_typefor.first/.last: extend the special-case chain withis_ptr_array_type(rt) → ptr_array_elem_type(rt)andpoly_array → poly.<int_array>_ptr_array.firstnow types asint_array, so the slice-assign branch fires. -
compile_array_method_exprfor ptr_array.first/.last: add the lowering (cast throughsp_PtrArray_get) mirroring the existing[]arm. Without this, the inference fix would leave the call unresolved at compile time.
Reproducer:
@banks = (0...n).map { buf.slice!(0, 8) }
@ref = [0] * 24
@ref[5, 8] = @banks.first
@ref[15, 8] = @banks.last
Pre-fix: @ref[5] and @ref[15] each got the literal 8. Post-fix:
the 8-element copy lands at the right offsets.
Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]
Details
- Commit
- 02462f86b48d
- Parent
- 9153a62f417c
spinel_codegen.rbmodifiedView file
if rt == "float_array" return "float" end # `<X>_ptr_array.first` / `.last` returns an `<X>` (e.g. # `int_array_ptr_array.first` → `int_array`). Without this, # downstream typed-array consumers (notably the slice-assign # `arr[i, n] = banks.first` path in compile_bracket_assign, # which needs `infer_type(arg_ids[2]) == "int_array"` to fire) # see "int" and silently fall through to element-assign, # silently lowering `arr[i, n] = src` to `arr[i] = n`. if is_ptr_array_type(rt) == 1 return ptr_array_elem_type(rt) end if rt == "poly_array" return "poly" end end return "int" end if mname == "[]" return "((" + ct + ")sp_PtrArray_get(" + rc + ", " + compile_arg0_as_int(nid) + "))" end if mname == "first" return "((" + ct + ")sp_PtrArray_get(" + rc + ", 0))" end if mname == "last" return "((" + ct + ")sp_PtrArray_get(" + rc + ", sp_PtrArray_length(" + rc + ") - 1))" end if mname == "push" return "(sp_PtrArray_push(" + rc + ", " + compile_arg0(nid) + "), 0)" endtest/intarray_slice_assign_from_intarray_ptr_array_first.rbaddedView file
Loading diff…
test/intarray_slice_assign_from_intarray_ptr_array_first.rb.expectedaddedView file
Loading diff…