Project

General

Profile

Actions

Bug #22599

closed

parse.y rejects valid retry after a class / class << self definition inside rescue (`Invalid retry after ensure`)

Bug #22599: parse.y rejects valid retry after a class / class << self definition inside rescue (`Invalid retry after ensure`)

Added by steviejj (Steven Johnstone) about 19 hours ago. Updated about 11 hours ago.

Status:
Closed
Assignee:
-
Target version:
-
ruby -v:
ruby 4.0.9 (2026-10-23 revision 3e93b23afc) +PRISM [arm64-darwin27]
[ruby-core:127581]

Description

In CRuby's parse.y, defining a class or singleton class (class A; end or class << self; end) inside a rescue clause causes a subsequent retry in the same rescue clause to fail compilation with SyntaxError: Invalid retry after ensure, even though no ensure clause exists. Defining a method (def tmp; end) inside rescue does not trigger this bug, and --parser=prism compiles and executes the retry cleanly. Discovered by differential fuzzing of prism against parse.y.

Reproduction

Tested on CRuby 4.1.0dev (2026-11-18 HEAD ed3e747aa4) and ruby 4.0.9 (2026-10-23 revision 3e93b23afc) +PRISM [arm64-darwin27]

# 1. Singleton class inside rescue:
n = 0
begin
  n += 1
  raise if n == 1
rescue
  class << self; end
  retry
end
p n

# --parser=prism:   outputs `2` (exit 0)
# --parser=parse.y: SyntaxError: Invalid retry after ensure (exit 1)

Same failure for a regular class definition (class TmpC; end):

n = 0
begin
  n += 1
  raise if n == 1
rescue
  class TmpC; end
  retry
end
p n

# --parser=prism:   outputs `2` (exit 0)
# --parser=parse.y: SyntaxError: Invalid retry after ensure (exit 1)

Control case (def instead of class):

n = 0
begin
  n += 1
  raise if n == 1
rescue
  def tmp_x; end
  retry
end
p n

# --parser=parse.y: outputs `2` (exit 0)
# --parser=prism:   outputs `2` (exit 0)
Actions

Also available in: PDF Atom