Issue #4473 has been updated by Yusuke Endoh.

Assignee changed from Koichi Sasada to Yusuke Endoh

Hello,

> |(2011/06/11 16:21), Nobuyoshi Nakada wrote:
> |> I bet that it is a bug in 1.8.
> |
> |Matz, could you give me a comment, again?
> 
> He didn't disclosed the reason why it was a 1.8 bug.  If he has
> a reasonable reason, I would accept it.


I have no idea which behavior is better.  But, at least, the
behavior of 1.9 seems to be caused by a wrong optimization:


  def foo
    begin
      return
    rescue
    else
      p :foo!
    end
  end

  foo #=> :foo!


  def foo
    begin
      return
    rescue
    else
      p :foo!
    end
    p :BOO  # dummy sentence
  end

  foo #=> nothing output, as OP expected


I'll commit the following patch unless there is objection.
Let's discuss the better spec in another thread, if needed.


diff --git a/parse.y b/parse.y
index 9769bb9..ffa649b 100644
--- a/parse.y
+++ b/parse.y
@@ -8713,6 +8713,10 @@ reduce_nodes_gen(struct parser_params *parser, NODE **body)
 	    if (!subnodes(nd_head, nd_resq)) goto end;
 	    break;
 	  case NODE_RESCUE:
+	    if (node->nd_else) {
+		body = &node->nd_resq;
+		break;
+	    }
 	    if (!subnodes(nd_head, nd_resq)) goto end;
 	    break;
 	  default:

-- 
Yusuke Endoh <mame / tsg.ne.jp>
----------------------------------------
Bug #4473: Calling return within begin still executes else
http://redmine.ruby-lang.org/issues/4473

Author: Todd Huss
Status: Open
Priority: Normal
Assignee: Yusuke Endoh
Category: 
Target version: 1.9.3
ruby -v: -


=begin
I see this issue in 1.9.2-p0 through the current version 1.9.2-p136 on Mac OS X. The following code prints 'else executed'. Whereas in Ruby 1.8.x it would not execute the else statement if you called return from within a begin block.

In summary, I would expect the following code to not print anything, but in Ruby 1.9.2 it actually prints 'else executed':

# This code prints 'else executed'
def test_return_in_method
  begin
    return
  rescue
    puts 'rescue executed'
  else
    puts 'else executed'
  end
end
test_return_in_method
=end



-- 
http://redmine.ruby-lang.org