Skip to content

Error on undefined variables in bitstring segments #8598

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

Merged
merged 4 commits into from
Jan 5, 2019
Merged

Error on undefined variables in bitstring segments #8598

merged 4 commits into from
Jan 5, 2019

Conversation

fertapric
Copy link
Member

Closes #8589

Binary/bitstring matching allows to dynamically define the size of the binary in certain conditions:

  • if the size variable is defined prior to the pattern match:

    iex> size = 8
    iex> <<a::size(size), rest::binary>> = "hello"
    iex> a
    104
    
  • if the size variable is matched within the same binary/bitstring match, prior to its use:

    iex> <<name_size::size(8), name::binary-size(name_size), _rest::binary>> = <<5, "Frank the Walrus">>
    iex> name
    "Frank"
    

Other cases are considered illegal patterns, for example:

{name_size, <<name::binary-size(name_size), _rest::binary>>} = {5, "Frank the Walrus"}

This commit raises a CompileError: undefined variable ... for such cases.

Binary/bitstring matching allows to dynamically define the
`size` of the binary in certain conditions:

  * if the `size` variable is defined prior to the pattern
    match:

        iex> size = 8
        iex> <<a::size(size), rest::binary>> = "hello"
        iex> a
        104

  * if the `size` variable is matched within the same
    binary/bitstring match, prior to its use:

        iex> <<name_size::size(8), name::binary-size(name_size), _rest::binary>> = <<5, "Frank the Walrus">>
        iex> name
        "Frank"

Other cases are considered illegal patterns, for example:

    {name_size, <<name::binary-size(name_size), _rest::binary>>} = {5, "Frank the Walrus"}

This commit raises a `CompileError: undefined variable ...` for such cases.
@josevalim josevalim merged commit 7ad213b into elixir-lang:master Jan 5, 2019
josevalim pushed a commit that referenced this pull request Jan 5, 2019
Binary/bitstring matching allows to dynamically define the
`size` of the binary in certain conditions:

  * if the `size` variable is defined prior to the pattern
    match:

        iex> size = 8
        iex> <<a::size(size), rest::binary>> = "hello"
        iex> a
        104

  * if the `size` variable is matched within the same
    binary/bitstring match, prior to its use:

        iex> <<name_size::size(8), name::binary-size(name_size), _rest::binary>> = <<5, "Frank the Walrus">>
        iex> name
        "Frank"

Other cases are considered illegal patterns, for example:

    {name_size, <<name::binary-size(name_size), _rest::binary>>} = {5, "Frank the Walrus"}

This commit raises a `CompileError: undefined variable ...` for such cases.

Signed-off-by: José Valim <jose.valim@plataformatec.com.br>
@fertapric fertapric deleted the fertapric-binary-match-specs branch January 5, 2019 17:07
xinz added a commit to xinz/ex_aliyun_ots that referenced this pull request Jan 17, 2019
1, Since Elixir 1.8 involves binary/bitstring matching allows to dynamically define the `size` of the binary in certain conditions [details](elixir-lang/elixir#8598), some pattern matches in `plainbuffer` need to keep update;
2, Upgrade `meck` unit test lib to fix runtime crash issue in Elixir 1.8.

The above updates works fine in Elixir 1.7.4 as well.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants