Alternation capture check rejects underscore names and captures from other scopes
Les mainteneurs répondent en général sous 1 jour
Personne n'a encore pris cette issue.
Évaluation
- Difficulté
- 4/5
- Temps estimé
- 3-5 jours
- Accessibilité débutants
- 58/100
Piste de recherche
Start in parse_pattern_primitives and inspect parse_pattern_alternation_error alongside parse_pattern_capture. Check the existing pattern-matching coverage in test/ruby/test_pattern_matching.rb and verify that underscore captures are accepted while captures from embedded statements, lambdas, and pin expressions do not affect the surrounding alternation check.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Description
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 pattern
in [x, _a | 1] # same
in [x, 1 | _] # same, even for a bare `_`
in [x, "#{case 1; in y; y; end}" | 2] # same
in [x, -> { case 1; in y; y; end } | 2] # same
in [x, ^(case 0; in y; y; end) | 2] # same
end
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 https://github.com/ruby/ruby/pull/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
I ran into this while testing https://bugs.ruby-lang.org/issues/22310. I have a fix with tests and can open a PR.
- Langage dominant
- C
- Étoiles
- 1k
- Forks
- 194
- Merge moyen
- 2 j 9 h
- PR mergées (30 j)
- 15
Préparer son environnement
Par où commencer
- Lisez l'issue en entier, puis le guide de contribution du projet.
- Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
- Forkez le dépôt et travaillez sur une branche.
- Ouvrez une pull request qui référence le numéro de l'issue.
Autres issues de ruby/prism
-
Trailing comma in ASTOuverteenhancement
Difficulté 5/5 Plus d'une semaine Accessibilité débutants 35/100
ruby/prism#4091 · 1 commentaire · 1 réaction ·
Les mainteneurs répondent en général sous 1 jour
-
Revisit commentsOuverteenhancement
Difficulté 5/5 Plus d'une semaine Accessibilité débutants 30/100
ruby/prism#4044 · 4 commentaires ·
Les mainteneurs répondent en général sous 1 jour
-
java
Difficulté 5/5 Plus d'une semaine Accessibilité débutants 25/100
ruby/prism#4040 · 5 commentaires ·
Les mainteneurs répondent en général sous 1 jour
-
Release official Java APIOuvertejava
Difficulté 5/5 Plus d'une semaine Accessibilité débutants 25/100
ruby/prism#4018 · 10 commentaires ·
Les mainteneurs répondent en général sous 1 jour
-
java
Difficulté 5/5 Plus d'une semaine Accessibilité débutants 25/100
ruby/prism#3959 · 15 commentaires ·
Les mainteneurs répondent en général sous 1 jour
Toutes les issues de ruby/prism
Issues similaires
-
bug needs triage
Difficulté 2/5 1-3 heures Accessibilité débutants 78/100
netdata/netdata#24062 · 1 commentaire ·
Les mainteneurs répondent en général sous 1 jour
-
Difficulté 2/5 1-3 heures Accessibilité débutants 76/100
-
Difficulté 2/5 1-3 heures Accessibilité débutants 88/100
BasedHardware/omi#19463 ·
Les mainteneurs répondent en général sous 1 jour
-
Difficulté 2/5 1-3 heures Accessibilité débutants 72/100
EchoTools/nevr-runtime#30 ·
-
Difficulté 2/5 1-3 heures Accessibilité débutants 88/100
riscv-software-src/riscv-isa-sim#2448 ·
Les mainteneurs répondent en général sous 2 jours