aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--NEWS2
-rw-r--r--parse.y2
-rw-r--r--spec/ruby/language/rescue_spec.rb18
-rw-r--r--test/ruby/test_parse.rb3
4 files changed, 17 insertions, 8 deletions
diff --git a/NEWS b/NEWS
index 3769b80cde..a8e1874939 100644
--- a/NEWS
+++ b/NEWS
@@ -18,6 +18,8 @@ with all sufficient information, see the ChangeLog file or Redmine
* 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
diff --git a/parse.y b/parse.y
index f838d3cb05..5f7bd2abe2 100644
--- a/parse.y
+++ b/parse.y
@@ -1000,7 +1000,7 @@ bodystmt : compstmt
$$ = 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) {
diff --git a/spec/ruby/language/rescue_spec.rb b/spec/ruby/language/rescue_spec.rb
index b3de84468b..66a493cd34 100644
--- a/spec/ruby/language/rescue_spec.rb
+++ b/spec/ruby/language/rescue_spec.rb
@@ -195,18 +195,26 @@ describe "The rescue keyword" do
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/)
+ }
+
+ 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
- ScratchPad.recorded.should == [:begin, :else]
+ 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
diff --git a/test/ruby/test_parse.rb b/test/ruby/test_parse.rb
index cd1c5a3ecc..e2dd179f9a 100644
--- a/test/ruby/test_parse.rb
+++ b/test/ruby/test_parse.rb
@@ -14,13 +14,12 @@ class TestParse < Test::Unit::TestCase
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