Project

General

Profile

Actions

Bug #21001

closed

unexpected nil result from proc with ensure and next

Added by aharpervc (Andrew Harper) about 1 month ago. Updated 28 days ago.

Status:
Closed
Assignee:
Target version:
-
ruby -v:
ruby 3.4.1 (2024-12-25 revision 48d4efcb85) +PRISM [x86_64-linux]
[ruby-core:120472]

Description

Hello,

I have noticed a different program behavior in Ruby 3.4 compared to previous versions. This is a simplified example for debugging that returns the expected result in Ruby 3.3 but nil in Ruby 3.4

Here is the example program:

puts "Ruby: #{RUBY_VERSION}"

# okay all versions
# RUN_ARRAY = [1]

# fails ruby 3.4 only
RUN_ARRAY = [1,2]

MAP_PROC = Proc.new do |&blk|
  block_results = []
  RUN_ARRAY.each do |value|
    block_value = blk.call(value)
    block_results.push block_value
  end
  block_results
ensure
  next block_results
end

test_fn_results = MAP_PROC.call do |value|
  break if value > 1
  next value
end

puts test_fn_results.inspect

For Ruby 3.3.6 the output is:

Ruby: 3.3.6
[1]

For Ruby 3.4.1 the output is:

Ruby: 3.4.1
nil

I am expecting the Ruby 3.3 output even for Ruby 3.4. This appears to be a bug, since the program implementation is unchanged. Perhaps there is a bug in the code that is now only showing up with Ruby 3.4, but that seemed unlikely since it's working as expected in other versions.

Thank you

Updated by ydah (Yudai Takada) about 1 month ago · Edited

memo) This does not occur even in Ruby 3.4.1 when --parser=parse.y.

❯ ruby --parser=parse.y -e 'puts "Ruby: #{RUBY_VERSION}"

# okay all versions
# RUN_ARRAY = [1]

# fails ruby 3.4 only
RUN_ARRAY = [1,2]

MAP_PROC = Proc.new do |&blk|
  block_results = []
  RUN_ARRAY.each do |value|
    block_value = blk.call(value)
    block_results.push block_value
  end
  block_results
ensure
  next block_results
end

test_fn_results = MAP_PROC.call do |value|
  break if value > 1
  next value
end

puts test_fn_results.inspect'
Ruby: 3.4.1
[1]

Updated by kddnewton (Kevin Newton) about 1 month ago

  • Assignee set to prism
Actions #3

Updated by kddnewton (Kevin Newton) 30 days ago

  • Status changed from Open to Closed

Applied in changeset git|31905d9e23ec6d1fa2a52f1ef2533f2056e7c9fb.


Allow escaping from ensures through next

Fixes [Bug #21001]

Actions #4

Updated by alanwu (Alan Wu) 28 days ago

  • Backport changed from 3.1: UNKNOWN, 3.2: UNKNOWN, 3.3: UNKNOWN, 3.4: UNKNOWN to 3.1: UNKNOWN, 3.2: UNKNOWN, 3.3: UNKNOWN, 3.4: REQUIRED
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0