[ruby-core:127581] [Ruby Bug#22599] parse.y rejects valid retry after a class / class << self definition inside rescue (`Invalid retry after ensure`)
Issue #22599 has been reported by steviejj (Steven Johnstone). ---------------------------------------- Bug #22599: parse.y rejects valid retry after a class / class << self definition inside rescue (`Invalid retry after ensure`) https://bugs.ruby-lang.org/issues/22599 * Author: steviejj (Steven Johnstone) * Status: Open * ruby -v: ruby 4.0.9 (2026-10-23 revision 3e93b23afc) +PRISM [arm64-darwin27] * Backport: 3.3: UNKNOWN, 3.4: UNKNOWN, 4.0: UNKNOWN ---------------------------------------- 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]` ```ruby # 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`): ```ruby 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`): ```ruby 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) ``` -- https://bugs.ruby-lang.org/
Issue #22599 has been updated by k0kubun (Takashi Kokubun). 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 commit:2f818d7ecffb3d4a0c6dd12e8a2644337bb34e65 merged revision(s) commit:34d551749bedd8f55154e7d09fb7d2b41b6cf52c. ---------------------------------------- Bug #22599: parse.y rejects valid retry after a class / class << self definition inside rescue (`Invalid retry after ensure`) https://bugs.ruby-lang.org/issues/22599#change-120119 * Author: steviejj (Steven Johnstone) * Status: Closed * ruby -v: ruby 4.0.9 (2026-10-23 revision 3e93b23afc) +PRISM [arm64-darwin27] * Backport: 3.3: REQUIRED, 3.4: REQUIRED, 4.0: DONE ---------------------------------------- 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]` ```ruby # 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`): ```ruby 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`): ```ruby 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) ``` -- https://bugs.ruby-lang.org/
participants (2)
-
k0kubun (Takashi Kokubun) -
steviejj (Steven Johnstone)