Commit de8cf15e

de8cf15e3c97Merged

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

Description

Implement Array#clear in expression position for typed arrays

Array#clear was only handled in expression position for poly_array; a typed array ([1, 2, 3].clear) fell through to the unresolved-call path and the result mis-typed as int 0. The statement-form clear already emptied every typed array correctly.

Add a unified clear arm in compile_array_method_expr that zeroes len (and resets the sliding start window for int_array/sym_array, matching the statement form) and returns the now-empty array.

Fixes #1325

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

Details

Commit
de8cf15e3c97
Parent
7b272468b4ad

spinel_codegen.rbmodifiedView file

@@ -24037,6 +24037,18 @@
2403724037 if mname == "append"
2403824038 return compile_array_method_expr(nid, "push", rc, recv_type)
2403924039 end
24040+ # Array#clear -- empty the array and return it. All typed-array
24041+ # structs share the `len` field; int_array/sym_array additionally
24042+ # carry a sliding `start` window that the statement-form arm also
24043+ # resets. Without this expression arm `[1,2,3].clear` fell through
24044+ # to the unresolved-call path and the result mis-typed as int 0.
24045+ if mname == "clear"
24046+ bt_clr = base_type(recv_type)
24047+ if bt_clr == "int_array" || bt_clr == "sym_array"
24048+ return "(" + rc + "->len = 0, " + rc + "->start = 0, " + rc + ")"
24049+ end
24050+ return "(" + rc + "->len = 0, " + rc + ")"
24051+ end
2404024052 # Array#object_id -- the receiver's pointer bit pattern, as for the
2404124053 # String case. Stable across GC (the array isn't relocated).
2404224054 if mname == "object_id"