Skip to content

Commit ab1da11

Browse files
thiamsantosjosevalim
authored andcommitted
Warn on zero arity callbacks inside protocols (#11519)
1 parent 6549b00 commit ab1da11

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

lib/elixir/lib/protocol.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -723,14 +723,14 @@ defmodule Protocol do
723723

724724
defp callback_ast_to_fa({kind, {:"::", meta, [{name, _, args}, _return]}, _pos})
725725
when kind in [:callback, :macrocallback] do
726-
[{{name, length(args)}, meta}]
726+
[{{name, length(List.wrap(args))}, meta}]
727727
end
728728

729729
defp callback_ast_to_fa(
730730
{kind, {:when, _, [{:"::", meta, [{name, _, args}, _return]}, _vars]}, _pos}
731731
)
732732
when kind in [:callback, :macrocallback] do
733-
[{{name, length(args)}, meta}]
733+
[{{name, length(List.wrap(args))}, meta}]
734734
end
735735

736736
defp callback_ast_to_fa({kind, _, _pos}) when kind in [:callback, :macrocallback] do

lib/elixir/test/elixir/kernel/warning_test.exs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1163,9 +1163,11 @@ defmodule Kernel.WarningTest do
11631163
11641164
def without_specs(term, options \\ [])
11651165
1166+
@callback foo :: {:ok, term}
11661167
@callback foo(term) :: {:ok, term}
11671168
@callback foo(term, keyword) :: {:ok, term, keyword}
11681169
1170+
@callback foo_when :: {:ok, x} when x: term
11691171
@callback foo_when(x) :: {:ok, x} when x: term
11701172
@callback foo_when(x, opts) :: {:ok, x, opts} when x: term, opts: keyword
11711173
@@ -1178,12 +1180,18 @@ defmodule Kernel.WarningTest do
11781180
""")
11791181
end)
11801182

1183+
assert message =~
1184+
"cannot define @callback foo/0 inside protocol, use def/1 to outline your protocol definition\n nofile:1"
1185+
11811186
assert message =~
11821187
"cannot define @callback foo/1 inside protocol, use def/1 to outline your protocol definition\n nofile:1"
11831188

11841189
assert message =~
11851190
"cannot define @callback foo/2 inside protocol, use def/1 to outline your protocol definition\n nofile:1"
11861191

1192+
assert message =~
1193+
"cannot define @callback foo_when/0 inside protocol, use def/1 to outline your protocol definition\n nofile:1"
1194+
11871195
assert message =~
11881196
"cannot define @callback foo_when/1 inside protocol, use def/1 to outline your protocol definition\n nofile:1"
11891197

0 commit comments

Comments
 (0)