Skip to content

Commit

Permalink
Merge pull request elixir-lang#3240 from oneeman/default-args-in-docs
Browse files Browse the repository at this point in the history
No longer translate arguments to `@on_definition`
  • Loading branch information
josevalim committed Apr 5, 2015
2 parents 733cf12 + ef8dc3c commit ded5e74
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 9 deletions.
15 changes: 11 additions & 4 deletions lib/elixir/lib/module.ex
Expand Up @@ -170,11 +170,11 @@ defmodule Module do
- the module environment
- kind: `:def`, `:defp`, `:defmacro`, or `:defmacrop`
- function/macro name
- list of expanded arguments
- list of expanded guards
- expanded function body
- list of quoted arguments
- list of quoted guards
- quoted function body
Note the hook receives the expanded arguments and it is invoked before
Note the hook receives the quoted arguments and it is invoked before
the function is stored in the module. So `Module.defines?/2` will return
`false` for the first clause of every function.
Expand Down Expand Up @@ -868,6 +868,13 @@ defmodule Module do
pair = {name, arity}
doc = get_attribute(module, :doc)

# Arguments are not expanded for the docs, but we make
# an exception for module attributes.
args = Macro.prewalk args, fn
{:@, _, _} = attr -> Macro.expand_once(attr, env)
x -> x
end

case add_doc(module, line, kind, pair, args, doc) do
:ok ->
:ok
Expand Down
6 changes: 3 additions & 3 deletions lib/elixir/src/elixir_def.erl
Expand Up @@ -79,7 +79,8 @@ store_definition(Line, Kind, CheckClauses, Name, Args, Guards, Body, KeepLocatio
elixir_locals:record_definition(Tuple, Kind, Module),

Location = retrieve_location(KeepLocation, Module),
{Function, Defaults, Super} = translate_definition(Kind, Line, Module, Name, Args, Guards, Body, E),
{Function, Defaults, Super} = translate_definition(Kind, Line, Name, Args, Guards, Body, E),
run_on_definition_callbacks(Kind, Line, Module, Name, Args, Guards, expr_from_body(Line, Body), E),

DefaultsLength = length(Defaults),
elixir_locals:record_defaults(Tuple, Kind, Module, DefaultsLength),
Expand Down Expand Up @@ -154,7 +155,7 @@ compile_super(_Module, _, _E) -> ok.
%% Translate the given call and expression given
%% and then store it in memory.

translate_definition(Kind, Line, Module, Name, Args, Guards, Body, E) when is_integer(Line) ->
translate_definition(Kind, Line, Name, Args, Guards, Body, E) when is_integer(Line) ->
Arity = length(Args),

{EArgs, EGuards, EBody, _} = elixir_exp_clauses:def(fun elixir_def_defaults:expand/2,
Expand All @@ -166,7 +167,6 @@ translate_definition(Kind, Line, Module, Name, Args, Guards, Body, E) when is_in
{Unpacked, Defaults} = elixir_def_defaults:unpack(Kind, Name, EArgs, S),
{Clauses, Super} = translate_clause(Body, Line, Kind, Unpacked, EGuards, EBody, S),

run_on_definition_callbacks(Kind, Line, Module, Name, EArgs, EGuards, EBody, E),
Function = {function, Line, Name, Arity, Clauses},
{Function, Defaults, Super}.

Expand Down
12 changes: 11 additions & 1 deletion lib/elixir/test/elixir/kernel/docs_test.exs
Expand Up @@ -10,7 +10,12 @@ defmodule Kernel.DocsTest do
assert [{{:__behaviour__, 1}, _, :def, [{:atom1, [], Elixir}], false},
{{:fun, 2}, _, :def, [{:x, [], nil}, {:y, [], nil}], "This is fun!\n"},
{{:nofun, 0}, _, :def, [], nil},
{{:sneaky, 1}, _, :def, [{:bool1, [], Elixir}], false}] = docs[:docs]
{{:sneaky, 1}, _, :def, [{:bool1, [], Elixir}], false},
{{:with_defaults, 4}, _, :def,
[{:int1, [], Elixir},
{:\\, [], [{:x, [], nil}, 0]},
{:\\, [], [{:y, [], nil}, 2015]},
{:\\, [], [{:f, [], nil}, {:&, _, [{:/, _, [{:>=, _, _}, 2]}]}]}], nil}] = docs[:docs]
assert {_, "Hello, I am a module"} = docs[:moduledoc]
assert [{{:bar, 1}, _, :def, false}, {{:baz, 2}, _, :def, nil},
{{:first, 0}, _, :def, "I should be first."},
Expand Down Expand Up @@ -78,6 +83,11 @@ defmodule Kernel.DocsTest do
def nofun() do
'not fun at all'
end

@year 2015
def with_defaults(@year, x \\ 0, y \\ @year, f \\ &>=/2) do
{f, x + y}
end
end)
end
end
2 changes: 1 addition & 1 deletion lib/elixir/test/elixir/module_test.exs
Expand Up @@ -98,7 +98,7 @@ defmodule ModuleTest do
assert name == :hello
assert [{:foo, _, _}, {:bar, _, _}] = args
assert [] = guards
assert {{:., _, [:erlang, :+]}, _, [{:foo, _, nil}, {:bar, _, nil}]} = expr
assert {:+, _, [{:foo, _, nil}, {:bar, _, nil}]} = expr
end

test :overridable_inside_before_compile do
Expand Down

0 comments on commit ded5e74

Please sign in to comment.