Skip to content

Analyze a pattern guard after the pattern binds its variables - #495

Merged
mame merged 2 commits into
ruby:masterfrom
mame:fix-pattern-guard-order
Sep 28, 2026
Merged

mame merged 2 commits into
ruby:masterfrom
mame:fix-pattern-guard-order

Conversation

@mame

@mame mame commented Sep 28, 2026

Copy link
Copy Markdown
Member

Found while reviewing #483.

A guard was analyzed before the guarded pattern, so a variable bound by the pattern was still nil in the guard:

case v
in [x] if x.even?   # => undefined method: nil#even?
end

Ruby evaluates the guard after the pattern matches, so this installs the pattern first. The same applies to unless guards.

mame and others added 2 commits September 28, 2026 19:50
IfPatternNode installed the guard before the guarded pattern, so a
variable bound by the pattern was still nil in the guard, and a common
guard like `in [x] if x.even?` reported "undefined method: nil#even?".
Ruby evaluates the guard after the pattern matches, so install the
pattern first.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@mame
mame enabled auto-merge (rebase) September 28, 2026 13:20
@mame
mame merged commit a375a78 into ruby:master Sep 28, 2026
6 checks passed
@mame
mame deleted the fix-pattern-guard-order branch September 28, 2026 13:24
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