* 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
This commit is contained in:
nobu 2015-10-23 01:49:38 +00:00
Родитель 7e730322ee
Коммит 0b7d473734
5 изменённых файлов: 38 добавлений и 14 удалений

Просмотреть файл

@ -1,3 +1,11 @@
Fri Oct 23 10:49:36 2015 Nobuyoshi Nakada <nobu@ruby-lang.org>
* 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.
Fri Oct 23 07:17:11 2015 Eric Wong <e@80x24.org>
* test/io/wait/test_io_wait.rb (test_wait_eof): test return value

Просмотреть файл

@ -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;

21
parse.y
Просмотреть файл

@ -451,8 +451,8 @@ static NODE *assignable_gen(struct parser_params*,ID,NODE*);
static NODE *aryset_gen(struct parser_params*,NODE*,NODE*);
#define aryset(node1,node2) aryset_gen(parser, (node1), (node2))
static NODE *attrset_gen(struct parser_params*,NODE*,ID);
#define attrset(node,id) attrset_gen(parser, (node), (id))
static NODE *attrset_gen(struct parser_params*,NODE*,ID,ID);
#define attrset(node,q,id) attrset_gen(parser, (node), (q), (id))
static void rb_backref_error_gen(struct parser_params*,NODE*);
#define rb_backref_error(n) rb_backref_error_gen(parser,(n))
@ -1720,7 +1720,7 @@ mlhs_node : user_variable
| primary_value call_op tIDENTIFIER
{
/*%%%*/
$$ = attrset($1, $3);
$$ = attrset($1, $2, $3);
/*%
$$ = dispatch3(field, $1, ripper_id2sym($2), $3);
%*/
@ -1728,7 +1728,7 @@ mlhs_node : user_variable
| primary_value tCOLON2 tIDENTIFIER
{
/*%%%*/
$$ = attrset($1, $3);
$$ = attrset($1, idCOLON2, $3);
/*%
$$ = dispatch2(const_path_field, $1, $3);
%*/
@ -1736,7 +1736,7 @@ mlhs_node : user_variable
| primary_value call_op tCONSTANT
{
/*%%%*/
$$ = attrset($1, $3);
$$ = attrset($1, $2, $3);
/*%
$$ = dispatch3(field, $1, ripper_id2sym($2), $3);
%*/
@ -1811,7 +1811,7 @@ lhs : user_variable
| primary_value call_op tIDENTIFIER
{
/*%%%*/
$$ = attrset($1, $3);
$$ = attrset($1, $2, $3);
/*%
$$ = dispatch3(field, $1, ripper_id2sym($2), $3);
%*/
@ -1819,7 +1819,7 @@ lhs : user_variable
| primary_value tCOLON2 tIDENTIFIER
{
/*%%%*/
$$ = attrset($1, $3);
$$ = attrset($1, idCOLON2, $3);
/*%
$$ = dispatch3(field, $1, ID2SYM(idCOLON2), $3);
%*/
@ -1827,7 +1827,7 @@ lhs : user_variable
| primary_value call_op tCONSTANT
{
/*%%%*/
$$ = attrset($1, $3);
$$ = attrset($1, $2, $3);
/*%
$$ = dispatch3(field, $1, ripper_id2sym($2), $3);
%*/
@ -9187,9 +9187,10 @@ block_dup_check_gen(struct parser_params *parser, NODE *node1, NODE *node2)
}
static NODE *
attrset_gen(struct parser_params *parser, NODE *recv, ID id)
attrset_gen(struct parser_params *parser, NODE *recv, ID atype, ID id)
{
return NEW_ATTRASGN(recv, rb_id_attrset(id), 0);
if (atype != tDOTQ) id = rb_id_attrset(id);
return NEW_ATTRASGN(recv, id, 0);
}
static void

Просмотреть файл

@ -42,5 +42,10 @@ class TestCall < Test::Unit::TestCase
assert_equal(6, o.x)
o.?x *= 7
assert_equal(42, o.x)
o = nil
assert_nil(o.?x)
assert_nothing_raised(NoMethodError) {o.?x = 6}
assert_nothing_raised(NoMethodError) {o.?x *= 7}
end
end

Просмотреть файл

@ -149,7 +149,7 @@ class TestISeq < Test::Unit::TestCase
src = "a['foo'] = a['bar']; 'a'.freeze"
_,_,_,_,_,_,_,_,_,_,_,_,_,body= RubyVM::InstructionSequence.compile(src, __FILE__, __FILE__, __LINE__, false).to_a
body.each{|insn|
next if Integer === insn
next unless Array === insn
op = insn.first
assert(!op.to_s.match(/^opt_/), "#{op}")
}