ruby-changes:50726
From: nobu <ko1@a...>
Date: Fri, 23 Mar 2018 09:40:14 +0900 (JST)
Subject: [ruby-changes:50726] nobu:r62901 (trunk): parse.y: else without rescue
nobu 2018-03-23 09:40:08 +0900 (Fri, 23 Mar 2018) New Revision: 62901 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=62901 Log: parse.y: else without rescue * parse.y (bodystmt): [EXPERIMENTAL] make `else` without `rescue` a syntax error. [DevelopersMeeting20180315Japan] https://bugs.ruby-lang.org/projects/ruby/wiki/DevelopersMeeting20180315Japan https://docs.google.com/document/d/1RT0ijSo8uJ4Awn3CEvuYkjH0TVeXSYgeAFNmVGYC3ak/edit# > * do-else-end > https://twitter.com/joker1007/status/974173396006129664 > https://twitter.com/joker1007/status/974173641347756032 > https://twitter.com/joker1007/status/974176512554369027 > Will be SyntaxError in 2.6-preview2 > All of begin/do/def (experimental) Modified files: trunk/NEWS trunk/parse.y trunk/spec/ruby/language/rescue_spec.rb trunk/test/ruby/test_parse.rb Index: NEWS =================================================================== --- NEWS (revision 62900) +++ NEWS (revision 62901) @@ -18,6 +18,8 @@ with all sufficient information, see the https://github.com/ruby/ruby/blob/trunk/NEWS#L18 * refinements take place at block passing. [Feature #14223] +* `else` without `rescue` is now causes a syntax error. [EXPERIMENTAL] + === Core classes updates (outstanding ones only) * Array Index: test/ruby/test_parse.rb =================================================================== --- test/ruby/test_parse.rb (revision 62900) +++ test/ruby/test_parse.rb (revision 62901) @@ -14,13 +14,12 @@ class TestParse < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_parse.rb#L14 end def test_else_without_rescue - x = eval <<-END, nil, __FILE__, __LINE__+1 + assert_syntax_error(<<-END, /else without rescue/) begin else 42 end END - assert_equal(42, x) end def test_alias_backref Index: parse.y =================================================================== --- parse.y (revision 62900) +++ parse.y (revision 62901) @@ -1000,7 +1000,7 @@ bodystmt : compstmt https://github.com/ruby/ruby/blob/trunk/parse.y#L1000 $$ = NEW_RESCUE($1, $2, $3, &@$); } else if ($3) { - rb_warn0("else without rescue is useless"); + compile_error(p, "else without rescue is useless"); $$ = block_append(p, $$, $3); } if ($4) { Index: spec/ruby/language/rescue_spec.rb =================================================================== --- spec/ruby/language/rescue_spec.rb (revision 62900) +++ spec/ruby/language/rescue_spec.rb (revision 62901) @@ -195,18 +195,26 @@ describe "The rescue keyword" do https://github.com/ruby/ruby/blob/trunk/spec/ruby/language/rescue_spec.rb#L195 ScratchPad.recorded.should == [:one, :else_ran, :ensure_ran, :outside_begin] end - it "will execute an else block even without rescue and ensure" do - lambda { - eval <<-ruby + else_without_rescue = lambda { + eval <<-ruby begin ScratchPad << :begin else ScratchPad << :else end ruby - }.should complain(/else without rescue is useless/) + } - ScratchPad.recorded.should == [:begin, :else] + ruby_version_is ""..."2.6" do + it "will execute an else block even without rescue and ensure" do + else_without_rescue.should complain(/else without rescue is useless/) + + ScratchPad.recorded.should == [:begin, :else] + end + end + + ruby_version_is "2.6" do + else_without_rescue.should raise_error(SyntaxError) end it "will not execute an else block if an exception was raised" do -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/