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)

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@62901 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2018-03-23 00:40:08 +00:00
parent 631272ff15
commit 140512d222
4 changed files with 17 additions and 8 deletions

2
NEWS
View file

@ -18,6 +18,8 @@ with all sufficient information, see the ChangeLog file or Redmine
* refinements take place at block passing. [Feature #14223] * refinements take place at block passing. [Feature #14223]
* `else` without `rescue` is now causes a syntax error. [EXPERIMENTAL]
=== Core classes updates (outstanding ones only) === Core classes updates (outstanding ones only)
* Array * Array

View file

@ -1000,7 +1000,7 @@ bodystmt : compstmt
$$ = NEW_RESCUE($1, $2, $3, &@$); $$ = NEW_RESCUE($1, $2, $3, &@$);
} }
else if ($3) { else if ($3) {
rb_warn0("else without rescue is useless"); compile_error(p, "else without rescue is useless");
$$ = block_append(p, $$, $3); $$ = block_append(p, $$, $3);
} }
if ($4) { if ($4) {

View file

@ -195,8 +195,7 @@ describe "The rescue keyword" do
ScratchPad.recorded.should == [:one, :else_ran, :ensure_ran, :outside_begin] ScratchPad.recorded.should == [:one, :else_ran, :ensure_ran, :outside_begin]
end end
it "will execute an else block even without rescue and ensure" do else_without_rescue = lambda {
lambda {
eval <<-ruby eval <<-ruby
begin begin
ScratchPad << :begin ScratchPad << :begin
@ -204,10 +203,19 @@ describe "The rescue keyword" do
ScratchPad << :else ScratchPad << :else
end end
ruby ruby
}.should complain(/else without rescue is useless/) }
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] ScratchPad.recorded.should == [:begin, :else]
end 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 it "will not execute an else block if an exception was raised" do
result = begin result = begin

View file

@ -14,13 +14,12 @@ class TestParse < Test::Unit::TestCase
end end
def test_else_without_rescue def test_else_without_rescue
x = eval <<-END, nil, __FILE__, __LINE__+1 assert_syntax_error(<<-END, /else without rescue/)
begin begin
else else
42 42
end end
END END
assert_equal(42, x)
end end
def test_alias_backref def test_alias_backref