Defining a struct and assigning a bitstring with bit size of less than 4 as a default value to one of its key produces this error:
iex> defmodule Struct do
...> defstruct bit: <<0::4>>
...> end
...>
{:module, Struct,
<<70, 79, 82, 49, 0, 0, 8, 252, 66, 69, 65, 77, 69, 120, 68, 99, 0, 0, 0, 186,
131, 104, 2, 100, 0, 14, 101, 108, 105, 120, 105, 114, 95, 100, 111, 99, 115,
95, 118, 49, 108, 0, 0, 0, 4, 104, 2, ...>>, %Struct{bit: <<0::size(4)>>}}
iex> %Struct{}
** (FunctionClauseError) no function clause matching in :elixir_bitstring.extract_bit_type/2
(elixir) src/elixir_bitstring.erl:226: :elixir_bitstring.extract_bit_type(4, [])
(elixir) src/elixir_bitstring.erl:220: :elixir_bitstring.extract_bit_info/2
(elixir) src/elixir_bitstring.erl:156: :elixir_bitstring.build_bitstr_each/5
(elixir) src/elixir_bitstring.erl:149: :elixir_bitstring.build_bitstr/4
(stdlib) lists.erl:1354: :lists.mapfoldl/3
(stdlib) lists.erl:1355: :lists.mapfoldl/3
When using a multiple of 8 as bit size, or when omitting the bit size, the result is as expected. No errors either when updating a struct key to <<0::4>>.
iex> %Struct{}
%Struct{bit: <<0::size(4)>>}
Closed via https://github.com/elixir-lang/elixir/commit/5904879412d57673d1c1b11fccfa83a9aa168feb.
Also fix is backported to v1.4.
Most helpful comment
Closed via https://github.com/elixir-lang/elixir/commit/5904879412d57673d1c1b11fccfa83a9aa168feb.
Also fix is backported to v1.4.