Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unexpected match with disjunction #99

Closed
gafter opened this issue Nov 9, 2023 · 1 comment · Fixed by #100
Closed

Unexpected match with disjunction #99

gafter opened this issue Nov 9, 2023 · 1 comment · Fixed by #100
Assignees
Labels

Comments

@gafter
Copy link
Member

gafter commented Nov 9, 2023

One would expect the following program to behave as described in the comments. Instead, it prints true four times.

using Match

function f(x)
    @match x begin
        ((1|y), y) => true
        _ => false
    end
end

println(f((1, 1))) # true: matches (y, y)
println(f((1, 2))) # true: matches (1, y)
println(f((2, 1))) # false
println(f((2, 2))) # true: matches (y, y)

At the very least we should produce a warning for the third println, rather than silently behaving in an unexpected way.

@gafter
Copy link
Member Author

gafter commented Nov 9, 2023

In case it isn't clear what is happening, there is no link between the two y variables in the pattern ((1|y), y) because 1|y doesn't bind the variable y.

A variable pattern (like y) matches anything and binds the variable to the input value when the variable is not previously bound in the pattern. Since the disjunctive pattern (1,y) does not bind y on each disjunct, y is not bound by 1|y.

Since it is unbound where each y appears, the second y matches anything.

@gafter gafter self-assigned this Nov 9, 2023
@gafter gafter added the bug label Nov 9, 2023
gafter added a commit that referenced this issue Dec 15, 2023
it is now an error to use it again later in the pattern,
or in a value expression. The reason is that the semantics
are too confusing. If you get this error, just rename the
pattern variable to avoid the conflict, or use a wildcard
instead.

This is technically a breaking change, but I'm guessing
that it will occur only rarely in practice. Therefore I
am only bumping the minor version number.

Fixes #99
gafter added a commit that referenced this issue Dec 19, 2023
….. (#100)

* Adjust tests to reflect changes in Julia 1.11 nightlies.
* When a pattern variable is defined on only one side of a disjunction,
it is now an error to use it again later in the pattern,
or in a value expression. The reason is that the semantics
are too confusing. If you get this error, just rename the
pattern variable to avoid the conflict, or use a wildcard
instead.

This is technically a breaking change, but I'm guessing
that it will occur only rarely in practice. Therefore I
am only bumping the minor version number.

Fixes #99
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant