Project

General

Profile

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

Added by ahogappa (sho hirano) 3 days ago. Updated 2 days ago.

Status:
Open
Target version:
-
ruby -v:
ruby 4.0.0 (2025-12-25 revision 553f1675f3) +PRISM [arm64-darwin25]
[ruby-core:<unknown>]
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.

Actions

Also available in: PDF Atom