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_exprint_array block: emit(sp_IntArray_replace(rc, arg), rc)so the call has the side effect and yields the receiver. infer_call_type:replacepreserves the receiver's array type soc = a.replace(b)tagscasint_arrayrather thanint.
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
end40074007
return "int_array"40084008
end4009+
# `replace(other)` returns the receiver, not a fresh array;4010+
# the inferred result must therefore preserve the receiver's4011+
# array type so that an expression-form `c = a.replace(b)`4012+
# still tags `c` as `int_array` (or whatever `a` is) rather4013+
# than falling through to `int`.4014+
if mname == "replace"4015+
if recv >= 04016+
return infer_type(recv)4017+
end4018+
end40094019
if mname == "pop"40104020
if recv >= 040114021
rt = infer_type(recv)@@ -26333,6 +26343,15 @@
2633326343
end2633426344
end2633526345
end26346+
# `replace(other)` in expression position: the stmt-form26347+
# arm in compile_*_stmt only fires when the call's value is26348+
# discarded; in expression position (e.g. last stmt of a26349+
# method body, or rvalue of an assignment) we still need to26350+
# emit the side effect *and* yield the receiver as the26351+
# 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+
end2633626355
# take_while / drop_while: block-driven prefix scan. take_while2633726356
# collects elements from the front while the block stays truthy;2633826357
# drop_while skips them and returns the rest. Mirrors thetest/int_array_replace_expr.rbaddedView file
Loading diff…
test/int_array_replace_expr.rb.expectedaddedView file
Loading diff…