Skip to content

Commit b954d53

Browse files
author
José Valim
committed
Merge pull request #907 from devinus/fix-record-guard-for-tuples
Fix is_record tests to check tuple_size before accessing the first eleme...
2 parents 2f84895 + fee5c5d commit b954d53

File tree

3 files changed

+24
-9
lines changed

3 files changed

+24
-9
lines changed

lib/elixir/lib/kernel.ex

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1581,12 +1581,14 @@ defmodule Kernel do
15811581
case __CALLER__.in_guard? do
15821582
true ->
15831583
quote do
1584-
is_tuple(unquote(thing)) and :erlang.element(1, unquote(thing)) == unquote(kind)
1584+
is_tuple(unquote(thing)) and tuple_size(unquote(thing)) > 0
1585+
and :erlang.element(1, unquote(thing)) == unquote(kind)
15851586
end
15861587
false ->
15871588
quote do
15881589
result = unquote(thing)
1589-
is_tuple(result) and :erlang.element(1, result) == unquote(kind)
1590+
is_tuple(result) and tuple_size(unquote(thing)) > 0
1591+
and :erlang.element(1, result) == unquote(kind)
15901592
end
15911593
end
15921594
end
@@ -1598,12 +1600,14 @@ defmodule Kernel do
15981600
case __CALLER__.in_guard? do
15991601
true ->
16001602
quote do
1601-
is_tuple(unquote(thing)) and is_atom(:erlang.element(1, unquote(thing)))
1603+
is_tuple(unquote(thing)) and tuple_size(unquote(thing)) > 0
1604+
and is_atom(:erlang.element(1, unquote(thing)))
16021605
end
16031606
false ->
16041607
quote do
16051608
result = unquote(thing)
1606-
is_tuple(result) and is_atom(:erlang.element(1, result))
1609+
is_tuple(result) and tuple_size(unquote(thing)) > 0
1610+
and is_atom(:erlang.element(1, result))
16071611
end
16081612
end
16091613
end

lib/elixir/test/elixir/exception_test.exs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ defmodule Kernel.ExceptionTest do
55

66
test :is_exception do
77
assert is_exception(RuntimeError.new)
8-
refute is_exception({ :foo, :bar })
8+
refute is_exception(empty_tuple)
9+
refute is_exception(a_tuple)
10+
refute is_exception(a_list)
911
end
1012

1113
test :format_entry_with_no_file_or_line do
@@ -74,4 +76,8 @@ defmodule Kernel.ExceptionTest do
7476
test :erlang_error_message do
7577
assert ErlangError.new(original: :sample).message == "erlang error: :sample"
7678
end
77-
end
79+
80+
defp empty_tuple, do: {}
81+
defp a_tuple, do: { :foo, :bar, :baz }
82+
defp a_list, do: [ :foo, :bar, :baz ]
83+
end

lib/elixir/test/elixir/record_test.exs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,11 @@ defmodule RecordTest do
103103

104104
test :is_record do
105105
assert is_record(RecordTest.FileInfo.new, RecordTest.FileInfo)
106+
assert is_record(RecordTest.WithNoField.new)
107+
refute is_record(empty_tuple)
108+
refute is_record(a_list)
109+
refute is_record(empty_tuple, RecordTest.FileInfo)
110+
refute is_record(a_tuple, RecordTest.FileInfo)
106111
refute is_record(a_list, RecordTest.FileInfo)
107112
refute is_record(RecordTest.FileInfo.new, List)
108113
end
@@ -177,7 +182,7 @@ defmodule RecordTest do
177182
file_info
178183
end
179184

180-
defp a_list do
181-
[:a, :b, :c]
182-
end
185+
defp empty_tuple, do: {}
186+
defp a_tuple, do: { :foo, :bar, :baz }
187+
defp a_list, do: [ :foo, :bar, :baz ]
183188
end

0 commit comments

Comments
 (0)