Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 38 additions & 19 deletions lib/elixir/lib/kernel/typespec.ex
Original file line number Diff line number Diff line change
Expand Up @@ -607,24 +607,24 @@ defmodule Kernel.Typespec do
compile_error(caller, "expected key-value pairs in struct #{Macro.to_string(name)}")
end

field_types =
collect_field_types(fields, {:struct, module}, vars, caller, state, fn field ->
if not :lists.any(&(&1.field == field), struct_info) do
compile_error(
caller,
"undefined field #{inspect(field)} on struct #{inspect(module)}"
)
end
end)

types =
:lists.map(
fn %{field: field} ->
{field, Keyword.get(fields, field, quote(do: term()))}
{field, :maps.get(field, field_types, quote(do: term()))}
end,
struct_info
)

fun = fn {field, _} ->
if not Enum.any?(struct_info, &(&1.field == field)) do
compile_error(
caller,
"undefined field #{inspect(field)} on struct #{inspect(module)}"
)
end
end

:lists.foreach(fun, fields)
typespec({:%{}, meta, [__struct__: module] ++ :lists.usort(types)}, vars, caller, state)

_ ->
Expand All @@ -646,25 +646,25 @@ defmodule Kernel.Typespec do
# as a compile time dependency because for records it actually is one.
case Macro.expand({name, [], [{:{}, [], []}]}, caller) do
{_, _, [tag, fields | _]} when is_list(fields) ->
field_types =
collect_field_types(field_specs, {:record, name}, vars, caller, state, fn field ->
if not Keyword.has_key?(fields, field) do
compile_error(caller, "undefined field #{field} on record #{inspect(name)}")
end
end)

types =
:lists.map(
fn {field, _} ->
{:"::", [],
[
{field, [], nil},
Keyword.get(field_specs, field, quote(do: term()))
:maps.get(field, field_types, quote(do: term()))
]}
end,
fields
)

fun = fn {field, _} ->
if not Keyword.has_key?(fields, field) do
compile_error(caller, "undefined field #{field} on record #{inspect(name)}")
end
end

:lists.foreach(fun, field_specs)
typespec({:{}, meta, [tag | types]}, vars, caller, state)

_ ->
Expand Down Expand Up @@ -985,6 +985,25 @@ defmodule Kernel.Typespec do

## Helpers

defp collect_field_types(fields, {kind, name}, vars, caller, state, validate_field) do
:lists.foldl(
fn {field, type}, acc ->
validate_field.(field)

if :maps.is_key(field, acc) do
# Validate the discarded type without counting its references in the resulting typespec.
typespec(type, vars, caller, state)
IO.warn("duplicate field #{inspect(field)} on #{kind} #{inspect(name)}", caller)
acc
else
:maps.put(field, type, acc)
end
end,
%{},
fields
)
end

# This is a modified backport of Macro.expand/2 because we want to expand
# aliases but we don't them to become compile-time references.
defp expand_remote({:__aliases__, meta, list} = alias, env) do
Expand Down
52 changes: 52 additions & 0 deletions lib/elixir/test/elixir/typespec_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -602,6 +602,30 @@ defmodule TypespecTest do
end
end

test "@type with a struct with duplicate fields" do
assert_raise Kernel.TypespecError, ~r"type undefined_type/0 undefined", fn ->
test_module do
@s URI
@type my_type :: %@s{port: integer(), port: undefined_type()}
end
end

{bytecode, diagnostics} =
Code.with_diagnostics(fn ->
test_module do
@type my_type :: %URI{port: integer(), path: binary(), port: binary()}
end
end)

assert [%{severity: :warning, message: "duplicate field :port on struct URI"}] = diagnostics
assert [type: {:my_type, {:type, _, :map, fields}, []}] = types(bytecode)

port_types =
for {:type, _, :map_field_exact, [{:atom, _, :port}, type]} <- fields, do: type

assert [{:type, _, :integer, []}] = port_types
end

test "@type when overriding Elixir built-in" do
assert_raise Kernel.TypespecError, ~r"type struct/0 is a built-in type", fn ->
test_module do
Expand Down Expand Up @@ -684,6 +708,34 @@ defmodule TypespecTest do
end
end

test "@type with a record with duplicate fields" do
assert_raise Kernel.TypespecError, ~r"type undefined_type/0 undefined", fn ->
test_module do
require Record
Record.defrecord(:user, name: nil)
@type t :: binary()
@type my_type :: record(:user, name: t(), name: undefined_type())
end
end

{bytecode, diagnostics} =
Code.with_diagnostics(fn ->
test_module do
require Record
Record.defrecord(:user, name: nil)
@type my_type :: record(:user, name: binary(), name: integer())
end
end)

assert [%{severity: :warning, message: "duplicate field :name on record :user"}] =
diagnostics

assert [type: {:my_type, {:type, _, :tuple, [{:atom, _, :user}, name]}, []}] =
types(bytecode)

assert {:ann_type, _, [{:var, _, :name}, {:type, _, :binary, []}]} = name
end

test "@type with a record which declares the name as the type `atom` rather than an atom literal" do
assert_raise Kernel.TypespecError, ~r"expected the record name to be an atom literal", fn ->
test_module do
Expand Down
Loading