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/Hash
27156+ # mutators conventionally called for side-effect. But several of
27157+ # these names (update, clear, concat, delete, each, pop, push, ...)
27158+ # are equally plausible on user classes — and there returning the
27159+ # call's value is the only correct behavior. If the receiver
27160+ # resolves to a user class and that class defines a method with
27161+ # the same name, treat the call as an ordinary expression.
27162+ last_recv = @nd_receiver[last]
27163+ is_user_method = 0
27164+ if last_recv < 0
27165+ if @current_class_idx >= 0
27166+ if cls_find_method_direct(@current_class_idx, lm) >= 0
27167+ is_user_method = 1
27168+ end
27169+ end
27170+ else
27171+ last_rt = base_type(infer_type(last_recv))
27172+ if is_obj_type(last_rt) == 1
27173+ recv_ci = find_class_idx(last_rt[4, last_rt.length - 4])
27174+ if recv_ci >= 0
27175+ if cls_find_method_direct(recv_ci, lm) >= 0
27176+ is_user_method = 1
27177+ end
27178+ end
27179+ end
27180+ end
27181+ if is_user_method == 0
27182+ compile_stmt(last)
27183+ if return_type != "void"
27184+ emit(" return " + c_return_default(return_type) + ";")
27185+ end
27186+ return
2715827187 end
27159- return
2716027188 end
2716127189 # Receiver-based setter calls (obj.attr = val)
2716227190 if lm.end_with?("=") && lm != "==" && lm != "!=" && lm != "<=" && lm != ">="