Skip to content

Commit 1ca451f

Browse files
author
José Valim
committed
Normalize is_guard checks
1 parent ebe0c8c commit 1ca451f

35 files changed

+165
-116
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,10 @@
2727

2828
* Deprecations
2929
* [Collectable] Deprecate `Collectable.empty/1` and `Enum.traverse/2`
30+
* [Integer] `odd?/1` and `even?/1` are deprecated in favor of `is_odd/1` and `is_even/1`
31+
* [Kernel] `nil?/1` is deprecated in favor of `is_nil/1`
3032
* [Kernel] `x.Alias` is deprecated in favor of an explicit `Module.concat/2`
33+
* [Record] `record?/1` and `record?/2` are deprecated in favor of `is_record/1` and `is_record/2`
3134
* [Stream] Returning `{item, acc} | nil` from `Stream.resource/2` is deprecated, instead return `{[item], acc} | {:halt, acc}` (similar to `Stream.transform/3`)
3235

3336
* Backwards incompatible changes

lib/elixir/lib/collectable.ex

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -82,17 +82,6 @@ defimpl Collectable, for: BitString do
8282
end
8383
end
8484

85-
defimpl Collectable, for: Function do
86-
def empty(function) do
87-
function
88-
end
89-
90-
def into(function) do
91-
IO.write :stderr, "warning: passing a function as Collectable is deprecated\n#{Exception.format_stacktrace}"
92-
{function, function}
93-
end
94-
end
95-
9685
defimpl Collectable, for: Map do
9786
def empty(_map) do
9887
%{}

lib/elixir/lib/enum.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ defmodule Enum do
341341
{_, {acc, {buffer, i}}} =
342342
Enumerable.reduce(coll, {:cont, {[], {[], 0}}}, R.chunk(n, step, limit))
343343

344-
if nil?(pad) || i == 0 do
344+
if is_nil(pad) || i == 0 do
345345
:lists.reverse(acc)
346346
else
347347
buffer = :lists.reverse(buffer) ++ take(pad, n - i)

lib/elixir/lib/exception.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -618,7 +618,7 @@ defmodule UndefinedFunctionError do
618618
def message(%{function: function, module: module, arity: arity, self: self}) do
619619
if function do
620620
formatted = Exception.format_mfa module, function, arity
621-
suffix = if self or nil?(module) or :code.is_loaded(module) do
621+
suffix = if self or is_nil(module) or :code.is_loaded(module) do
622622
""
623623
else
624624
" (module #{inspect module} is not available)"

lib/elixir/lib/integer.ex

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,35 @@ defmodule Integer do
55

66
import Bitwise
77

8+
@doc false
9+
defmacro odd?(n) do
10+
quote do: (unquote(n) &&& 1) == 1
11+
end
12+
813
@doc """
914
Determines if an integer is odd.
1015
1116
Returns `true` if `n` is an odd number, otherwise `false`.
12-
Implemented as a macro so it is allowed in guard clauses.
17+
18+
Allowed in guard clauses.
1319
"""
14-
defmacro odd?(n) do
20+
defmacro is_odd(n) do
1521
quote do: (unquote(n) &&& 1) == 1
1622
end
1723

24+
@doc false
25+
defmacro even?(n) do
26+
quote do: (unquote(n) &&& 1) == 0
27+
end
28+
1829
@doc """
1930
Determines if an integer is even.
2031
2132
Returns `true` if `n` is an even number, otherwise `false`.
22-
Implemented as a macro so it is allowed in guard clauses.
33+
34+
Allowed in guard clauses.
2335
"""
24-
defmacro even?(n) do
36+
defmacro is_even(n) do
2537
quote do: (unquote(n) &&& 1) == 0
2638
end
2739

lib/elixir/lib/kernel.ex

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1875,20 +1875,25 @@ defmodule Kernel do
18751875
quote do: List.Chars.to_char_list(unquote(arg))
18761876
end
18771877

1878+
@doc false
1879+
defmacro nil?(x) do
1880+
quote do: unquote(x) == nil
1881+
end
1882+
18781883
@doc """
18791884
Checks if the given argument is nil or not.
18801885
Allowed in guard clauses.
18811886
18821887
## Examples
18831888
1884-
iex> nil?(1)
1889+
iex> is_nil(1)
18851890
false
18861891
1887-
iex> nil?(nil)
1892+
iex> is_nil(nil)
18881893
true
18891894
18901895
"""
1891-
defmacro nil?(x) do
1896+
defmacro is_nil(x) do
18921897
quote do: unquote(x) == nil
18931898
end
18941899

lib/elixir/lib/macro/env.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,9 @@ defmodule Macro.Env do
105105
"""
106106
def stacktrace(%{__struct__: Macro.Env} = env) do
107107
cond do
108-
nil?(env.module) ->
108+
is_nil(env.module) ->
109109
[{:elixir_compiler, :__FILE__, 1, relative_location(env)}]
110-
nil?(env.function) ->
110+
is_nil(env.function) ->
111111
[{env.module, :__MODULE__, 0, relative_location(env)}]
112112
true ->
113113
{name, arity} = env.function

lib/elixir/lib/module.ex

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -476,11 +476,6 @@ defmodule Module do
476476
:elixir_aliases.safe_concat([left, right])
477477
end
478478

479-
@doc false
480-
def function(mod, fun, arity) do
481-
:erlang.make_fun(mod, fun, arity)
482-
end
483-
484479
@doc """
485480
Attaches documentation to a given function or type. It expects
486481
the module the function/type belongs to, the line (a non negative
@@ -522,7 +517,7 @@ defmodule Module do
522517
line,
523518
kind,
524519
merge_signatures(old_sign, signature, 1),
525-
if(nil?(doc), do: old_doc, else: doc)
520+
if(is_nil(doc), do: old_doc, else: doc)
526521
})
527522
:ok
528523
end
@@ -896,7 +891,7 @@ defmodule Module do
896891
end
897892

898893
@doc false
899-
def get_attribute(module, key, warn) when is_atom(key) and (is_list(warn) or nil?(warn)) do
894+
def get_attribute(module, key, warn) when is_atom(key) and (is_list(warn) or is_nil(warn)) do
900895
assert_not_compiled!(:get_attribute, module)
901896
table = data_table_for(module)
902897

lib/elixir/lib/record.ex

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ defmodule Record do
44
55
Records are simply tuples where the first element is an atom:
66
7-
iex> Record.record? {User, "john", 27}
7+
iex> Record.is_record {User, "john", 27}
88
true
99
1010
This module provides conveniences for working with records at
@@ -56,6 +56,23 @@ defmodule Record do
5656
Macro.escape Record.Extractor.extract(name, opts)
5757
end
5858

59+
@doc false
60+
defmacro record?(data, kind) do
61+
case Macro.Env.in_guard?(__CALLER__) do
62+
true ->
63+
quote do
64+
is_tuple(unquote(data)) and tuple_size(unquote(data)) > 0
65+
and :erlang.element(1, unquote(data)) == unquote(kind)
66+
end
67+
false ->
68+
quote do
69+
result = unquote(data)
70+
is_tuple(result) and tuple_size(result) > 0
71+
and :erlang.element(1, result) == unquote(kind)
72+
end
73+
end
74+
end
75+
5976
@doc """
6077
Checks if the given `data` is a record of `kind`.
6178
@@ -64,11 +81,11 @@ defmodule Record do
6481
## Examples
6582
6683
iex> record = {User, "john", 27}
67-
iex> Record.record?(record, User)
84+
iex> Record.is_record(record, User)
6885
true
6986
7087
"""
71-
defmacro record?(data, kind) do
88+
defmacro is_record(data, kind) do
7289
case Macro.Env.in_guard?(__CALLER__) do
7390
true ->
7491
quote do
@@ -84,6 +101,23 @@ defmodule Record do
84101
end
85102
end
86103

104+
@doc false
105+
defmacro record?(data) do
106+
case Macro.Env.in_guard?(__CALLER__) do
107+
true ->
108+
quote do
109+
is_tuple(unquote(data)) and tuple_size(unquote(data)) > 0
110+
and is_atom(:erlang.element(1, unquote(data)))
111+
end
112+
false ->
113+
quote do
114+
result = unquote(data)
115+
is_tuple(result) and tuple_size(result) > 0
116+
and is_atom(:erlang.element(1, result))
117+
end
118+
end
119+
end
120+
87121
@doc """
88122
Checks if the given `data` is a record.
89123
@@ -92,14 +126,14 @@ defmodule Record do
92126
## Examples
93127
94128
iex> record = {User, "john", 27}
95-
iex> Record.record?(record)
129+
iex> Record.is_record(record)
96130
true
97131
iex> tuple = {}
98-
iex> Record.record?(tuple)
132+
iex> Record.is_record(tuple)
99133
false
100134
101135
"""
102-
defmacro record?(data) do
136+
defmacro is_record(data) do
103137
case Macro.Env.in_guard?(__CALLER__) do
104138
true ->
105139
quote do
@@ -319,7 +353,7 @@ defmodule Record do
319353
# Returns a keyword list of the record
320354
@doc false
321355
def __keyword__(atom, fields, record) do
322-
if record?(record, atom) do
356+
if is_record(record, atom) do
323357
[_tag|values] = Tuple.to_list(record)
324358
join_keyword(fields, values, [])
325359
else

lib/elixir/lib/stream.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ defmodule Stream do
162162
end
163163

164164
defp do_chunk(acc(h, {buffer, count} = old, t) = acc, n, pad, f1) do
165-
if nil?(pad) || count == 0 do
165+
if is_nil(pad) || count == 0 do
166166
{:cont, acc}
167167
else
168168
buffer = :lists.reverse(buffer) ++ Enum.take(pad, n - count)

0 commit comments

Comments
 (0)