Commit a7be5ac8

a7be5ac8a4bbMerged

Yusuke Endoh <[email protected]> · 6 months ago · 2 files · +1 added · ~1 modified

Description

fix(codegen): don't drop return value of user methods named like mutators

compile_body_return has a hardcoded list of method names — update, clear, concat, delete, each, pop, push, merge!, times, upto, ... — that get treated as statements when they appear as the final expression of a method body. The intent is to discard the return value of Hash#update / Array#concat / etc., which are conventionally called for side-effect.

But the check fires purely on method name, ignoring the receiver. So if a user class defines def update(x) and another method ends with update(x + 1) (or c.update(x) with c : MyClass), the value is silently dropped and the method's return ... becomes return 0.

Resolve the receiver before matching the name list. If the receiver (or self for no-recv calls) resolves to a user class that defines a method by that name, fall through to the normal expression path. Builtin Hash/Array recv still hits the statement path as before.

Encountered while compiling optcarrot's APU#update_latency whose body is update(@cpu.update + 1) — both calls were eaten.

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

Details

Commit
a7be5ac8a4bb
Parent
bee7e89d5b64

spinel_codegen.rbmodifiedView file

@@ -26979,11 +26979,39 @@
2697926979 if lt == "CallNode"
2698026980 lm = @nd_name[last]
2698126981 if lm == "[]=" || lm == "push" || lm == "pop" || lm == "emit" || lm == "emit_raw" || lm == "puts" || lm == "print" || lm == "p" || lm == "printf" || lm == "warn" || lm == "raise" || lm == "exit" || lm == "abort" || lm == "sleep" || lm == "delete" || lm == "clear" || lm == "concat" || lm == "prepend" || lm == "fill" || lm == "insert" || lm == "update" || lm == "merge!" || lm == "store" || lm == "reverse!" || lm == "sort!" || lm == "each" || lm == "times" || lm == "upto" || lm == "downto"
26982- compile_stmt(last)
26983- if return_type != "void"
26984- emit(" return " + c_return_default(return_type) + ";")
26982+ # The hardcoded list above is meant to catch builtin Array/Hash
26983+ # mutators conventionally called for side-effect. But several of
26984+ # these names (update, clear, concat, delete, each, pop, push, ...)
26985+ # are equally plausible on user classes — and there returning the
26986+ # call's value is the only correct behavior. If the receiver
26987+ # resolves to a user class and that class defines a method with
26988+ # the same name, treat the call as an ordinary expression.
26989+ last_recv = @nd_receiver[last]
26990+ is_user_method = 0
26991+ if last_recv < 0
26992+ if @current_class_idx >= 0
26993+ if cls_find_method_direct(@current_class_idx, lm) >= 0
26994+ is_user_method = 1
26995+ end
26996+ end
26997+ else
26998+ last_rt = base_type(infer_type(last_recv))
26999+ if is_obj_type(last_rt) == 1
27000+ recv_ci = find_class_idx(last_rt[4, last_rt.length - 4])
27001+ if recv_ci >= 0
27002+ if cls_find_method_direct(recv_ci, lm) >= 0
27003+ is_user_method = 1
27004+ end
27005+ end
27006+ end
27007+ end
27008+ if is_user_method == 0
27009+ compile_stmt(last)
27010+ if return_type != "void"
27011+ emit(" return " + c_return_default(return_type) + ";")
27012+ end
27013+ return
2698527014 end
26986- return
2698727015 end
2698827016 # Receiver-based setter calls (obj.attr = val)
2698927017 if lm.end_with?("=") && lm != "==" && lm != "!=" && lm != "<=" && lm != ">="