Skip to content

Commit 7b05138

Browse files
committed
[GR-15311] Allow rescue/else/ensure clauses in do...end blocks.
PullRequest: truffleruby/809
2 parents 2a67033 + ffb6576 commit 7b05138

File tree

9 files changed

+3931
-4022
lines changed

9 files changed

+3931
-4022
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ Compatibility:
2525
* `StringScanner` will now match a regexp beginning with `^` even when not scanning from the start of the string.
2626
* `Module#define_method` is now public like in MRI.
2727
* `Kernel#warn` now supports the `uplevel:` keyword argument.
28+
* `do...end` blocks can now have `rescue/else/ensure` clauses like MRI (#1618).
2829

2930
# 1.0 RC 15, 5 April 2019
3031

spec/ruby/language/block_spec.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,12 @@ def m(a) yield a end
217217
it "does not raise an exception when values are yielded" do
218218
@y.s(0) { 1 }.should == 1
219219
end
220+
221+
ruby_version_is "2.5" do
222+
it "may include a rescue clause" do
223+
eval("@y.z do raise ArgumentError; rescue ArgumentError; 7; end").should == 7
224+
end
225+
end
220226
end
221227

222228
describe "taking || arguments" do
@@ -227,6 +233,12 @@ def m(a) yield a end
227233
it "does not raise an exception when values are yielded" do
228234
@y.s(0) { || 1 }.should == 1
229235
end
236+
237+
ruby_version_is "2.5" do
238+
it "may include a rescue clause" do
239+
eval('@y.z do || raise ArgumentError; rescue ArgumentError; 7; end').should == 7
240+
end
241+
end
230242
end
231243

232244
describe "taking |a| arguments" do
@@ -252,6 +264,12 @@ def m(a) yield a end
252264
it "does not destructure a single Array value" do
253265
@y.s([1, 2]) { |a| a }.should == [1, 2]
254266
end
267+
268+
ruby_version_is "2.5" do
269+
it "may include a rescue clause" do
270+
eval('@y.s(1) do |x| raise ArgumentError; rescue ArgumentError; 7; end').should == 7
271+
end
272+
end
255273
end
256274

257275
describe "taking |a, b| arguments" do

spec/ruby/language/lambda_spec.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ def create_lambda
2222
-> () { }.lambda?.should be_true
2323
end
2424

25+
ruby_version_is "2.5" do
26+
it "may include a rescue clause" do
27+
eval('-> do raise ArgumentError; rescue ArgumentError; 7; end').should be_an_instance_of(Proc)
28+
end
29+
end
30+
2531
it "has its own scope for local variables" do
2632
l = -> arg {
2733
var = arg
@@ -305,6 +311,13 @@ def obj.define
305311
lambda { lambda }.should raise_error(ArgumentError)
306312
end
307313

314+
ruby_version_is "2.5" do
315+
it "may include a rescue clause" do
316+
eval('lambda do raise ArgumentError; rescue ArgumentError; 7; end').should be_an_instance_of(Proc)
317+
end
318+
end
319+
320+
308321
context "with an implicit block" do
309322
before do
310323
def meth; lambda; end

spec/tags/language/ensure_tags.txt

Lines changed: 0 additions & 5 deletions
This file was deleted.

spec/tags/language/rescue_tags.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
fails:The rescue keyword allows 'rescue' in method arguments
22
fails:The rescue keyword raises SyntaxError when else is used without rescue and ensure
3-
fails:The rescue keyword allows rescue in 'do end' block

src/main/java/org/truffleruby/parser/parser/RubyParser.java

Lines changed: 208 additions & 208 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/main/java/org/truffleruby/parser/parser/RubyParser.y

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1742,13 +1742,13 @@ f_larglist : tLPAREN2 f_args opt_bv_decl tRPAREN {
17421742
lambda_body : tLAMBEG compstmt tRCURLY {
17431743
$$ = $2;
17441744
}
1745-
| kDO_LAMBDA compstmt kEND {
1745+
| kDO_LAMBDA bodystmt kEND {
17461746
$$ = $2;
17471747
}
17481748

17491749
do_block : kDO_BLOCK {
17501750
support.pushBlockScope();
1751-
} opt_block_param compstmt kEND {
1751+
} opt_block_param bodystmt kEND {
17521752
$$ = new IterParseNode($1, $3, $4, support.getCurrentScope());
17531753
support.popCurrentScope();
17541754
}
@@ -1826,7 +1826,7 @@ brace_block : tLCURLY {
18261826
}
18271827
| kDO {
18281828
support.pushBlockScope();
1829-
} opt_block_param compstmt kEND {
1829+
} opt_block_param bodystmt kEND {
18301830
$$ = new IterParseNode($1, $3, $4, support.getCurrentScope());
18311831
support.popCurrentScope();
18321832
}

src/main/java/org/truffleruby/parser/parser/YyTables.java

Lines changed: 3688 additions & 3800 deletions
Large diffs are not rendered by default.

test/mri/excludes/TestSyntax.rb

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,6 @@
2121
exclude :test_keyword_invalid_name, "needs investigation"
2222
exclude :test_methoddef_in_cond, "needs investigation"
2323
exclude :test_parenthesised_statement_argument, "needs investigation"
24-
exclude :test_rescue_do_end_raised, "needs investigation"
2524
exclude :test_return_toplevel, "needs investigation"
2625
exclude :test_heredoc_newline, "needs investigation"
2726
exclude :test_method_call_location, "needs investigation"
28-
exclude :test_rescue_do_end_ensure_in_lambda, "needs investigation"
29-
exclude :test_rescue_do_end_ensure_result, "needs investigation"
30-
exclude :test_rescue_do_end_no_raise, "needs investigation"
31-
exclude :test_rescue_do_end_rescued, "needs investigation"

0 commit comments

Comments
 (0)