aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--ChangeLog6
-rw-r--r--compile.c68
-rw-r--r--node.h3
-rw-r--r--parse.y38
-rw-r--r--test/ruby/test_parse.rb4
-rw-r--r--test/ruby/test_syntax.rb64
6 files changed, 173 insertions, 10 deletions
diff --git a/ChangeLog b/ChangeLog
index d57b12b088..5c1647113e 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+Tue Dec 25 00:59:29 2012 Nobuyoshi Nakada <nobu@ruby-lang.org>
+
+ * 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@ruby-lang.org>
* lib/net/http/generic_request.rb (Net::HTTPGenericRequest):
diff --git a/compile.c b/compile.c
index 3daa6077fc..e151fa83bf 100644
--- a/compile.c
+++ b/compile.c
@@ -4163,6 +4163,74 @@ iseq_compile_each(rb_iseq_t *iseq, LINK_ANCHOR *ret, NODE * node, int poped)
}
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));
diff --git a/node.h b/node.h
index 09186094ba..0fa72545c2 100644
--- a/node.h
+++ b/node.h
@@ -88,6 +88,8 @@ enum node_type {
#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 {
#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)
diff --git a/parse.y b/parse.y
index e42cd9e79f..2b119d465d 100644
--- a/parse.y
+++ b/parse.y
@@ -435,6 +435,8 @@ static NODE *node_assign_gen(struct parser_params*,NODE*,NODE*);
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 = EXPR_FNAME;} fitem
| 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
| 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_params *parser, NODE *lhs, ID attr, ID op,
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)
diff --git a/test/ruby/test_parse.rb b/test/ruby/test_parse.rb
index 319af751d0..d6e42b12eb 100644
--- a/test/ruby/test_parse.rb
+++ b/test/ruby/test_parse.rb
@@ -173,10 +173,12 @@ class TestParse < Test::Unit::TestCase
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
diff --git a/test/ruby/test_syntax.rb b/test/ruby/test_syntax.rb
index b2d625cd2a..5af1ea48ba 100644
--- a/test/ruby/test_syntax.rb
+++ b/test/ruby/test_syntax.rb
@@ -209,6 +209,70 @@ eom
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