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

ruby-changes:33193

From: nobu <ko1@a...>
Date: Wed, 5 Mar 2014 15:56:55 +0900 (JST)
Subject: [ruby-changes:33193] nobu:r45272 (trunk): parse.y: optional arguments in rhs

nobu	2014-03-05 15:56:49 +0900 (Wed, 05 Mar 2014)

  New Revision: 45272

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

  Log:
    parse.y: optional arguments in rhs
    
    * parse.y (f_arg_asgn): define optional arguments as argument
      variables in the rhs default expressions.
      [ruby-core:61299] [Bug #9593]

  Modified files:
    trunk/ChangeLog
    trunk/parse.y
    trunk/test/ruby/test_syntax.rb
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 45271)
+++ ChangeLog	(revision 45272)
@@ -1,3 +1,9 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Wed Mar  5 15:56:18 2014  Nobuyoshi Nakada  <nobu@r...>
+
+	* parse.y (f_arg_asgn): define optional arguments as argument
+	  variables in the rhs default expressions.
+	  [ruby-core:61299] [Bug #9593]
+
 Wed Mar  5 11:58:30 2014  Nobuyoshi Nakada  <nobu@r...>
 
 	* ext/openssl/ossl.c (ossl_make_error): check NULL for unknown
Index: parse.y
===================================================================
--- parse.y	(revision 45271)
+++ parse.y	(revision 45272)
@@ -784,7 +784,7 @@ static void token_info_pop(struct parser https://github.com/ruby/ruby/blob/trunk/parse.y#L784
 %type <node> mlhs mlhs_head mlhs_basic mlhs_item mlhs_node mlhs_post mlhs_inner
 %type <id>   fsym keyword_variable user_variable sym symbol operation operation2 operation3
 %type <id>   cname fname op f_rest_arg f_block_arg opt_f_block_arg f_norm_arg f_bad_arg
-%type <id>   f_kwrest f_label
+%type <id>   f_kwrest f_label f_arg_asgn
 /*%%%*/
 /*%
 %type <val> program reswords then do dot_or_colon
@@ -4548,9 +4548,15 @@ f_norm_arg	: f_bad_arg https://github.com/ruby/ruby/blob/trunk/parse.y#L4548
 		    }
 		;
 
-f_arg_item	: f_norm_arg
+f_arg_asgn	: f_norm_arg
 		    {
 			arg_var(get_id($1));
+			$$ = $1;
+		    }
+		;
+
+f_arg_item	: f_arg_asgn
+		    {
 		    /*%%%*/
 			$$ = NEW_ARGS_AUX($1, 1);
 		    /*%
@@ -4708,9 +4714,8 @@ f_kwrest	: kwrest_mark tIDENTIFIER https://github.com/ruby/ruby/blob/trunk/parse.y#L4714
 		    }
 		;
 
-f_opt		: f_norm_arg '=' arg_value
+f_opt		: f_arg_asgn '=' arg_value
 		    {
-			arg_var(get_id($1));
 			$$ = assignable($1, $3);
 		    /*%%%*/
 			$$ = NEW_OPT_ARG(0, $$);
@@ -4720,9 +4725,8 @@ f_opt		: f_norm_arg '=' arg_value https://github.com/ruby/ruby/blob/trunk/parse.y#L4725
 		    }
 		;
 
-f_block_opt	: f_norm_arg '=' primary_value
+f_block_opt	: f_arg_asgn '=' primary_value
 		    {
-			arg_var(get_id($1));
 			$$ = assignable($1, $3);
 		    /*%%%*/
 			$$ = NEW_OPT_ARG(0, $$);
Index: test/ruby/test_syntax.rb
===================================================================
--- test/ruby/test_syntax.rb	(revision 45271)
+++ test/ruby/test_syntax.rb	(revision 45272)
@@ -111,6 +111,30 @@ class TestSyntax < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_syntax.rb#L111
     assert_raise(TypeError) {o.kw(**h)}
   end
 
+  def test_keyword_self_reference
+    bug9593 = '[ruby-core:61299] [Bug #9593]'
+    o = Object.new
+    def o.foo(var: defined?(var)) var end
+    assert_equal(42, o.foo(var: 42))
+    assert_equal("local-variable", o.foo, bug9593)
+
+    o = Object.new
+    def o.foo(var: var) var end
+    assert_nil(o.foo, bug9593)
+  end
+
+  def test_optional_self_reference
+    bug9593 = '[ruby-core:61299] [Bug #9593]'
+    o = Object.new
+    def o.foo(var = defined?(var)) var end
+    assert_equal(42, o.foo(42))
+    assert_equal("local-variable", o.foo, bug9593)
+
+    o = Object.new
+    def o.foo(var = var) var end
+    assert_nil(o.foo, bug9593)
+  end
+
   def test_warn_grouped_expression
     bug5214 = '[ruby-core:39050]'
     assert_warning("", bug5214) do

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

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