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
if lt == "CallNode" lm = @nd_name[last] 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" compile_stmt(last) if return_type != "void" emit(" return " + c_return_default(return_type) + ";") # The hardcoded list above is meant to catch builtin Array/Hash # mutators conventionally called for side-effect. But several of # these names (update, clear, concat, delete, each, pop, push, ...) # are equally plausible on user classes — and there returning the # call's value is the only correct behavior. If the receiver # resolves to a user class and that class defines a method with # the same name, treat the call as an ordinary expression. last_recv = @nd_receiver[last] is_user_method = 0 if last_recv < 0 if @current_class_idx >= 0 if cls_find_method_direct(@current_class_idx, lm) >= 0 is_user_method = 1 end end else last_rt = base_type(infer_type(last_recv)) if is_obj_type(last_rt) == 1 recv_ci = find_class_idx(last_rt[4, last_rt.length - 4]) if recv_ci >= 0 if cls_find_method_direct(recv_ci, lm) >= 0 is_user_method = 1 end end end end if is_user_method == 0 compile_stmt(last) if return_type != "void" emit(" return " + c_return_default(return_type) + ";") end return end return end # Receiver-based setter calls (obj.attr = val) if lm.end_with?("=") && lm != "==" && lm != "!=" && lm != "<=" && lm != ">="test/user_method_named_like_mutator.rbaddedView file
Loading diff…