Bug #4473

Calling return within begin still executes else

Added by Todd Huss about 3 years ago. Updated almost 3 years ago.

[ruby-core:<unknown>]
Status:Closed
Priority:Normal
Assignee:Yusuke Endoh
Category:-
Target version:1.9.3
ruby -v:- Backport:

Description

=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 testreturninmethod
begin
return
rescue
puts 'rescue executed'
else
puts 'else executed'
end
end
test
returninmethod
=end

ruby-1.9.2-return-else.rb Magnifier (167 Bytes) Todd Huss, 03/06/2011 04:40 PM

Associated revisions

Revision 32483
Added by Yusuke Endoh almost 3 years ago

  • parse.y (reducenodesgen): NODE_RETURN in rescue body must not be reduced when there is an else clause. This caused bizarre behavior in [Bug #4473] .

History

#1 Updated by Todd Huss about 3 years ago

=begin
The code didn't paste so well so I'm attaching the source code
=end

#2 Updated by Mayank Kohaley about 3 years ago

=begin
I can see the same issue happening on Windows also.
=end

#3 Updated by Koichi Sasada almost 3 years ago

  • ruby -v changed from ruby 1.9.2p136 (2010-12-25 revision 30365) [x86_64-darwin10.6.0] to -

Hi,

I recognize this issue. Matz, what do you think about it?

(2011/04/06 21:12), Mayank Kohaley wrote:

Issue #4473 has been updated by Mayank Kohaley.

I can see the same issue happening on Windows also.

Bug #4473: Calling return within begin still executes else
http://redmine.ruby-lang.org/issues/4473

Author: Todd Huss
Status: Open
Priority: Normal
Assignee:
Category:
Target version:
ruby -v: ruby 1.9.2p136 (2010-12-25 revision 30365) [x86_64-darwin10.6.0]

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 testreturninmethod
begin
return
rescue
puts 'rescue executed'
else
puts 'else executed'
end
end
test
returninmethod

--
// SASADA Koichi at atdot dot net

#4 Updated by Yukihiro Matsumoto almost 3 years ago

Hi,

In message "Re: Re: [Ruby 1.9 - Bug #4473] Calling return within begin still executes else"
on Fri, 10 Jun 2011 22:36:19 +0900, SASADA Koichi ko1@atdot.net writes:

|I recognize this issue. Matz, what do you think about it?

I think it should print nothing, like 1.8 does.

                        matz.

#5 Updated by Koichi Sasada almost 3 years ago

  • Assignee set to Koichi Sasada

Yugui-san,

This fix will introduce incompatibility from 1.9.2. Is it acceptable?

#6 Updated by Koichi Sasada almost 3 years ago

(2011/06/10 22:56), Yukihiro Matsumoto wrote:

In message "Re: Re: [Ruby 1.9 - Bug #4473] Calling return within begin still executes else"
on Fri, 10 Jun 2011 22:36:19 +0900, SASADA Koichi ko1@atdot.net writes:

|I recognize this issue. Matz, what do you think about it?

I think it should print nothing, like 1.8 does.

OK. I'll check it.

--
// SASADA Koichi at atdot dot net

#7 Updated by Nobuyoshi Nakada almost 3 years ago

I bet that it is a bug in 1.8.

#8 Updated by Koichi Sasada almost 3 years ago

(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?

--
// SASADA Koichi at atdot dot net

#9 Updated by Yukihiro Matsumoto almost 3 years ago

Hi,

In message "Re: Re: [Ruby 1.9 - Bug #4473] Calling return within begin still executes else"
on Mon, 13 Jun 2011 23:04:46 +0900, SASADA Koichi ko1@atdot.net writes:

|(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.

                        matz.

#10 Updated by Hiroshi Nakamura almost 3 years ago

  • Target version set to 1.9.3

#11 Updated by Yusuke Endoh almost 3 years ago

  • 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 @@ reducenodesgen(struct parserparams parser, NODE *body)
if (!subnodes(nd
head, ndresq)) goto end;
break;
case NODE
RESCUE:
+ if (node->ndelse) {
+ body = &node->nd
resq;
+ break;
+ }
if (!subnodes(ndhead, ndresq)) goto end;
break;
default:

Yusuke Endoh mame@tsg.ne.jp

#12 Updated by Yusuke Endoh almost 3 years ago

  • Status changed from Open to Closed
  • % Done changed from 0 to 100

This issue was solved with changeset r32483.
Todd, thank you for reporting this issue.
Your contribution to Ruby is greatly appreciated.
May Ruby be with you.


  • parse.y (reducenodesgen): NODE_RETURN in rescue body must not be reduced when there is an else clause. This caused bizarre behavior in [Bug #4473] .

Also available in: Atom PDF