ruby-changes:58296
From: Jeremy <ko1@a...>
Date: Fri, 18 Oct 2019 01:32:25 +0900 (JST)
Subject: [ruby-changes:58296] 0162e7e647 (master): Make circular argument reference a SyntaxError instead of a warning
https://git.ruby-lang.org/ruby.git/commit/?id=0162e7e647 From 0162e7e6471b639dfeeded29943e9e27c9519826 Mon Sep 17 00:00:00 2001 From: Jeremy Evans <code@j...> Date: Sun, 11 Aug 2019 16:22:58 -0700 Subject: Make circular argument reference a SyntaxError instead of a warning Fixes [Bug #10314] diff --git a/parse.y b/parse.y index f9de951..8e65bf7 100644 --- a/parse.y +++ b/parse.y @@ -9890,7 +9890,8 @@ gettable(struct parser_params *p, ID id, const YYLTYPE *loc) https://github.com/ruby/ruby/blob/trunk/parse.y#L9890 if (dyna_in_block(p) && dvar_defined_ref(p, id, &vidp)) { if (NUMPARAM_ID_P(id) && numparam_nested_p(p)) return 0; if (id == p->cur_arg) { - rb_warn1("circular argument reference - %"PRIsWARN, rb_id2str(id)); + compile_error(p, "circular argument reference - %"PRIsWARN, rb_id2str(id)); + return 0; } if (vidp) *vidp |= LVAR_USED; node = NEW_DVAR(id, loc); @@ -9898,7 +9899,8 @@ gettable(struct parser_params *p, ID id, const YYLTYPE *loc) https://github.com/ruby/ruby/blob/trunk/parse.y#L9899 } if (local_id_ref(p, id, &vidp)) { if (id == p->cur_arg) { - rb_warn1("circular argument reference - %"PRIsWARN, rb_id2str(id)); + compile_error(p, "circular argument reference - %"PRIsWARN, rb_id2str(id)); + return 0; } if (vidp) *vidp |= LVAR_USED; node = NEW_LVAR(id, loc); diff --git a/spec/ruby/language/block_spec.rb b/spec/ruby/language/block_spec.rb index e5c9fcb..6f92383 100644 --- a/spec/ruby/language/block_spec.rb +++ b/spec/ruby/language/block_spec.rb @@ -876,20 +876,38 @@ describe "Post-args" do https://github.com/ruby/ruby/blob/trunk/spec/ruby/language/block_spec.rb#L876 end describe "with a circular argument reference" do - it "shadows an existing local with the same name as the argument" do - a = 1 - -> { - @proc = eval "proc { |a=a| a }" - }.should complain(/circular argument reference/) - @proc.call.should == nil + ruby_version_is ''...'2.7' do + it "warns and uses a nil value when there is an existing local variable with same name" do + a = 1 + -> { + @proc = eval "proc { |a=a| a }" + }.should complain(/circular argument reference/) + @proc.call.should == nil + end + + it "warns and uses a nil value when there is an existing method with same name" do + def a; 1; end + -> { + @proc = eval "proc { |a=a| a }" + }.should complain(/circular argument reference/) + @proc.call.should == nil + end end - it "shadows an existing method with the same name as the argument" do - def a; 1; end - -> { - @proc = eval "proc { |a=a| a }" - }.should complain(/circular argument reference/) - @proc.call.should == nil + ruby_version_is '2.7' do + it "raises a SyntaxError if using an existing local with the same name as the argument" do + a = 1 + -> { + @proc = eval "proc { |a=a| a }" + }.should raise_error(SyntaxError) + end + + it "raises a SyntaxError if there is an existing method with the same name as the argument" do + def a; 1; end + -> { + @proc = eval "proc { |a=a| a }" + }.should raise_error(SyntaxError) + end end it "calls an existing method with the same name as the argument if explicitly using ()" do diff --git a/spec/ruby/language/def_spec.rb b/spec/ruby/language/def_spec.rb index 82813bd..fc5693a 100644 --- a/spec/ruby/language/def_spec.rb +++ b/spec/ruby/language/def_spec.rb @@ -177,17 +177,32 @@ describe "An instance method with a default argument" do https://github.com/ruby/ruby/blob/trunk/spec/ruby/language/def_spec.rb#L177 foo(2,3,3).should == [2,3,[3]] end - it "shadows an existing method with the same name as the local" do - def bar - 1 + ruby_version_is ''...'2.7' do + it "warns and uses a nil value when there is an existing local method with same name" do + def bar + 1 + end + -> { + eval "def foo(bar = bar) + bar + end" + }.should complain(/circular argument reference/) + foo.should == nil + foo(2).should == 2 + end + end + + ruby_version_is '2.7' do + it "raises a syntaxError an existing method with the same name as the local variable" do + def bar + 1 + end + -> { + eval "def foo(bar = bar) + bar + end" + }.should raise_error(SyntaxError) end - -> { - eval "def foo(bar = bar) - bar - end" - }.should complain(/circular argument reference/) - foo.should == nil - foo(2).should == 2 end it "calls a method with the same name as the local when explicitly using ()" do diff --git a/spec/ruby/language/lambda_spec.rb b/spec/ruby/language/lambda_spec.rb index 5fa7fa6..ddd0b57 100644 --- a/spec/ruby/language/lambda_spec.rb +++ b/spec/ruby/language/lambda_spec.rb @@ -267,20 +267,38 @@ describe "A lambda literal -> () { }" do https://github.com/ruby/ruby/blob/trunk/spec/ruby/language/lambda_spec.rb#L267 end describe "with circular optional argument reference" do - it "shadows an existing local with the same name as the argument" do - a = 1 - -> { - @proc = eval "-> (a=a) { a }" - }.should complain(/circular argument reference/) - @proc.call.should == nil + ruby_version_is ''...'2.7' do + it "warns and uses a nil value when there is an existing local variable with same name" do + a = 1 + -> { + @proc = eval "-> (a=a) { a }" + }.should complain(/circular argument reference/) + @proc.call.should == nil + end + + it "warns and uses a nil value when there is an existing method with same name" do + def a; 1; end + -> { + @proc = eval "-> (a=a) { a }" + }.should complain(/circular argument reference/) + @proc.call.should == nil + end end - it "shadows an existing method with the same name as the argument" do - def a; 1; end - -> { - @proc = eval "-> (a=a) { a }" - }.should complain(/circular argument reference/) - @proc.call.should == nil + ruby_version_is '2.7' do + it "raises a SyntaxError if using an existing local with the same name as the argument" do + a = 1 + -> { + @proc = eval "-> (a=a) { a }" + }.should raise_error(SyntaxError) + end + + it "raises a SyntaxError if there is an existing method with the same name as the argument" do + def a; 1; end + -> { + @proc = eval "-> (a=a) { a }" + }.should raise_error(SyntaxError) + end end it "calls an existing method with the same name as the argument if explicitly using ()" do diff --git a/test/ruby/test_syntax.rb b/test/ruby/test_syntax.rb index 48610da..4b9be49 100644 --- a/test/ruby/test_syntax.rb +++ b/test/ruby/test_syntax.rb @@ -230,27 +230,23 @@ class TestSyntax < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_syntax.rb#L230 end def test_keyword_self_reference - bug9593 = '[ruby-core:61299] [Bug #9593]' o = Object.new - assert_warn(/circular argument reference - var/) do + assert_raise(SyntaxError) do o.instance_eval("def foo(var: defined?(var)) var end") end - assert_equal(42, o.foo(var: 42)) - assert_equal("local-variable", o.foo, bug9593) o = Object.new - assert_warn(/circular argument reference - var/) do + assert_raise(SyntaxError) do o.instance_eval("def foo(var: var) var end") end - assert_nil(o.foo, bug9593) o = Object.new - assert_warn(/circular argument reference - var/) do + assert_raise(SyntaxError) do o.instance_eval("def foo(var: bar(var)) var end") end o = Object.new - assert_warn(/circular argument reference - var/) do + assert_raise(SyntaxError) do o.instance_eval("def foo(var: bar {var}) var end") end @@ -302,37 +298,33 @@ class TestSyntax < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_syntax.rb#L298 end def test_optional_self_reference - bug9593 = '[ruby-core:61299] [Bug #9593]' o = Object.new - assert_warn(/circular argument reference - var/) do + assert_raise(SyntaxError) do o.instance_eval("def foo(var = defined?(var)) var end") end - assert_equal(42, o.foo(42)) - assert_equal("local-variable", o.foo, bug9593) o = Object.new - assert_warn(/circular argument reference - var/) do + assert_raise(SyntaxError) do o.instance_eval("def foo(var = var) var end") end - assert_nil(o.foo, bug9593) o = Object.new - assert_warn(/circular argument reference - var/) do + assert_raise(SyntaxError) do o.instance_eval("def foo(var = bar(var)) var end") end o = Object.new - assert_warn(/circular argument reference - var/) do + assert_raise(SyntaxError) do o.instance_eval("def foo(var = bar {var}) var end") end o = Object.new - assert_warn(/circular argument reference - var/) do + assert_raise(SyntaxError) do o.instance_eval("def foo(var = (def bar;end; var)) var end") end o = Object.new - assert_warn(/circular argument reference - var/) do + assert_raise(SyntaxError) do o.instance_eval("def foo(var = (def self.bar;end; var)) var end") end -- cgit v0.10.2 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/