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

Miscompilation with binary pattern matching and select_val #8122

Closed
michalmuskala opened this issue Feb 15, 2024 · 1 comment
Closed

Miscompilation with binary pattern matching and select_val #8122

michalmuskala opened this issue Feb 15, 2024 · 1 comment
Labels
bug Issue is reported as a bug

Comments

@michalmuskala
Copy link
Contributor

michalmuskala commented Feb 15, 2024

Describe the bug
Given the following code:

-module(test).

-export([test/1]).

-define(is_valid(B), B =:= 1; B =:= 2).
-define(is_not_valid(B), B =:= 0; B =:= 3).

test(Bin) when is_binary(Bin) -> test1(Bin, 0).

test1(<<B1, B2, Rest/binary>>, N) when ?is_valid(B1), ?is_valid(B2) -> 
    test1(Rest, N + 2);
test1(<<B, Rest/binary>>, N) when ?is_valid(B) ->
    test1(Rest, N + 1);
test1(<<B, _Rest/binary>>, N) when ?is_not_valid(B) ->
    error(N);
test1(_, N) -> N.

The code is not behaving as expected when called as:

1> test:test(<<1, 1, 1, 1, 1>>).
5
2> test:test(<<1, 0, 1, 1, 1>>).
5
3>

The second call, that includes the number 0 should clearly trigger the error clause and result in an exception.

Importantly, removing the multi-byte case (first clause) results in correct behaviour.

Affected versions
Recent master

Additional context
This was discovered while working on further optimising #8111

@michalmuskala michalmuskala added the bug Issue is reported as a bug label Feb 15, 2024
@michalmuskala
Copy link
Contributor Author

False alarm - turns out macros, ;, and , have precedence implications I didn't foresee.
This was parsed as when (B1 =:= 1); (B1 =:= 2, B2 =:= 1); (B2 =:= 2) instead of
when (B1 =:= 1; B1 =:= 2), (B2 =:= 1; B2 =:= 2) that I expected.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Issue is reported as a bug
Projects
None yet
Development

No branches or pull requests

1 participant