aboutsummaryrefslogtreecommitdiffstats
path: root/compile.c
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2015-10-23 01:49:38 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2015-10-23 01:49:38 +0000
commit0b7d473734d0dec8520afe7a36540aa1f40d2532 (patch)
tree6d2d4381b6bf8322445991dcb3a0c3bc97ef197b /compile.c
parent7e730322ee714b607c94389911f5b86704cc8ed4 (diff)
downloadruby-0b7d473734d0dec8520afe7a36540aa1f40d2532.tar.gz
safe navigation attrset
* compile.c (iseq_compile_each): support safe navigation of simple attribute assignment. [Feature #11537] * parse.y (mlhs_node, lhs, attrset_gen): ditto. keep mid non-attrset as the sign of safe navigation. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@52234 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'compile.c')
-rw-r--r--compile.c16
1 files changed, 13 insertions, 3 deletions
diff --git a/compile.c b/compile.c
index d6329e7aa9..8d12fc0e01 100644
--- a/compile.c
+++ b/compile.c
@@ -5588,12 +5588,14 @@ iseq_compile_each(rb_iseq_t *iseq, LINK_ANCHOR *ret, NODE * node, int poped)
DECL_ANCHOR(recv);
DECL_ANCHOR(args);
unsigned int flag = 0;
+ ID mid = node->nd_mid;
+ LABEL *lskip = 0;
VALUE argc;
/* optimization shortcut
* obj["literal"] = value -> opt_aset_with(obj, "literal", value)
*/
- if (node->nd_mid == idASET && !private_recv_p(node) && node->nd_args &&
+ if (mid == idASET && !private_recv_p(node) && node->nd_args &&
nd_type(node->nd_args) == NODE_ARRAY && node->nd_args->nd_alen == 2 &&
nd_type(node->nd_args->nd_head) == NODE_STR &&
iseq->compile_data->current_block == NULL &&
@@ -5622,8 +5624,15 @@ iseq_compile_each(rb_iseq_t *iseq, LINK_ANCHOR *ret, NODE * node, int poped)
flag |= COMPILE_RECV(recv, "recv", node);
debugp_param("argc", argc);
- debugp_param("nd_mid", ID2SYM(node->nd_mid));
+ debugp_param("nd_mid", ID2SYM(mid));
+ if (!rb_is_attrset_id(mid)) {
+ /* safe nav attr */
+ mid = rb_id_attrset(mid);
+ ADD_INSN(recv, line, dup);
+ lskip = NEW_LABEL(line);
+ ADD_INSNL(recv, line, branchnil, lskip);
+ }
if (!poped) {
ADD_INSN(ret, line, putnil);
ADD_SEQ(ret, recv);
@@ -5653,7 +5662,8 @@ iseq_compile_each(rb_iseq_t *iseq, LINK_ANCHOR *ret, NODE * node, int poped)
ADD_SEQ(ret, recv);
ADD_SEQ(ret, args);
}
- ADD_SEND_WITH_FLAG(ret, line, node->nd_mid, argc, INT2FIX(flag));
+ ADD_SEND_WITH_FLAG(ret, line, mid, argc, INT2FIX(flag));
+ if (lskip) ADD_LABEL(ret, lskip);
ADD_INSN(ret, line, pop);
break;