Skip to content

Commit da561ca

Browse files
fertapricjosevalim
authored andcommitted
Raise compile errors on bad record types
The record name must be an atom. These checks were previously performed by erl_lint.
1 parent f702f93 commit da561ca

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

lib/elixir/lib/kernel/typespec.ex

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -568,11 +568,11 @@ defmodule Kernel.Typespec do
568568
end
569569

570570
# Handle records
571-
defp typespec({:record, meta, [atom]}, vars, caller, state) do
571+
defp typespec({:record, meta, [atom]}, vars, caller, state) when is_atom(atom) do
572572
typespec({:record, meta, [atom, []]}, vars, caller, state)
573573
end
574574

575-
defp typespec({:record, meta, [tag, field_specs]}, vars, caller, state) do
575+
defp typespec({:record, meta, [tag, field_specs]}, vars, caller, state) when is_atom(tag) do
576576
# We cannot set a function name to avoid tracking
577577
# as a compile time dependency because for records it actually is one.
578578
case Macro.expand({tag, [], [{:{}, [], []}]}, caller) do
@@ -595,6 +595,10 @@ defmodule Kernel.Typespec do
595595
end
596596
end
597597

598+
defp typespec({:record, _meta, _args}, _vars, caller, _state) do
599+
compile_error(caller, "invalid record specification, expected the record name to be an atom")
600+
end
601+
598602
# Handle ranges
599603
defp typespec({:.., meta, [left, right]}, vars, caller, state) do
600604
{left, state} = typespec(left, vars, caller, state)

lib/elixir/test/elixir/typespec_test.exs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -571,6 +571,14 @@ defmodule TypespecTest do
571571
end
572572
end
573573

574+
test "@type with invalid record" do
575+
assert_raise CompileError, ~r"invalid record specification", fn ->
576+
test_module do
577+
@type my_type :: record(atom)
578+
end
579+
end
580+
end
581+
574582
test "@type with an invalid map notation" do
575583
assert_raise CompileError, ~r"invalid map specification", fn ->
576584
test_module do

0 commit comments

Comments
 (0)