Skip to content

Fix unification variables not persisting in any-order nodes#399

Merged
marcandre merged 1 commit intomasterfrom
unification-fix
Mar 11, 2026
Merged

Fix unification variables not persisting in any-order nodes#399
marcandre merged 1 commit intomasterfrom
unification-fix

Conversation

@marcandre
Copy link
Contributor

@marcandre marcandre commented Mar 10, 2026

Unification variables inside any-order patterns (<>) were not working correctly due to Ruby's block scoping behavior. The pattern (or <(send _receiver :bar) (send _receiver :baz)>) would match foo.baz || foo.bar but fail to match foo.bar || foo.baz.

# Current (bad):
(matched = {}; true) &&
(2).times do
  when ... && (unify_receiver = value; true) ...  # Inside block - doesn't persist!
end

# Fixed:
(matched = {}; unify_receiver = nil; true) &&  # Initialize outside!
(2).times do
  when ... && (unify_receiver = value; true) ...  # Now assigns to existing variable
end

[Fixes #335]

@marcandre marcandre force-pushed the unification-fix branch 2 times, most recently from 22c47d0 to 34682ce Compare March 11, 2026 02:30
Unification variables inside any-order patterns (`<>`) were not working
correctly due to Ruby's block scoping behavior. The pattern
`(or <(send _receiver :bar) (send _receiver :baz)>)` would match
`foo.baz || foo.bar` but fail to match `foo.bar || foo.baz`.

```ruby
  # Current (bad):
  (matched = {}; true) &&
  (2).times do
    when ... && (unify_receiver = value; true) ...  # Inside block - doesn't persist!
  end

  # Good:
  (matched = {}; unify_receiver = nil; true) &&  # Initialize outside!
  (2).times do
    when ... && (unify_receiver = value; true) ...  # Now assigns to existing variable
  end
```

[Fixes #335]
@marcandre marcandre merged commit aea6f62 into master Mar 11, 2026
21 checks passed
@marcandre marcandre deleted the unification-fix branch March 11, 2026 04:12
@marcandre
Copy link
Contributor Author

Released as 1.49.1

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.

Unification does not work inside any-order node

1 participant