You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Alternation capture check rejects underscore names and captures from other scopes #4235
Since the parse-time check for captures inside alternative patterns (ruby/ruby#14923, ruby/ruby@8d0105824f), Prism rejects these once any capture exists earlier in the same pattern:
case[5,1]in[x,1 | _a]# variable capture in alternative patternin[x,_a | 1]# samein[x,1 | _]# same, even for a bare `_`in[x,"#{case1;iny;y;end}" | 2]# samein[x,->{case1;iny;y;end} | 2]# samein[x, ^(case0;iny;y;end) | 2]# sameend
Without the sibling capture, the same alternations are accepted (in [1 | _a], in ["#{case 1; in y; y; end}" | 2]). Prism 3.4.9 accepts all of them, and so does parse.y with ruby/ruby#18830 applied. doc/syntax/pattern_matching.rdoc in ruby/ruby says variables starting with _ are the one exception to the alternation rule, and test/ruby/test_pattern_matching.rb there already runs in _ | _a.
As far as I can tell, parse_pattern_primitives only runs parse_pattern_alternation_error on an operand when captures->size is non-zero, and that visitor flags every LocalVariableTargetNode it reaches. It does not skip underscore-prefixed targets the way parse_pattern_capture does, and it descends into embedded statements ("#{...}"), lambdas and pin expressions (^(...)), which are separate scopes as far as the surrounding pattern is concerned. I would expect only the non-underscore captures that belong to the alternation's own operands to be reported, which is what compile.c checks at runtime.
$ ruby -v
ruby 4.0.4 (2026-05-12 revision b89eb1bcbf) +PRISM [arm64-darwin25]
$ ruby --parser=prism -c t.rb # errors above
$ ruby --parser=parse.y -c t.rb # Syntax OK with ruby/ruby#18830, otherwise fails for the unrelated sibling reason in https://bugs.ruby-lang.org/issues/22310
Activity