Skip to content

Commit fee5c5d

Browse files
author
Devin Torres
committed
Fix is_record tests to check tuple_size before accessing the first element
1 parent c6ff84e commit fee5c5d

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
@@ -1558,12 +1558,14 @@ defmodule Kernel do
15581558
case __CALLER__.in_guard? do
15591559
true ->
15601560
quote do
1561-
is_tuple(unquote(thing)) and :erlang.element(1, unquote(thing)) == unquote(kind)
1561+
is_tuple(unquote(thing)) and tuple_size(unquote(thing)) > 0
1562+
and :erlang.element(1, unquote(thing)) == unquote(kind)
15621563
end
15631564
false ->
15641565
quote do
15651566
result = unquote(thing)
1566-
is_tuple(result) and :erlang.element(1, result) == unquote(kind)
1567+
is_tuple(result) and tuple_size(unquote(thing)) > 0
1568+
and :erlang.element(1, result) == unquote(kind)
15671569
end
15681570
end
15691571
end
@@ -1575,12 +1577,14 @@ defmodule Kernel do
15751577
case __CALLER__.in_guard? do
15761578
true ->
15771579
quote do
1578-
is_tuple(unquote(thing)) and is_atom(:erlang.element(1, unquote(thing)))
1580+
is_tuple(unquote(thing)) and tuple_size(unquote(thing)) > 0
1581+
and is_atom(:erlang.element(1, unquote(thing)))
15791582
end
15801583
false ->
15811584
quote do
15821585
result = unquote(thing)
1583-
is_tuple(result) and is_atom(:erlang.element(1, result))
1586+
is_tuple(result) and tuple_size(unquote(thing)) > 0
1587+
and is_atom(:erlang.element(1, result))
15841588
end
15851589
end
15861590
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)