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)

Updated by nobu (Nobuyoshi Nakada) about 15 hours ago Actions #1

  • Backport changed from 3.3: UNKNOWN, 3.4: UNKNOWN, 4.0: UNKNOWN to 3.3: REQUIRED, 3.4: REQUIRED, 4.0: REQUIRED

Updated by nobu (Nobuyoshi Nakada) about 14 hours ago Actions #2

  • Status changed from Open to Closed

Applied in changeset git|34d551749bedd8f55154e7d09fb7d2b41b6cf52c.


[Bug #22599] Restore in_rescue mode after class/module blocks

Updated by k0kubun (Takashi Kokubun) about 11 hours ago Actions #3 [ruby-core:127600]

  • Backport changed from 3.3: REQUIRED, 3.4: REQUIRED, 4.0: REQUIRED to 3.3: REQUIRED, 3.4: REQUIRED, 4.0: DONE
Actions

Also available in: PDF Atom