Skip to content

Commit b64d3c5

Browse files
committed
Docs and tests
1 parent b07f978 commit b64d3c5

8 files changed

Lines changed: 35 additions & 28 deletions

File tree

lib/elixir/pages/cheatsheets/types-cheat.cheatmd

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ not type
3636
### Indivisible types
3737

3838
```elixir
39+
bitstring()
3940
binary()
4041
empty_list()
4142
integer()
@@ -45,6 +46,8 @@ port()
4546
reference()
4647
```
4748

49+
`binary()` is a subtype of `bitstring()`.
50+
4851
### Atoms
4952

5053
#### All atoms
@@ -167,24 +170,20 @@ tuple()
167170

168171
#### At least n-element tuples
169172

170-
```
173+
```elixir
171174
{binary(), binary(), ...}
172175
```
173176

174177
## Additional types for convenience
175178

176-
#### Aliases
179+
#### Common aliases
177180

178181
```elixir
179-
bitstring() = binary()
180182
boolean() = true or false
181183
number() = integer() or float()
182184
```
183185

184-
The type system currently does not distinguish between
185-
binaries and bitstrings.
186-
187-
#### Lists
186+
#### List aliases
188187

189188
```elixir
190189
list() = empty_list() or non_empty_list(term())

lib/elixir/pages/references/gradual-set-theoretic-types.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ The basic types are:
2424
```elixir
2525
atom()
2626
binary()
27+
bitstring()
2728
empty_list()
2829
integer()
2930
float()
@@ -56,7 +57,9 @@ In this section we will cover the syntax of all data types. At the moment, devel
5657

5758
### Indivisible types
5859

59-
These types are indivisibile and have no further representation. They are: `binary()`, `integer()`, `float()`, `pid()`, `port()`, `reference()`. For example, the numbers `1` and `42` are both represented by the type `integer()`.
60+
These types are indivisibile and have no further representation. They are: `binary()`, `bitstring()`, `integer()`, `float()`, `pid()`, `port()`, `reference()`. For example, the numbers `1` and `42` are both represented by the type `integer()`.
61+
62+
Note the `binary()` type is a subtype of the less frequently used `bitstring()` type, as binaries are bitstrings where the number of bits is divisible by 8.
6063

6164
### Atoms
6265

@@ -148,7 +151,7 @@ That's the same as specifying all lists:
148151
%{list() => integer() or binary()}
149152
```
150153

151-
The supported domain keys are `atom()`, `binary()`, `integer()`, `float()`, `fun()`, `list()`, `map()`, `pid()`, `port()`, `reference()`, and `tuple()`.
154+
The supported domain keys are `atom()`, `bitstring()`, `binary()`, `integer()`, `float()`, `fun()`, `list()`, `map()`, `pid()`, `port()`, `reference()`, and `tuple()`. In the case of maps, the `bitstring()` domain stores exclusively keys which are not binary. The ones which are `binary()` are stored under the `binary()` domain.
152155

153156
Furthermore, it is important to note that domain keys are, by definition, optional. Whenever you have a `%{integer() => integer()}`and you try to fetch a key, we must assume the key may not exist (after all, it is not possible to store all integers as map keys as they are infinite).
154157

lib/elixir/test/elixir/module/types/descr_test.exs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ defmodule Module.Types.DescrTest do
5454
atom(),
5555
integer(),
5656
float(),
57-
binary(),
57+
bitstring(),
5858
open_map(),
5959
non_empty_list(term(), term()),
6060
empty_list(),
@@ -182,7 +182,6 @@ defmodule Module.Types.DescrTest do
182182
) == closed_map(a: union(float(), integer()), b: atom())
183183

184184
# Optimization two: we can tell that one map is a subtype of the other:
185-
186185
assert union(
187186
closed_map(a: term(), b: term()),
188187
closed_map(a: float(), b: binary())
@@ -216,8 +215,7 @@ defmodule Module.Types.DescrTest do
216215
tuple([integer(), atom()])
217216
) == tuple([union(float(), integer()), atom()])
218217

219-
# Optimization two: we can tell that one tuple is a subtype of the other:
220-
218+
# Optimization two: we can tell that one tuple is a subtype of the other
221219
assert union(
222220
tuple([term(), term()]),
223221
tuple([float(), binary()])
@@ -2489,8 +2487,14 @@ defmodule Module.Types.DescrTest do
24892487

24902488
describe "to_quoted" do
24912489
test "bitmap" do
2490+
assert union(pid(), bitstring()) |> to_quoted_string() ==
2491+
"bitstring() or pid()"
2492+
24922493
assert union(integer(), union(float(), binary())) |> to_quoted_string() ==
24932494
"binary() or float() or integer()"
2495+
2496+
assert difference(bitstring(), binary()) |> union(integer()) |> to_quoted_string() ==
2497+
"(bitstring() and not binary()) or integer()"
24942498
end
24952499

24962500
test "none" do

lib/elixir/test/elixir/module/types/expr_test.exs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2040,10 +2040,10 @@ defmodule Module.Types.ExprTest do
20402040
end
20412041

20422042
describe "comprehensions" do
2043-
test "binary generators" do
2043+
test "bitstring generators" do
20442044
assert typeerror!([<<x>>], for(<<y <- x>>, do: y)) ==
20452045
~l"""
2046-
expected the right side of <- in a binary generator to be a binary:
2046+
expected the right side of <- in a binary generator to be a binary (or bitstring):
20472047
20482048
x
20492049
@@ -2066,7 +2066,7 @@ defmodule Module.Types.ExprTest do
20662066
for(<<i <- if(:rand.uniform() > 0.5, do: x, else: y)>>, do: i)
20672067
) =~
20682068
~l"""
2069-
expected the right side of <- in a binary generator to be a binary:
2069+
expected the right side of <- in a binary generator to be a binary (or bitstring):
20702070
20712071
if :rand.uniform() > 0.5 do
20722072
x
@@ -2092,14 +2092,14 @@ defmodule Module.Types.ExprTest do
20922092
"""
20932093
end
20942094

2095-
test "infers binary generators" do
2095+
test "infers bitstring generators" do
20962096
assert typecheck!(
20972097
[x],
20982098
(
20992099
for <<_ <- x>>, do: :ok
21002100
x
21012101
)
2102-
) == dynamic(binary())
2102+
) == dynamic(bitstring())
21032103
end
21042104

21052105
test ":into" do

lib/elixir/test/elixir/module/types/integration_test.exs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ defmodule Module.Types.IntegrationTest do
240240
end
241241

242242
assert itself_arg.(Itself.Atom) == dynamic(atom())
243-
assert itself_arg.(Itself.BitString) == dynamic(binary())
243+
assert itself_arg.(Itself.BitString) == dynamic(bitstring())
244244
assert itself_arg.(Itself.Float) == dynamic(float())
245245
assert itself_arg.(Itself.Function) == dynamic(fun())
246246
assert itself_arg.(Itself.Integer) == dynamic(integer())
@@ -583,7 +583,8 @@ defmodule Module.Types.IntegrationTest do
583583
dynamic(
584584
%Date{} or %DateTime{} or %NaiveDateTime{} or %Time{} or %URI{} or %Version{} or
585585
%Version.Requirement{}
586-
) or atom() or binary() or empty_list() or float() or integer() or non_empty_list(term(), term())
586+
) or atom() or bitstring() or empty_list() or float() or integer() or
587+
non_empty_list(term(), term())
587588
""",
588589
"""
589590
warning: incompatible value given to string interpolation:
@@ -667,7 +668,7 @@ defmodule Module.Types.IntegrationTest do
667668
668669
hint: the Collectable protocol is implemented for the following types:
669670
670-
dynamic(%File.Stream{} or %HashDict{} or %HashSet{} or %IO.Stream{} or %MapSet{}) or binary() or
671+
dynamic(%File.Stream{} or %HashDict{} or %HashSet{} or %IO.Stream{} or %MapSet{}) or bitstring() or
671672
empty_list() or non_empty_list(term(), term()) or non_struct_map()
672673
""",
673674
"""
@@ -684,7 +685,7 @@ defmodule Module.Types.IntegrationTest do
684685
685686
hint: the Collectable protocol is implemented for the following types:
686687
687-
dynamic(%File.Stream{} or %HashDict{} or %HashSet{} or %IO.Stream{} or %MapSet{}) or binary() or
688+
dynamic(%File.Stream{} or %HashDict{} or %HashSet{} or %IO.Stream{} or %MapSet{}) or bitstring() or
688689
empty_list() or non_empty_list(term(), term()) or non_struct_map()
689690
"""
690691
]
@@ -1567,7 +1568,7 @@ defmodule Module.Types.IntegrationTest do
15671568

15681569
defp read_chunk(binary) do
15691570
assert {:ok, {_module, [{~c"ExCk", chunk}]}} = :beam_lib.chunks(binary, [~c"ExCk"])
1570-
assert {:elixir_checker_v4, map} = :erlang.binary_to_term(chunk)
1571+
assert {:elixir_checker_v5, map} = :erlang.binary_to_term(chunk)
15711572
map
15721573
end
15731574

lib/elixir/test/elixir/module/types/pattern_test.exs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -552,10 +552,10 @@ defmodule Module.Types.PatternTest do
552552

553553
test "is_binary/1" do
554554
assert typecheck!([x], is_binary(x), x) == dynamic(binary())
555-
assert typecheck!([x], not is_binary(x), x) == dynamic(term())
555+
assert typecheck!([x], not is_binary(x), x) == dynamic(negation(binary()))
556556

557-
assert typecheck!([x], is_bitstring(x), x) == dynamic(binary())
558-
assert typecheck!([x], not is_bitstring(x), x) == dynamic(negation(binary()))
557+
assert typecheck!([x], is_bitstring(x), x) == dynamic(bitstring())
558+
assert typecheck!([x], not is_bitstring(x), x) == dynamic(negation(bitstring()))
559559
end
560560

561561
test "is_function/2" do

lib/elixir/test/elixir/protocol/consolidation_test.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ defmodule Protocol.ConsolidationTest do
165165

166166
defp exports(binary) do
167167
{:ok, {_, [{~c"ExCk", check_bin}]}} = :beam_lib.chunks(binary, [~c"ExCk"])
168-
assert {:elixir_checker_v4, contents} = :erlang.binary_to_term(check_bin)
168+
assert {:elixir_checker_v5, contents} = :erlang.binary_to_term(check_bin)
169169
Map.new(contents.exports)
170170
end
171171

lib/mix/lib/mix/compilers/elixir.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
defmodule Mix.Compilers.Elixir do
66
@moduledoc false
77

8-
@manifest_vsn 30
8+
@manifest_vsn 31
99
@checkpoint_vsn 4
1010

1111
import Record

0 commit comments

Comments
 (0)