Actions
Bug #21823
open$! is nil inside rescue block when $! is accessed before raise in Ruby::Box
Bug #21823:
$! is nil inside rescue block when $! is accessed before raise in Ruby::Box
ruby -v:
ruby 4.0.0 (2025-12-25 revision 553f1675f3) +PRISM [arm64-darwin25]
Tags:
Description
Description¶
When Ruby::Box is enabled (RUBY_BOX=1), accessing $! before a raise statement causes $! to be nil inside the subsequent rescue block, instead of containing the raised exception.
Reproduction¶
# test.rb
begin
$! # Accessing $! before raise triggers the bug
raise "test error"
rescue
puts "$! = #{$!.inspect}"
end
Without Ruby::Box:¶
$ ruby test.rb
$! = #<RuntimeError: test error>
With Ruby::Box:¶
$ RUBY_BOX=1 ruby test.rb
$! = nil
Expected behavior¶
$! should contain the raised exception (#<RuntimeError: test error>) inside the rescue block, regardless of whether $! was accessed before the raise.
Actual behavior¶
When RUBY_BOX=1 is set and $! is accessed before raise, $! becomes nil inside the rescue block.
Updated by nobu (Nobuyoshi Nakada) 2 days ago
- Tags set to box
- Assignee set to tagomoris (Satoshi Tagomori)
- Backport changed from 3.2: UNKNOWN, 3.3: UNKNOWN, 3.4: UNKNOWN, 4.0: UNKNOWN to 3.2: DONTNEED, 3.3: DONTNEED, 3.4: DONTNEED, 4.0: REQUIRED
Actions