Skip to content

Error reusing variable with binary segment specifier #14737

@yastanotheruser

Description

@yastanotheruser

Elixir and Erlang/OTP versions

Erlang/OTP 27 [erts-15.2.7.1] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:1] [jit:ns]

Elixir 1.20.0-dev (dd45d96) (compiled with Erlang/OTP 27)

Operating system

Alpine Linux Edge

Current behavior

Attempting to match an already bound binary variable and binding the same name, in a binary segment whose specifier references the previously bound value, results in an Erlang interpreter/compiler error.

iex(1)> a = "123"
iex(2)> <<a::binary-size(byte_size(a) - 1), b>> = "123"
** (ErlangError) Erlang error: {:illegal_pattern, {:bin, 2, [{:bin_element, 2, {:var, 2, :_a@1}, {:var, 2, :_a@1}, [:binary]}, {:bin_element, 2, {:var, 2, :_b@1}, :default, [:integer]}]}}
    (stdlib 6.2.2.2) erl_eval.erl:1414: :erl_eval.match/6
    iex:2: (file)

Expected behavior

Either display an error indicating such pattern isn't allowed, or allow referencing the old value.

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions