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
17 changes: 14 additions & 3 deletions lib/elixir/lib/kernel/typespec.ex
Original file line number Diff line number Diff line change
Expand Up @@ -255,9 +255,20 @@ defmodule Kernel.Typespec do

case type_to_signature(expr) do
{name, arity} = type_pair ->
if built_in_type?(name, arity) do
message = "type #{name}/#{arity} is a built-in type and it cannot be redefined"
compile_error(env, message)
cond do
# This is a built-in type since OTP 29 but it just generates a warning for now
{name, arity} == {:record, 0} ->
IO.warn("type #{name}/#{arity} is overriding a built-in type",
file: file,
line: line
)

built_in_type?(name, arity) ->
message = "type #{name}/#{arity} is a built-in type and it cannot be redefined"
compile_error(env, message)

true ->
:ok
end

if Map.has_key?(type_pairs, type_pair) do
Expand Down
20 changes: 11 additions & 9 deletions lib/elixir/test/elixir/typespec_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -681,15 +681,17 @@ defmodule TypespecTest do
end
end

test "@type can be named record" do
bytecode =
test_module do
@type record :: binary
@spec foo?(record) :: boolean
def foo?(_), do: true
end

assert [type: {:record, {:type, _, :binary, []}, []}] = types(bytecode)
test "@type named record/0 warns" do
assert ExUnit.CaptureIO.capture_io(:stderr, fn ->
bytecode =
test_module do
@type record :: binary
@spec foo?(record) :: boolean
def foo?(_), do: true
end

assert [type: {:record, {:type, _, :binary, []}, []}] = types(bytecode)
end) =~ "type record/0 is overriding a built-in type"
end

test "@type with an invalid map notation" do
Expand Down