Commit dcd875a5

dcd875a5b754Merged

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

Description

feat(codegen): support Array#replace in expression position on int_array

The stmt-form arm in compile_*_stmt only fires when the call's value is discarded. In expression position (e.g. last stmt of a method body or rvalue of an assignment) the dispatcher fell through to the unresolved-call warning and emitted literal 0.

Two changes:

  • Expression arm in compile_object_method_expr int_array block: emit (sp_IntArray_replace(rc, arg), rc) so the call has the side effect and yields the receiver.
  • infer_call_type: replace preserves the receiver's array type so c = a.replace(b) tags c as int_array rather than int.

Surfaced via optcarrot's def load_battery; ...; @wrk.replace(sav.bytes); end where the call sits at the tail of the method.

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

Details

Commit
dcd875a5b754
Parent
02836bc61f76

spinel_codegen.rbmodifiedView file

@@ -4006,6 +4006,16 @@
40064006 end
40074007 return "int_array"
40084008 end
4009+ # `replace(other)` returns the receiver, not a fresh array;
4010+ # the inferred result must therefore preserve the receiver's
4011+ # array type so that an expression-form `c = a.replace(b)`
4012+ # still tags `c` as `int_array` (or whatever `a` is) rather
4013+ # than falling through to `int`.
4014+ if mname == "replace"
4015+ if recv >= 0
4016+ return infer_type(recv)
4017+ end
4018+ end
40094019 if mname == "pop"
40104020 if recv >= 0
40114021 rt = infer_type(recv)
@@ -26333,6 +26343,15 @@
2633326343 end
2633426344 end
2633526345 end
26346+ # `replace(other)` in expression position: the stmt-form
26347+ # arm in compile_*_stmt only fires when the call's value is
26348+ # discarded; in expression position (e.g. last stmt of a
26349+ # method body, or rvalue of an assignment) we still need to
26350+ # emit the side effect *and* yield the receiver as the
26351+ # value. Use the comma operator so the result is `rc`.
26352+ if mname == "replace" && recv_type == "int_array"
26353+ return "(sp_IntArray_replace(" + rc + ", " + compile_arg0(nid) + "), " + rc + ")"
26354+ end
2633626355 # take_while / drop_while: block-driven prefix scan. take_while
2633726356 # collects elements from the front while the block stays truthy;
2633826357 # drop_while skips them and returns the rest. Mirrors the