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:

  1. infer_type for .first / .last: extend the special-case chain with is_ptr_array_type(rt) → ptr_array_elem_type(rt) and poly_array → poly. <int_array>_ptr_array.first now types as int_array, so the slice-assign branch fires.

  2. compile_array_method_expr for ptr_array .first / .last: add the lowering (cast through sp_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

@@ -4052,6 +4052,19 @@
40524052 if rt == "float_array"
40534053 return "float"
40544054 end
4055+ # `<X>_ptr_array.first` / `.last` returns an `<X>` (e.g.
4056+ # `int_array_ptr_array.first` → `int_array`). Without this,
4057+ # downstream typed-array consumers (notably the slice-assign
4058+ # `arr[i, n] = banks.first` path in compile_bracket_assign,
4059+ # which needs `infer_type(arg_ids[2]) == "int_array"` to fire)
4060+ # see "int" and silently fall through to element-assign,
4061+ # silently lowering `arr[i, n] = src` to `arr[i] = n`.
4062+ if is_ptr_array_type(rt) == 1
4063+ return ptr_array_elem_type(rt)
4064+ end
4065+ if rt == "poly_array"
4066+ return "poly"
4067+ end
40554068 end
40564069 return "int"
40574070 end
@@ -25831,6 +25844,12 @@
2583125844 if mname == "[]"
2583225845 return "((" + ct + ")sp_PtrArray_get(" + rc + ", " + compile_arg0_as_int(nid) + "))"
2583325846 end
25847+ if mname == "first"
25848+ return "((" + ct + ")sp_PtrArray_get(" + rc + ", 0))"
25849+ end
25850+ if mname == "last"
25851+ return "((" + ct + ")sp_PtrArray_get(" + rc + ", sp_PtrArray_length(" + rc + ") - 1))"
25852+ end
2583425853 if mname == "push"
2583525854 return "(sp_PtrArray_push(" + rc + ", " + compile_arg0(nid) + "), 0)"
2583625855 end