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

ruby-changes:62358

From: Jeremy <ko1@a...>
Date: Thu, 23 Jul 2020 00:35:17 +0900 (JST)
Subject: [ruby-changes:62358] d47e124857 (master): Switch reserved for numbered parameter warning to SyntaxError

https://git.ruby-lang.org/ruby.git/commit/?id=d47e124857

From d47e124857ecdccfeb0766c04cf1ef2cdfdd08bc Mon Sep 17 00:00:00 2001
From: Jeremy Evans <code@j...>
Date: Fri, 29 May 2020 15:54:30 -0700
Subject: Switch reserved for numbered parameter warning to SyntaxError


diff --git a/parse.y b/parse.y
index e580f7f..ab71469 100644
--- a/parse.y
+++ b/parse.y
@@ -12079,8 +12079,8 @@ static void https://github.com/ruby/ruby/blob/trunk/parse.y#L12079
 numparam_name(struct parser_params *p, ID id)
 {
     if (!NUMPARAM_ID_P(id)) return;
-    rb_warn1("`_%d' is reserved for numbered parameter; consider another name",
-	     WARN_I(NUMPARAM_ID_TO_IDX(id)));
+    compile_error(p, "_%d is reserved for numbered parameter",
+        NUMPARAM_ID_TO_IDX(id));
 }
 
 static void
diff --git a/spec/ruby/language/numbered_parameters_spec.rb b/spec/ruby/language/numbered_parameters_spec.rb
index e7b2204..5b0f74b 100644
--- a/spec/ruby/language/numbered_parameters_spec.rb
+++ b/spec/ruby/language/numbered_parameters_spec.rb
@@ -32,19 +32,38 @@ ruby_version_is "2.7" do https://github.com/ruby/ruby/blob/trunk/spec/ruby/language/numbered_parameters_spec.rb#L32
       }.should raise_error(SyntaxError, /numbered parameter is already used in.+ outer block here/m)
     end
 
-    it "can be overwritten with local variable" do
-      suppress_warning do
-        eval <<~CODE
-          _1 = 0
-          proc { _1 }.call("a").should == 0
-        CODE
+    ruby_version_is '2.7'...'2.8' do
+      it "can be overwritten with local variable" do
+        suppress_warning do
+          eval <<~CODE
+            _1 = 0
+            proc { _1 }.call("a").should == 0
+          CODE
+        end
+      end
+
+      it "warns when numbered parameter is overriten with local variable" do
+        -> {
+          eval("_1 = 0")
+        }.should complain(/warning: `_1' is reserved for numbered parameter; consider another name/)
       end
     end
 
-    it "warns when numbered parameter is overriten with local variable" do
-      -> {
-        eval("_1 = 0")
-      }.should complain(/warning: `_1' is reserved for numbered parameter; consider another name/)
+    ruby_version_is '2.8' do
+      it "cannot be overwritten with local variable" do
+        -> {
+          eval <<~CODE
+            _1 = 0
+            proc { _1 }.call("a").should == 0
+          CODE
+        }.should raise_error(SyntaxError, /_1 is reserved for numbered parameter/)
+      end
+
+      it "errors when numbered parameter is overriten with local variable" do
+        -> {
+          eval("_1 = 0")
+        }.should raise_error(SyntaxError, /_1 is reserved for numbered parameter/)
+      end
     end
 
     it "raises SyntaxError when block parameters are specified explicitly" do
diff --git a/test/ruby/test_syntax.rb b/test/ruby/test_syntax.rb
index 6245c73..546b5a2 100644
--- a/test/ruby/test_syntax.rb
+++ b/test/ruby/test_syntax.rb
@@ -1468,13 +1468,12 @@ eom https://github.com/ruby/ruby/blob/trunk/test/ruby/test_syntax.rb#L1468
     assert_syntax_error('-> {_1; -> {_2}}', /numbered parameter is already used/)
     assert_syntax_error('-> {-> {_1}; _2}', /numbered parameter is already used/)
     assert_syntax_error('proc {_1; _1 = nil}', /Can't assign to numbered parameter _1/)
-    mesg = proc {|n| /`_#{n}' is reserved for numbered parameter/}
-    assert_warn(mesg[1]) {eval('proc {_1 = nil}')}
-    assert_warn(mesg[2]) {eval('_2=1')}
-    assert_warn(mesg[3]) {eval('proc {|_3|}')}
-    assert_warn(mesg[4]) {instance_eval('def x(_4) end')}
-    assert_warn(mesg[5]) {instance_eval('def _5; end')}
-    assert_warn(mesg[6]) {instance_eval('def self._6; end')}
+    assert_syntax_error('proc {_1 = nil}', /_1 is reserved for numbered parameter/)
+    assert_syntax_error('_2=1', /_2 is reserved for numbered parameter/)
+    assert_syntax_error('proc {|_3|}', /_3 is reserved for numbered parameter/)
+    assert_syntax_error('def x(_4) end', /_4 is reserved for numbered parameter/)
+    assert_syntax_error('def _5; end', /_5 is reserved for numbered parameter/)
+    assert_syntax_error('def self._6; end', /_6 is reserved for numbered parameter/)
     assert_raise_with_message(NameError, /undefined local variable or method `_1'/) {
       eval('_1')
     }
-- 
cgit v0.10.2


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

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