Commit d3a530b4
d3a530b4042dMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 2 files · +1 added · ~1 modified
Description
Merge pull request #161 from mame/pr/user-method-mutator-name-return
fix(codegen): don't drop return value of user methods named like mutators
Details
- Commit
- d3a530b4042d
- Parent
- 48ac49664a7b
- Merge parent
- a7be5ac8a4bb
spinel_codegen.rbmodifiedView file
@@ -27152,11 +27152,39 @@
2715227152
if lt == "CallNode"2715327153
lm = @nd_name[last]2715427154
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"27155-
compile_stmt(last)27156-
if return_type != "void"27157-
emit(" return " + c_return_default(return_type) + ";")27155+
# The hardcoded list above is meant to catch builtin Array/Hash27156+
# mutators conventionally called for side-effect. But several of27157+
# these names (update, clear, concat, delete, each, pop, push, ...)27158+
# are equally plausible on user classes — and there returning the27159+
# call's value is the only correct behavior. If the receiver27160+
# resolves to a user class and that class defines a method with27161+
# the same name, treat the call as an ordinary expression.27162+
last_recv = @nd_receiver[last]27163+
is_user_method = 027164+
if last_recv < 027165+
if @current_class_idx >= 027166+
if cls_find_method_direct(@current_class_idx, lm) >= 027167+
is_user_method = 127168+
end27169+
end27170+
else27171+
last_rt = base_type(infer_type(last_recv))27172+
if is_obj_type(last_rt) == 127173+
recv_ci = find_class_idx(last_rt[4, last_rt.length - 4])27174+
if recv_ci >= 027175+
if cls_find_method_direct(recv_ci, lm) >= 027176+
is_user_method = 127177+
end27178+
end27179+
end27180+
end27181+
if is_user_method == 027182+
compile_stmt(last)27183+
if return_type != "void"27184+
emit(" return " + c_return_default(return_type) + ";")27185+
end27186+
return2715827187
end27159-
return2716027188
end2716127189
# Receiver-based setter calls (obj.attr = val)2716227190
if lm.end_with?("=") && lm != "==" && lm != "!=" && lm != "<=" && lm != ">="test/user_method_named_like_mutator.rbaddedView file
Loading diff…