ruby-changes:26534
From: nobu <ko1@a...>
Date: Tue, 25 Dec 2012 00:59:42 +0900 (JST)
Subject: [ruby-changes:26534] nobu:r38585 (trunk): scoped constant op-assignment
nobu 2012-12-25 00:59:31 +0900 (Tue, 25 Dec 2012) New Revision: 38585 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=38585 Log: scoped constant op-assignment * node.h (NODE_OP_CDECL), compile.c (iseq_compile_each), parse.y (stmt, arg): allow scoped constant op-assignment. [ruby-core:40154] [Bug #5449] Modified files: trunk/ChangeLog trunk/compile.c trunk/node.h trunk/parse.y trunk/test/ruby/test_parse.rb trunk/test/ruby/test_syntax.rb Index: ChangeLog =================================================================== --- ChangeLog (revision 38584) +++ ChangeLog (revision 38585) @@ -1,3 +1,9 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1 +Tue Dec 25 00:59:29 2012 Nobuyoshi Nakada <nobu@r...> + + * node.h (NODE_OP_CDECL), compile.c (iseq_compile_each), + parse.y (stmt, arg): allow scoped constant op-assignment. + [ruby-core:40154] [Bug #5449] + Mon Dec 24 04:56:48 2012 NARUSE, Yui <naruse@r...> * lib/net/http/generic_request.rb (Net::HTTPGenericRequest): Index: compile.c =================================================================== --- compile.c (revision 38584) +++ compile.c (revision 38585) @@ -4163,6 +4163,74 @@ iseq_compile_each(rb_iseq_t *iseq, LINK_ https://github.com/ruby/ruby/blob/trunk/compile.c#L4163 } break; } + case NODE_OP_CDECL: { + LABEL *lfin = 0; + LABEL *lassign = 0; + ID mid; + + switch (nd_type(node->nd_head)) { + case NODE_COLON3: + ADD_INSN1(ret, nd_line(node), putobject, rb_cObject); + break; + case NODE_COLON2: + COMPILE(ret, "NODE_OP_CDECL/colon2#nd_head", node->nd_head->nd_head); + break; + default: + do { + COMPILE_ERROR((ERROR_ARGS "%s: invalid node in NODE_OP_CDECL", + ruby_node_name(nd_type(node->nd_head)))); + } while (0); + return COMPILE_NG; + } + mid = node->nd_head->nd_mid; + /* cref */ + if (node->nd_aid == 0) { + lassign = NEW_LABEL(nd_line(node)); + ADD_INSN(ret, nd_line(node), dup); /* cref cref */ + ADD_INSN3(ret, nd_line(node), defined, INT2FIX(DEFINED_CONST), + ID2SYM(mid), Qfalse); /* cref bool */ + ADD_INSNL(ret, nd_line(node), branchunless, lassign); /* cref */ + } + ADD_INSN(ret, nd_line(node), dup); /* cref cref */ + ADD_INSN1(ret, nd_line(node), getconstant, ID2SYM(mid)); /* cref obj */ + + if (node->nd_aid == 0 || node->nd_aid == 1) { + lfin = NEW_LABEL(nd_line(node)); + if (!poped) ADD_INSN(ret, nd_line(node), dup); /* cref [obj] obj */ + if (node->nd_aid == 0) + ADD_INSNL(ret, nd_line(node), branchif, lfin); + else + ADD_INSNL(ret, nd_line(node), branchunless, lfin); + /* cref [obj] */ + if (!poped) ADD_INSN(ret, nd_line(node), pop); /* cref */ + if (lassign) ADD_LABEL(ret, lassign); + COMPILE(ret, "NODE_OP_CDECL#nd_value", node->nd_value); + /* cref value */ + if (poped) + ADD_INSN1(ret, nd_line(node), topn, INT2FIX(1)); /* cref value cref */ + else { + ADD_INSN1(ret, nd_line(node), dupn, INT2FIX(2)); /* cref value cref value */ + ADD_INSN(ret, nd_line(node), swap); /* cref value value cref */ + } + ADD_INSN1(ret, nd_line(node), setconstant, ID2SYM(mid)); /* cref [value] */ + ADD_LABEL(ret, lfin); /* cref [value] */ + if (!poped) ADD_INSN(ret, nd_line(node), swap); /* [value] cref */ + ADD_INSN(ret, nd_line(node), pop); /* [value] */ + } + else { + COMPILE(ret, "NODE_OP_CDECL#nd_value", node->nd_value); + /* cref obj value */ + ADD_CALL(ret, nd_line(node), ID2SYM(node->nd_aid), INT2FIX(1)); + /* cref value */ + ADD_INSN(ret, nd_line(node), swap); /* value cref */ + if (!poped) { + ADD_INSN1(ret, nd_line(node), topn, INT2FIX(1)); /* value cref value */ + ADD_INSN(ret, nd_line(node), swap); /* value value cref */ + } + ADD_INSN1(ret, nd_line(node), setconstant, ID2SYM(mid)); + } + break; + } case NODE_OP_ASGN_AND: case NODE_OP_ASGN_OR:{ LABEL *lfin = NEW_LABEL(nd_line(node)); Index: parse.y =================================================================== --- parse.y (revision 38584) +++ parse.y (revision 38585) @@ -435,6 +435,8 @@ static NODE *node_assign_gen(struct pars https://github.com/ruby/ruby/blob/trunk/parse.y#L435 static NODE *new_op_assign_gen(struct parser_params *parser, NODE *lhs, ID op, NODE *rhs); static NODE *new_attr_op_assign_gen(struct parser_params *parser, NODE *lhs, ID attr, ID op, NODE *rhs); #define new_attr_op_assign(lhs, type, attr, op, rhs) new_attr_op_assign_gen(parser, (lhs), (attr), (op), (rhs)) +static NODE *new_const_op_assign_gen(struct parser_params *parser, NODE *lhs, ID op, NODE *rhs); +#define new_const_op_assign(lhs, op, rhs) new_const_op_assign_gen(parser, (lhs), (op), (rhs)) static NODE *match_op_gen(struct parser_params*,NODE*,NODE*); #define match_op(node1,node2) match_op_gen(parser, (node1), (node2)) @@ -1200,12 +1202,11 @@ stmt : keyword_alias fitem {lex_state = https://github.com/ruby/ruby/blob/trunk/parse.y#L1202 | primary_value tCOLON2 tCONSTANT tOP_ASGN command_call { /*%%%*/ - yyerror("constant re-assignment"); - $$ = 0; + $$ = NEW_COLON2($1, $3); + $$ = new_const_op_assign($$, $4, $5); /*% $$ = dispatch2(const_path_field, $1, $3); $$ = dispatch3(opassign, $$, $4, $5); - $$ = dispatch1(assign_error, $$); %*/ } | primary_value tCOLON2 tIDENTIFIER tOP_ASGN command_call @@ -2007,23 +2008,21 @@ arg : lhs '=' arg https://github.com/ruby/ruby/blob/trunk/parse.y#L2008 | primary_value tCOLON2 tCONSTANT tOP_ASGN arg { /*%%%*/ - yyerror("constant re-assignment"); - $$ = NEW_BEGIN(0); + $$ = NEW_COLON2($1, $3); + $$ = new_const_op_assign($$, $4, $5); /*% $$ = dispatch2(const_path_field, $1, $3); $$ = dispatch3(opassign, $$, $4, $5); - $$ = dispatch1(assign_error, $$); %*/ } | tCOLON3 tCONSTANT tOP_ASGN arg { /*%%%*/ - yyerror("constant re-assignment"); - $$ = NEW_BEGIN(0); + $$ = NEW_COLON3($2); + $$ = new_const_op_assign($$, $3, $4); /*% $$ = dispatch1(top_const_field, $2); $$ = dispatch3(opassign, $$, $3, $4); - $$ = dispatch1(assign_error, $$); %*/ } | backref tOP_ASGN arg @@ -9364,6 +9363,27 @@ new_attr_op_assign_gen(struct parser_par https://github.com/ruby/ruby/blob/trunk/parse.y#L9363 fixpos(asgn, lhs); return asgn; } + +static NODE * +new_const_op_assign_gen(struct parser_params *parser, NODE *lhs, ID op, NODE *rhs) +{ + NODE *asgn; + + if (op == tOROP) { + op = 0; + } + else if (op == tANDOP) { + op = 1; + } + if (lhs) { + asgn = NEW_OP_CDECL(lhs, op, rhs); + } + else { + asgn = NEW_BEGIN(0); + } + fixpos(asgn, lhs); + return asgn; +} #else static VALUE new_op_assign_gen(struct parser_params *parser, VALUE lhs, VALUE op, VALUE rhs) Index: test/ruby/test_syntax.rb =================================================================== --- test/ruby/test_syntax.rb (revision 38584) +++ test/ruby/test_syntax.rb (revision 38585) @@ -209,6 +209,70 @@ eom https://github.com/ruby/ruby/blob/trunk/test/ruby/test_syntax.rb#L209 assert_equal(expected, actual) end + def assert_constant_reassignment_nested(preset, op, expected, err = [], bug = '[Bug #5449]') + [ + ["p ", ""], # no-pop + ["", "p Foo::Bar"], # pop + ].each do |p1, p2| + src = <<-EOM.gsub(/^\s*\n/, '') + class Foo + #{"Bar = " + preset if preset} + end + #{p1}Foo::Bar #{op}= 42 + #{p2} + EOM + msg = "\# #{bug}\n#{src}" + assert_valid_syntax(src, caller_locations(1, 1)[0].path, msg) + assert_in_out_err([], src, expected, err, msg) + end + end + + def test_constant_reassignment_nested + already = /already initialized constant Foo::Bar/ + uninitialized = /uninitialized constant Foo::Bar/ + assert_constant_reassignment_nested(nil, "||", %w[42]) + assert_constant_reassignment_nested("false", "||", %w[42], already) + assert_constant_reassignment_nested("true", "||", %w[true]) + assert_constant_reassignment_nested(nil, "&&", [], uninitialized) + assert_constant_reassignment_nested("false", "&&", %w[false]) + assert_constant_reassignment_nested("true", "&&", %w[42], already) + assert_constant_reassignment_nested(nil, "+", [], uninitialized) + assert_constant_reassignment_nested("false", "+", [], /undefined method/) + assert_constant_reassignment_nested("11", "+", %w[53], already) + end + + def assert_constant_reassignment_toplevel(preset, op, expected, err = [], bug = '[Bug #5449]') + [ + ["p ", ""], # no-pop + ["", "p ::Bar"], # pop + ].each do |p1, p2| + src = <<-EOM.gsub(/^\s*\n/, '') + #{"Bar = " + preset if preset} + class Foo + #{p1}::Bar #{op}= 42 + #{p2} + end + EOM + msg = "\# #{bug}\n#{src}" + assert_valid_syntax(src, caller_locations(1, 1)[0].path, msg) + assert_in_out_err([], src, expected, err, msg) + end + end + + def test_constant_reassignment_toplevel + already = /already initialized constant Bar/ + uninitialized = /uninitialized constant Bar/ + assert_constant_reassignment_toplevel(nil, "||", %w[42]) + assert_constant_reassignment_toplevel("false", "||", %w[42], already) + assert_constant_reassignment_toplevel("true", "||", %w[true]) + assert_constant_reassignment_toplevel(nil, "&&", [], uninitialized) + assert_constant_reassignment_toplevel("false", "&&", %w[false]) + assert_constant_reassignment_toplevel("true", "&&", %w[42], already) + assert_constant_reassignment_toplevel(nil, "+", [], uninitialized) + assert_constant_reassignment_toplevel("false", "+", [], /undefined method/) + assert_constant_reassignment_toplevel("11", "+", %w[53], already) + end + private def not_label(x) @result = x; @not_label ||= nil end Index: test/ruby/test_parse.rb =================================================================== --- test/ruby/test_parse.rb (revision 38584) +++ test/ruby/test_parse.rb (revision 38585) @@ -173,10 +173,12 @@ class TestParse < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_parse.rb#L173 end c = Class.new - assert_raise(SyntaxError) do + assert_nothing_raised(SyntaxError) do eval <<-END, nil, __FILE__, __LINE__+1 + if false c::FOO &= 1 ::FOO &= 1 + end END end Index: node.h =================================================================== --- node.h (revision 38584) +++ node.h (revision 38585) @@ -88,6 +88,8 @@ enum node_type { https://github.com/ruby/ruby/blob/trunk/node.h#L88 #define NODE_OP_ASGN_AND NODE_OP_ASGN_AND NODE_OP_ASGN_OR, #define NODE_OP_ASGN_OR NODE_OP_ASGN_OR + NODE_OP_CDECL, +#define NODE_OP_CDECL NODE_OP_CDECL NODE_CALL, #define NODE_CALL NODE_CALL NODE_FCALL, @@ -399,6 +401,7 @@ typedef struct RNode { https://github.com/ruby/ruby/blob/trunk/node.h#L401 #define NEW_OP_ASGN22(i,o) NEW_NODE(NODE_OP_ASGN2,i,o,rb_id_attrset(i)) #define NEW_OP_ASGN_OR(i,val) NEW_NODE(NODE_OP_ASGN_OR,i,val,0) #define NEW_OP_ASGN_AND(i,val) NEW_NODE(NODE_OP_ASGN_AND,i,val,0) +#define NEW_OP_CDECL(v,op,val) NEW_NODE(NODE_OP_CDECL,v,val,op) #define NEW_GVAR(v) NEW_NODE(NODE_GVAR,v,0,rb_global_entry(v)) #define NEW_LVAR(v) NEW_NODE(NODE_LVAR,v,0,0) #define NEW_DVAR(v) NEW_NODE(NODE_DVAR,v,0,0) -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/