ruby-changes:45303
From: nobu <ko1@a...>
Date: Thu, 19 Jan 2017 18:55:03 +0900 (JST)
Subject: [ruby-changes:45303] nobu:r57376 (trunk): parse.y: rescue/else/ensure in do-end
nobu 2017-01-19 18:54:58 +0900 (Thu, 19 Jan 2017) New Revision: 57376 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=57376 Log: parse.y: rescue/else/ensure in do-end * parse.y (do_body): allow rescue/else/ensure inside do/end blocks. [Feature #12906] Modified files: trunk/NEWS trunk/parse.y trunk/test/ruby/test_syntax.rb Index: NEWS =================================================================== --- NEWS (revision 57375) +++ NEWS (revision 57376) @@ -16,6 +16,8 @@ with all sufficient information, see the https://github.com/ruby/ruby/blob/trunk/NEWS#L16 * Top-level constant look-up is removed. [Feature #11547] +* rescue/else/ensure are allowed inside do/end blocks. [Feature #12906] + === Core classes updates (outstanding ones only) === Stdlib updates (outstanding ones only) Index: parse.y =================================================================== --- parse.y (revision 57375) +++ parse.y (revision 57376) @@ -3763,7 +3763,7 @@ brace_body : {$<vars>$ = dyna_push();} https://github.com/ruby/ruby/blob/trunk/parse.y#L3763 do_body : {$<vars>$ = dyna_push();} {$<val>$ = cmdarg_stack; CMDARG_SET(0);} - opt_block_param compstmt + opt_block_param bodystmt { $$ = new_do_body($3, $4); dyna_pop($<vars>1); Index: test/ruby/test_syntax.rb =================================================================== --- test/ruby/test_syntax.rb (revision 57375) +++ test/ruby/test_syntax.rb (revision 57376) @@ -979,6 +979,73 @@ eom https://github.com/ruby/ruby/blob/trunk/test/ruby/test_syntax.rb#L979 assert_in_out_err(%w[-e redo], "", [], /^-e:1: /) end + def test_rescue_do_end_raised + result = [] + assert_raise(RuntimeError) do + eval("#{<<-"begin;"}\n#{<<-"end;"}") + begin; + tap do + result << :begin + raise "An exception occured!" + ensure + result << :ensure + end + end; + end + assert_equal([:begin, :ensure], result) + end + + def test_rescue_do_end_rescued + result = [] + assert_nothing_raised(RuntimeError) do + eval("#{<<-"begin;"}\n#{<<-"end;"}") + begin; + tap do + result << :begin + raise "An exception occured!" + rescue + result << :rescue + else + result << :else + ensure + result << :ensure + end + end; + end + assert_equal([:begin, :rescue, :ensure], result) + end + + def test_rescue_do_end_no_raise + result = [] + assert_nothing_raised(RuntimeError) do + eval("#{<<-"begin;"}\n#{<<-"end;"}") + begin; + tap do + result << :begin + rescue + result << :rescue + else + result << :else + ensure + result << :ensure + end + end; + end + assert_equal([:begin, :else, :ensure], result) + end + + def test_rescue_do_end_ensure_result + result = eval("#{<<-"begin;"}\n#{<<-"end;"}") + begin; + proc do + :begin + ensure + :ensure + end.call + end; + assert_equal(:begin, result) + end + private def not_label(x) @result = x; @not_label ||= nil end -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/