Alternation capture check rejects underscore names and captures from other scopes
Maintainer antworten meist innerhalb von 1 Tag
Dieses Issue hat noch niemand übernommen.
Bewertung
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Anfängerfreundlichkeit
- 58/100
Rechercherichtung
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.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Beschreibung
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.
- Vorherrschende Sprache
- C
- Sterne
- 1k
- Forks
- 194
- Ø Merge
- 2 T. 9 Std.
- Gemergte PRs (30 T.)
- 15
Entwicklungsumgebung
Erste Schritte
- Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
- Forken Sie das Repository und arbeiten Sie in einem Branch.
- Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.
Mehr aus ruby/prism
-
Trailing comma in ASTOffenenhancement
Schwierigkeit 5/5 Über eine Woche Anfängerfreundlichkeit 35/100
ruby/prism#4091 · 1 Kommentar · 1 Reaktion ·
Maintainer antworten meist innerhalb von 1 Tag
-
Revisit commentsOffenenhancement
Schwierigkeit 5/5 Über eine Woche Anfängerfreundlichkeit 30/100
ruby/prism#4044 · 4 Kommentare ·
Maintainer antworten meist innerhalb von 1 Tag
-
java
Schwierigkeit 5/5 Über eine Woche Anfängerfreundlichkeit 25/100
ruby/prism#4040 · 5 Kommentare ·
Maintainer antworten meist innerhalb von 1 Tag
-
java
Schwierigkeit 5/5 Über eine Woche Anfängerfreundlichkeit 25/100
ruby/prism#4018 · 10 Kommentare ·
Maintainer antworten meist innerhalb von 1 Tag
-
java
Schwierigkeit 5/5 Über eine Woche Anfängerfreundlichkeit 25/100
ruby/prism#3959 · 15 Kommentare ·
Maintainer antworten meist innerhalb von 1 Tag
Ähnliche Issues
-
bug needs triage
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 78/100
netdata/netdata#24062 · 1 Kommentar ·
Maintainer antworten meist innerhalb von 1 Tag
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 76/100
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 88/100
riscv-software-src/riscv-isa-sim#2448 ·
Maintainer antworten meist innerhalb von 2 Tagen
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 78/100
-
Schwierigkeit 1/5 Unter einer Stunde Anfängerfreundlichkeit 75/100
NabuCasa/silabs-firmware-builder#231 · 1 Kommentar ·