[前][次][番号順一覧][スレッド一覧]

ruby-changes:28086

From: nobu <ko1@a...>
Date: Sat, 6 Apr 2013 02:30:52 +0900 (JST)
Subject: [ruby-changes:28086] nobu:r40138 (trunk): parse.y: "nil" for defined? with empty expression

nobu	2013-04-06 02:30:42 +0900 (Sat, 06 Apr 2013)

  New Revision: 40138

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=40138

  Log:
    parse.y: "nil" for defined? with empty expression
    
    * parse.y (new_defined): remove all extra parentheses, and return
      "nil" for defined? with empty expression.
      [ruby-core:54024] [Bug #8224]

  Modified files:
    trunk/ChangeLog
    trunk/parse.y
    trunk/test/ruby/test_defined.rb

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 40137)
+++ ChangeLog	(revision 40138)
@@ -1,3 +1,9 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Sat Apr  6 02:30:28 2013  Nobuyoshi Nakada  <nobu@r...>
+
+	* parse.y (new_defined): remove all extra parentheses, and return
+	  "nil" for defined? with empty expression.
+	  [ruby-core:54024] [Bug #8224]
+
 Sat Apr  6 02:06:04 2013  Aaron Patterson <aaron@t...>
 
 	* ext/psych/lib/psych/visitors/to_ruby.rb: correctly register
Index: parse.y
===================================================================
--- parse.y	(revision 40137)
+++ parse.y	(revision 40138)
@@ -369,6 +369,7 @@ static void fixpos(NODE*,NODE*); https://github.com/ruby/ruby/blob/trunk/parse.y#L369
 static int value_expr_gen(struct parser_params*,NODE*);
 static void void_expr_gen(struct parser_params*,NODE*);
 static NODE *remove_begin(NODE*);
+static NODE *remove_begin_all(NODE*);
 #define value_expr(node) value_expr_gen(parser, (node) = remove_begin(node))
 #define void_expr0(node) void_expr_gen(parser, (node))
 #define void_expr(node) void_expr0((node) = remove_begin(node))
@@ -438,6 +439,8 @@ static NODE *new_attr_op_assign_gen(stru https://github.com/ruby/ruby/blob/trunk/parse.y#L439
 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))
 
+#define new_defined(expr) NEW_DEFINED(remove_begin_all(expr))
+
 static NODE *match_op_gen(struct parser_params*,NODE*,NODE*);
 #define match_op(node1,node2) match_op_gen(parser, (node1), (node2))
 
@@ -616,6 +619,8 @@ new_args_tail_gen(struct parser_params * https://github.com/ruby/ruby/blob/trunk/parse.y#L619
 }
 #define new_args_tail(k,kr,b) new_args_tail_gen(parser, (k),(kr),(b))
 
+#define new_defined(expr) dispatch1(defined, (expr))
+
 #define FIXME 0
 
 #endif /* RIPPER */
@@ -2305,7 +2310,7 @@ arg		: lhs '=' arg https://github.com/ruby/ruby/blob/trunk/parse.y#L2310
 		    {
 		    /*%%%*/
 			in_defined = 0;
-			$$ = NEW_DEFINED($4);
+			$$ = new_defined($4);
 		    /*%
 			in_defined = 0;
 			$$ = dispatch1(defined, $4);
@@ -2705,7 +2710,7 @@ primary		: literal https://github.com/ruby/ruby/blob/trunk/parse.y#L2710
 		    {
 		    /*%%%*/
 			in_defined = 0;
-			$$ = NEW_DEFINED($5);
+			$$ = new_defined($5);
 		    /*%
 			in_defined = 0;
 			$$ = dispatch1(defined, $5);
@@ -8924,6 +8929,16 @@ remove_begin(NODE *node) https://github.com/ruby/ruby/blob/trunk/parse.y#L8929
 	*n = n1 = n1->nd_body;
     }
     return node;
+}
+
+static NODE *
+remove_begin_all(NODE *node)
+{
+    NODE **n = &node, *n1 = node;
+    while (n1 && nd_type(n1) == NODE_BEGIN) {
+	*n = n1 = n1->nd_body;
+    }
+    return node;
 }
 
 static void
Index: test/ruby/test_defined.rb
===================================================================
--- test/ruby/test_defined.rb	(revision 40137)
+++ test/ruby/test_defined.rb	(revision 40138)
@@ -89,6 +89,13 @@ class TestDefined < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_defined.rb#L89
     assert_equal("true", defined?(true))
     assert_equal("false", defined?(false))
     assert_equal("expression", defined?(1))
+
+    bug8224 = '[ruby-core:54024] [Bug #8224]'
+    (1..3).each do |level|
+      expr = "("*level+")"*level
+      assert_equal("nil", eval("defined? #{expr}"), "#{bug8224} defined? #{expr}")
+      assert_equal("nil", eval("defined?(#{expr})"), "#{bug8224} defined?(#{expr})")
+    end
   end
 
   def test_defined_impl_specific

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]