Skip to content

Commit

Permalink
Fix known callbacks suggestions (#13007)
Browse files Browse the repository at this point in the history
  • Loading branch information
sabiwara authored Oct 15, 2023
1 parent 88ade1d commit 8a0961c
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 19 deletions.
2 changes: 1 addition & 1 deletion lib/elixir/lib/module/types/behaviour.ex
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ defmodule Module.Types.Behaviour do

defp known_callbacks(callbacks) do
formatted_callbacks =
for {{name, arity}, {kind, module, _}} <- callbacks do
for {{name, arity}, list} <- callbacks, {kind, module, _} <- list do
"\n * " <> Exception.format_mfa(module, name, arity) <> " (#{format_definition(kind)})"
end

Expand Down
49 changes: 31 additions & 18 deletions lib/elixir/test/elixir/kernel/impl_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -202,28 +202,41 @@ defmodule Kernel.ImplTest do
end

test "warns for @impl true with callback name not in behaviour" do
assert capture_err(fn ->
Code.eval_string("""
defmodule Kernel.ImplTest.ImplAttributes do
@behaviour Kernel.ImplTest.Behaviour
@impl true
def bar(), do: :ok
end
""")
end) =~
message =
capture_err(fn ->
Code.eval_string("""
defmodule Kernel.ImplTest.ImplAttributes do
@behaviour Kernel.ImplTest.Behaviour
@impl true
def bar(), do: :ok
end
""")
end)

assert message =~
"got \"@impl true\" for function bar/0 but no behaviour specifies such callback"

assert message =~ "The known callbacks are"
assert message =~ "* Kernel.ImplTest.Behaviour.foo/0 (function)"
end

test "warns for @impl true with macro callback name not in behaviour" do
assert capture_err(fn ->
Code.eval_string("""
defmodule Kernel.ImplTest.ImplAttributes do
@behaviour Kernel.ImplTest.MacroBehaviour
@impl true
defmacro foo(), do: :ok
end
""")
end) =~ "got \"@impl true\" for macro foo/0 but no behaviour specifies such callback"
message =
capture_err(fn ->
Code.eval_string("""
defmodule Kernel.ImplTest.ImplAttributes do
@behaviour Kernel.ImplTest.MacroBehaviour
@impl true
defmacro foo(), do: :ok
end
""")
end)

assert message =~
"got \"@impl true\" for macro foo/0 but no behaviour specifies such callback"

assert message =~ "The known callbacks are"
assert message =~ "* Kernel.ImplTest.MacroBehaviour.bar/0 (macro)"
end

test "warns for @impl true with callback kind not in behaviour" do
Expand Down

0 comments on commit 8a0961c

Please sign in to comment.