Skip to content

Commit 997eb4c

Browse files
committed
PR review comments
1 parent 54cabc4 commit 997eb4c

File tree

2 files changed

+21
-26
lines changed

2 files changed

+21
-26
lines changed

lib/elixir/lib/macro/env.ex

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -578,9 +578,7 @@ defmodule Macro.Env do
578578
| {:error, :not_found | {:conflict, module()} | {:ambiguous, [module()]}}
579579
def expand_import(env, meta, name, arity, opts \\ [])
580580
when is_list(meta) and is_atom(name) and is_integer(arity) and is_list(opts) do
581-
local_for_callback = Keyword.get(opts, :local_for_callback, fn meta, name, arity, kinds, e ->
582-
:elixir_def.local_for(meta, name, arity, kinds, e)
583-
end)
581+
local_for_callback = Keyword.get(opts, :local_for_callback)
584582

585583
case :elixir_import.special_form(name, arity) do
586584
true ->
@@ -594,7 +592,7 @@ defmodule Macro.Env do
594592
# When local_for_callback is provided, we don't need to pass module macros as extra
595593
# because the callback will handle local macro resolution
596594
extra =
597-
if Keyword.has_key?(opts, :local_for_callback) do
595+
if local_for_callback do
598596
[]
599597
else
600598
case allow_locals and function_exported?(module, :__info__, 1) do
@@ -603,7 +601,7 @@ defmodule Macro.Env do
603601
end
604602
end
605603

606-
case :elixir_dispatch.expand_import(meta, name, arity, env, extra, allow_locals, trace, local_for_callback) do
604+
case :elixir_dispatch.expand_import(meta, name, arity, env, extra, local_for_callback || allow_locals, trace) do
607605
{:macro, receiver, expander} ->
608606
{:macro, receiver, wrap_expansion(receiver, expander, meta, name, arity, env, opts)}
609607

lib/elixir/src/elixir_dispatch.erl

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
-module(elixir_dispatch).
99
-export([dispatch_import/6, dispatch_require/7,
1010
require_function/5, import_function/4,
11-
expand_import/8, expand_require/6, check_deprecated/6,
11+
expand_import/7, expand_require/6, check_deprecated/6,
1212
default_functions/0, default_macros/0, default_requires/0,
1313
find_import/4, find_imports/3, format_error/1]).
1414
-include("elixir.hrl").
@@ -115,8 +115,7 @@ dispatch_import(Meta, Name, Args, S, E, Callback) ->
115115
_ -> false
116116
end,
117117

118-
DefaultLocalForCallback = fun(M, N, A, K, Env) -> elixir_def:local_for(M, N, A, K, Env) end,
119-
case expand_import(Meta, Name, Arity, E, [], AllowLocals, true, DefaultLocalForCallback) of
118+
case expand_import(Meta, Name, Arity, E, [], AllowLocals, true) of
120119
{macro, Receiver, Expander} ->
121120
check_deprecated(macro, Meta, Receiver, Name, Arity, E),
122121
Caller = {?line(Meta), S, E},
@@ -160,7 +159,7 @@ dispatch_require(_Meta, Receiver, Name, _Args, _S, _E, Callback) ->
160159

161160
%% Macros expansion
162161

163-
expand_import(Meta, Name, Arity, E, Extra, AllowLocals, Trace, LocalForCallback) ->
162+
expand_import(Meta, Name, Arity, E, Extra, AllowLocals, Trace) ->
164163
Tuple = {Name, Arity},
165164
Module = ?key(E, module),
166165
Dispatch = find_import_by_name_arity(Meta, Tuple, Extra, E),
@@ -173,7 +172,13 @@ expand_import(Meta, Name, Arity, E, Extra, AllowLocals, Trace, LocalForCallback)
173172
do_expand_import(Dispatch, Meta, Name, Arity, Module, E, Trace);
174173

175174
_ ->
176-
Local = AllowLocals andalso LocalForCallback(Meta, Name, Arity, [defmacro, defmacrop], E),
175+
Local = case AllowLocals of
176+
false -> false;
177+
true -> elixir_def:local_for(Meta, Name, Arity, [defmacro, defmacrop], E);
178+
Fun when is_function(Fun, 5) ->
179+
%% If we have a custom local resolver, use it.
180+
Fun(Meta, Name, Arity, [defmacro, defmacrop], E)
181+
end,
177182

178183
case Dispatch of
179184
%% There is a local and an import. This is a conflict unless
@@ -250,22 +255,14 @@ expander_macro_named(Meta, Receiver, Name, Arity, E) ->
250255
fun(Args, Caller) -> expand_macro_fun(Meta, Fun, Receiver, Name, Args, Caller, E) end.
251256

252257
expand_macro_fun(Meta, Fun, Receiver, Name, Args, Caller, E) ->
253-
%% Check if Fun is actually a function, as it might be a fake value for local macros
254-
%% when using custom local_for_callback
255-
case is_function(Fun) of
256-
true ->
257-
try
258-
apply(Fun, [Caller | Args])
259-
catch
260-
Kind:Reason:Stacktrace ->
261-
Arity = length(Args),
262-
MFA = {Receiver, elixir_utils:macro_name(Name), Arity+1},
263-
Info = [{Receiver, Name, Arity, [{file, "expanding macro"}]}, caller(?line(Meta), E)],
264-
erlang:raise(Kind, Reason, prune_stacktrace(Stacktrace, MFA, Info, {ok, Caller}))
265-
end;
266-
false ->
267-
%% Return a fake value and omit expansion when Fun is not a function
268-
ok
258+
try
259+
apply(Fun, [Caller | Args])
260+
catch
261+
Kind:Reason:Stacktrace ->
262+
Arity = length(Args),
263+
MFA = {Receiver, elixir_utils:macro_name(Name), Arity+1},
264+
Info = [{Receiver, Name, Arity, [{file, "expanding macro"}]}, caller(?line(Meta), E)],
265+
erlang:raise(Kind, Reason, prune_stacktrace(Stacktrace, MFA, Info, {ok, Caller}))
269266
end.
270267

271268
expand_quoted(Meta, Receiver, Name, Arity, Quoted, S, E) ->

0 commit comments

Comments
 (0)