Project

General

Profile

Actions

Bug #20917

open

redo/next in nested begin block causes wrong order of execution

Added by hoshiumiarata (Arata Hoshiumi) 6 days ago. Updated 1 day ago.

Status:
Open
Assignee:
-
Target version:
-
ruby -v:
ruby 3.3.5 (2024-09-03 revision ef084cc8f4) [x86_64-darwin24]
[ruby-core:120034]

Description

It seems that redo/next in a nested begin block can cause the wrong order of execution.
For example:

for _ in [0]
  puts 0
  begin
    puts 1
    begin
      puts 2
      redo
    ensure
      puts 3
    end
  ensure
    puts 4
    break
  end
end

It prints:

0
1
2
3
4
3
4
=> nil

But I think it should print:

0
1
2
3
4
=> nil

Because execution order should be:

  1. puts 0
  2. puts 1
  3. puts 2
  4. redo
  5. unwind to nested ensure block
  6. puts 3
  7. unwind to outer ensure block
  8. puts 4
  9. break
  10. end of loop

Interestingly enough, if we add an empty rescue block before any of the ensure blocks, then the execution order is correct.

Updated by Eregon (Benoit Daloze) 6 days ago

Indeed, it seems like a bug, I think as well the "redo jump/unwind/exception" should run ensure's and get to the "break jump/unwind/exception" which should override the redo like when a Ruby exception overrides another.

FWIW, this is 0 1 2 3 4 on both TruffleRuby and JRuby.

Updated by kddnewton (Kevin Newton) 1 day ago

Agreed this is a bug. For additional context, this is the same on parse.y and prism.

Actions

Also available in: Atom PDF

Like0
Like0Like0