Elixir: Error when accessing a struct with a bitstring size < 8 as default value

Created on 15 Apr 2017  路  1Comment  路  Source: elixir-lang/elixir

Environment

  • Elixir & Erlang versions (elixir --version): Erlang/OTP 19 [erts-8.3] [source] [64-bit] [smp:4:4] [async-threads:10] [hipe] [kernel-poll:false] [dtrace], Elixir 1.4.2
  • Operating system: macOS 10.12.4

Current behavior

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>>.

Expected behavior

iex> %Struct{}
%Struct{bit: <<0::size(4)>>}
Elixir Bug

Most helpful comment

>All comments

Was this page helpful?
0 / 5 - 0 ratings

Related issues

pragdave picture pragdave  路  28Comments

michalmuskala picture michalmuskala  路  35Comments

dmorneau picture dmorneau  路  30Comments

josevalim picture josevalim  路  27Comments

jakubpawlowicz picture jakubpawlowicz  路  32Comments