Skip to content

Show docs for callback with multiple clauses #8553

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jan 2, 2019
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
7 changes: 5 additions & 2 deletions lib/elixir/lib/kernel/typespec.ex
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,11 @@ defmodule Kernel.Typespec do

case spec_to_signature(expr) do
{name, arity} ->
{line, doc} = get_doc_info(set, :doc, line)
store_doc(set, kind, name, arity, line, :doc, doc, %{})
# store doc only once in case callback has multiple clauses
unless :ets.member(set, {kind, name, arity}) do
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just checking: is this still necessary with the new fix?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, still needed.

{line, doc} = get_doc_info(set, :doc, line)
store_doc(set, kind, name, arity, line, :doc, doc, %{})
end

:error ->
:error
Expand Down
10 changes: 7 additions & 3 deletions lib/elixir/src/elixir_erl.erl
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,12 @@ typespecs_form(Map, TranslatedTypespecs, MacroNames) ->
Forms1 = types_form(Types, Forms0),
Forms2 = callspecs_form(spec, Specs, [], MacroNames, Forms1, Map),
Forms3 = callspecs_form(callback, AllCallbacks, OptionalCallbacks, MacroCallbackNames, Forms2, Map),
{Types, AllCallbacks, Forms3}.

AllCallbacksWithoutSpecs = lists:usort([
{Kind, Name, Arity} || {Kind, {Name, Arity}, _Line, _Spec} <- AllCallbacks
]),

{Types, AllCallbacksWithoutSpecs, Forms3}.

%% Types

Expand Down Expand Up @@ -516,8 +521,7 @@ get_callback_docs(Set, Callbacks) ->
[],
doc_value(Doc),
Meta
} || {Kind, {Name, Arity}, _, _} <- Callbacks,
{Key, Line, Doc, Meta} <- ets:lookup(Set, {Kind, Name, Arity})].
} || Callback <- Callbacks, {Key, Line, Doc, Meta} <- ets:lookup(Set, Callback)].

get_type_docs(Set, Types) ->
[{Key,
Expand Down
16 changes: 16 additions & 0 deletions lib/elixir/test/elixir/kernel/docs_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,17 @@ defmodule Kernel.DocsTest do
@callback bar() :: term
@callback baz(any, term) :: any

@doc "Callback with multiple clauses"
@callback callback_multi(integer) :: integer
@callback callback_multi(atom) :: atom

@doc "Macrocallback doc"
@macrocallback qux(any) :: any

@doc "Macrocallback with multiple clauses"
@macrocallback macrocallback_multi(integer) :: integer
@macrocallback macrocallback_multi(atom) :: atom

@doc "Function doc"
@doc since: "1.2.3", color: :red
@doc color: :blue, stable: true
Expand Down Expand Up @@ -210,6 +218,7 @@ defmodule Kernel.DocsTest do
[
callback_bar,
callback_baz,
callback_multi,
callback_foo,
function_struct_0,
function_struct_1,
Expand All @@ -219,6 +228,7 @@ defmodule Kernel.DocsTest do
function_nullary,
function_qux,
guard_is_zero,
macrocallback_multi,
macrocallback_qux,
type_bar,
type_foo
Expand All @@ -231,6 +241,9 @@ defmodule Kernel.DocsTest do
%{since: "1.2.3", deprecated: "use baz/2 instead", color: :blue, stable: true}} =
callback_foo

assert {{:callback, :callback_multi, 1}, _, [], %{"en" => "Callback with multiple clauses"},
%{}} = callback_multi

assert {{:function, :__struct__, 0}, _, ["%Kernel.DocsTest.SampleDocs{}"],
%{"en" => "My struct"}, %{}} = function_struct_0

Expand Down Expand Up @@ -259,6 +272,9 @@ defmodule Kernel.DocsTest do
assert {{:macro, :is_zero, 1}, _, ["is_zero(v)"], %{"en" => "A guard"}, %{guard: true}} =
guard_is_zero

assert {{:macrocallback, :macrocallback_multi, 1}, _, [],
%{"en" => "Macrocallback with multiple clauses"}, %{}} = macrocallback_multi

assert {{:macrocallback, :qux, 1}, _, [], %{"en" => "Macrocallback doc"}, %{}} =
macrocallback_qux

Expand Down
7 changes: 7 additions & 0 deletions lib/iex/test/iex/helpers_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -677,6 +677,13 @@ defmodule IEx.HelpersTest do
@callback test(:foo) :: integer()
@callback test(:bar) :: [integer()]
"""

assert capture_io(fn -> b(MultipleClauseCallback.test()) end) =~ """
@callback test(:foo) :: integer()
@callback test(:bar) :: [integer()]

callback
"""
end)
after
cleanup_modules([MultipleClauseCallback])
Expand Down