Skip to content

Commit 17940d5

Browse files
josevalimggVGc
authored andcommitted
Add tests for allow_locals option
1 parent 4e01418 commit 17940d5

File tree

3 files changed

+24
-7
lines changed

3 files changed

+24
-7
lines changed

lib/elixir/lib/macro/env.ex

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -558,13 +558,13 @@ defmodule Macro.Env do
558558
559559
* `:allow_locals` - controls how local macros are resolved.
560560
Defaults to `true`.
561-
561+
562562
- When `false`, does not attempt to capture local macros defined in the
563563
current module in `env`
564564
- When `true`, uses a default resolver that looks for public macros in
565565
the current module
566-
- When a function, uses the function as a custom local resolver. The function
567-
must have the signature: `(meta, name, arity, env) -> function() | false`
566+
- When a function, it will be invoked to lazily compute a local function
567+
(or return false). It has signature `(-> function() | false)`
568568
569569
* `:check_deprecations` - when set to `false`, does not check for deprecations
570570
when expanding macros
@@ -591,7 +591,7 @@ defmodule Macro.Env do
591591
# When allow_locals is a callback, we don't need to pass module macros as extra
592592
# because the callback will handle local macro resolution
593593
extra =
594-
if is_function(allow_locals, 4) do
594+
if is_function(allow_locals, 0) do
595595
[]
596596
else
597597
case allow_locals and function_exported?(module, :__info__, 1) do

lib/elixir/src/elixir_dispatch.erl

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -175,9 +175,7 @@ expand_import(Meta, Name, Arity, E, Extra, AllowLocals, Trace) ->
175175
Local = case AllowLocals of
176176
false -> false;
177177
true -> elixir_def:local_for(Meta, Name, Arity, [defmacro, defmacrop], E);
178-
Fun when is_function(Fun, 4) ->
179-
%% If we have a custom local resolver, use it.
180-
Fun(Meta, Name, Arity, E)
178+
Fun when is_function(Fun, 0) -> Fun()
181179
end,
182180

183181
case Dispatch of

lib/elixir/test/elixir/macro/env_test.exs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,25 @@ defmodule Macro.EnvTest do
207207
assert fun.([generated: true], [quote(do: hello())]) == quote(generated: true, do: hello())
208208
end
209209

210+
defmacro allow_locals_example, do: :ok
211+
212+
test "allow_locals" do
213+
{:macro, Macro.EnvTest, fun} =
214+
expand_import(env(), meta(), :allow_locals_example, 0)
215+
216+
assert fun.([], []) == :ok
217+
218+
assert expand_import(env(), meta(), :allow_locals_example, 0, allow_locals: false) ==
219+
{:error, :not_found}
220+
221+
assert expand_import(env(), meta(), :allow_locals_example, 0,
222+
allow_locals: fn -> send(self(), false) end
223+
) ==
224+
{:error, :not_found}
225+
226+
assert_received false
227+
end
228+
210229
test "with tracing and deprecations" do
211230
message = "MacroEnvMacros.my_deprecated_macro/1 is deprecated"
212231

0 commit comments

Comments
 (0)