Skip to content

Commit 07e6e1a

Browse files
authored
Warn when defining @type record(), fixes CI on OTP29 (#15150)
1 parent 663c4ab commit 07e6e1a

2 files changed

Lines changed: 25 additions & 12 deletions

File tree

lib/elixir/lib/kernel/typespec.ex

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -255,9 +255,20 @@ defmodule Kernel.Typespec do
255255

256256
case type_to_signature(expr) do
257257
{name, arity} = type_pair ->
258-
if built_in_type?(name, arity) do
259-
message = "type #{name}/#{arity} is a built-in type and it cannot be redefined"
260-
compile_error(env, message)
258+
cond do
259+
# This is a built-in type since OTP 29 but it just generates a warning for now
260+
{name, arity} == {:record, 0} ->
261+
IO.warn("type #{name}/#{arity} is overriding a built-in type",
262+
file: file,
263+
line: line
264+
)
265+
266+
built_in_type?(name, arity) ->
267+
message = "type #{name}/#{arity} is a built-in type and it cannot be redefined"
268+
compile_error(env, message)
269+
270+
true ->
271+
:ok
261272
end
262273

263274
if Map.has_key?(type_pairs, type_pair) do

lib/elixir/test/elixir/typespec_test.exs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -681,15 +681,17 @@ defmodule TypespecTest do
681681
end
682682
end
683683

684-
test "@type can be named record" do
685-
bytecode =
686-
test_module do
687-
@type record :: binary
688-
@spec foo?(record) :: boolean
689-
def foo?(_), do: true
690-
end
691-
692-
assert [type: {:record, {:type, _, :binary, []}, []}] = types(bytecode)
684+
test "@type named record/0 warns" do
685+
assert ExUnit.CaptureIO.capture_io(:stderr, fn ->
686+
bytecode =
687+
test_module do
688+
@type record :: binary
689+
@spec foo?(record) :: boolean
690+
def foo?(_), do: true
691+
end
692+
693+
assert [type: {:record, {:type, _, :binary, []}, []}] = types(bytecode)
694+
end) =~ "type record/0 is overriding a built-in type"
693695
end
694696

695697
test "@type with an invalid map notation" do

0 commit comments

Comments
 (0)