Skip to content

Support an unless guard in a pattern - #483

Open
sinsoku wants to merge 1 commit into
ruby:masterfrom
sinsoku:support-unless-guard-in-pattern
Open

Support an unless guard in a pattern#483
sinsoku wants to merge 1 commit into
ruby:masterfrom
sinsoku:support-unless-guard-in-pattern

Conversation

@sinsoku

@sinsoku sinsoku commented Sep 12, 2026

Copy link
Copy Markdown
Collaborator

Motivation / Background

This Pull Request has been created because an unless guard in a pattern aborts the analysis:

case 1
in Integer unless false
end
#=> unknown pattern node type: unless_node (RuntimeError)

An if guard has always worked. ruby/ruby uses unless guards in test/ruby/test_pattern_matching.rb and spec/ruby/language/pattern_matching_spec.rb.

Detail

IfPatternNode reads only the predicate and the guarded pattern, and the truth value of a guard is never used to narrow the subject, so if and unless need no separate nodes. A guard never has an else part either way, so the same sanity check covers both.

🤖 Generated with Claude Code

AST.create_pattern_node routed a guard to IfPatternNode only for
:if_node, so `in pat unless cond` raised "unknown pattern node type:
unless_node". IfPatternNode reads only the predicate and the guarded
pattern, so an unless guard needs no node of its own; only the sanity
check differs, because Prism names the else slot `subsequent` on IfNode
and `else_clause` on UnlessNode.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant