Bug #22599
closedparse.y rejects valid retry after a class / class << self definition inside rescue (`Invalid retry after ensure`)
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):
Updated by nobu (Nobuyoshi Nakada) about 15 hours ago
- 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
- 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
- Backport changed from 3.3: REQUIRED, 3.4: REQUIRED, 4.0: REQUIRED to 3.3: REQUIRED, 3.4: REQUIRED, 4.0: DONE
ruby_4_0 2f818d7ecffb3d4a0c6dd12e8a2644337bb34e65 merged revision(s) 34d551749bedd8f55154e7d09fb7d2b41b6cf52c.