Commit 18819eac

18819eacc369Merged

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

Description

feat(codegen): support Array#clear in expression position on poly_array

The stmt-form was already supported; in expression position (rvalue of ||=, last expr of a method body, chained call) the dispatcher fell through to the unresolved-call warning and emitted literal 0. The infer_call_type default of int then mistyped any binding receiving the result.

Two changes:

  • Expression arm in poly_array's compile_object_method_expr block: emit (rc->len = 0, rc) so the side effect runs and the receiver is yielded.
  • infer_call_type: clear preserves the receiver's array (or string) type so b = a.clear keeps the array tag instead of collapsing to int.

Surfaced via optcarrot's @sp_visible ||= @sp_map.clear PPU shape.

Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]

Details

Commit
18819eacc369
Parent
64226cf0deb5

spinel_codegen.rbmodifiedView file

@@ -4114,6 +4114,20 @@
41144114 return infer_type(recv)
41154115 end
41164116 end
4117+ # `clear` mutates in place and returns the now-empty receiver.
4118+ # Same shape as `replace`: preserve the receiver's array/string
4119+ # type so chained or `||=`-style usage doesn't fall back to int.
4120+ if mname == "clear"
4121+ if recv >= 0
4122+ rt_clr = infer_type(recv)
4123+ if rt_clr == "int_array" || rt_clr == "sym_array" ||
4124+ rt_clr == "str_array" || rt_clr == "float_array" ||
4125+ rt_clr == "poly_array" || is_ptr_array_type(rt_clr) == 1 ||
4126+ rt_clr == "string" || rt_clr == "mutable_str"
4127+ return rt_clr
4128+ end
4129+ end
4130+ end
41174131 if mname == "pop"
41184132 if recv >= 0
41194133 rt = infer_type(recv)
@@ -26926,6 +26940,13 @@
2692626940 end
2692726941 return "(sp_PolyArray_push(" + rc + ", " + box_expr_to_poly(arg_id) + "), 0)"
2692826942 end
26943+ # `clear` in expression position (e.g. `@x ||= @arr.clear`).
26944+ # Mirrors the stmt-form arm in compile_*_stmt: zero the
26945+ # length, leave capacity/buffer alone, return the (now-empty)
26946+ # array so the caller can chain.
26947+ if mname == "clear"
26948+ return "(" + rc + "->len = 0, " + rc + ")"
26949+ end
2692926950 end
2693026951 ""
2693126952 end