Skip to content

Commit 201952d

Browse files
committed
Revert "remove extra"
1 parent f9c418d commit 201952d

File tree

2 files changed

+32
-73
lines changed

2 files changed

+32
-73
lines changed

lib/elixir/lib/macro/env.ex

Lines changed: 24 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -555,12 +555,17 @@ defmodule Macro.Env do
555555
556556
## Options
557557
558-
* `:allow_locals` - when set to `false`, it does not attempt to capture
559-
local macros defined in the current module in `env`.
560-
When set to `true`, it uses a default resolver that looks for public macros.
561-
When set to a function, it uses the function as a local resolver.
562-
The function must have the following signature:
563-
`fn meta, name, arity, kinds, env -> function() | false`.
558+
* `:allow_locals` - controls how local macros are resolved.
559+
Defaults to `true`.
560+
561+
- When `false`, does not attempt to capture local macros defined in the
562+
current module in `env`
563+
- When `true`, uses a default resolver that looks for public macros in
564+
the current module
565+
- When a function, uses the function as a custom local resolver. The function
566+
must have the signature: `(meta, name, arity, kinds, env) -> function() | false`
567+
where `kinds` is a list of atoms indicating the types of symbols being
568+
searched (e.g., `[:defmacro, :defmacrop]`)
564569
565570
* `:check_deprecations` - when set to `false`, does not check for deprecations
566571
when expanding macros
@@ -580,74 +585,28 @@ defmodule Macro.Env do
580585
{:error, :not_found}
581586

582587
false ->
588+
allow_locals = Keyword.get(opts, :allow_locals, true)
589+
trace = Keyword.get(opts, :trace, true)
583590
module = env.module
584591

585-
allow_locals =
586-
case Keyword.get(opts, :allow_locals, true) do
587-
false ->
588-
false
589-
590-
true ->
591-
macros =
592-
if function_exported?(module, :__info__, 1) do
593-
module.__info__(:macros)
594-
else
595-
[]
596-
end
597-
598-
fn _meta, name, arity, kinds, _e ->
599-
IO.puts(
600-
"Resolving local macro #{name}/#{arity} in #{module} with kinds: #{inspect(kinds)}"
601-
)
602-
603-
IO.puts("Macros defined in #{module}: #{inspect(macros)}")
604-
605-
# by default use a resolver looking for public macros
606-
cond do
607-
:lists.any(
608-
fn
609-
:defmacro -> true
610-
:defmacrop -> true
611-
_ -> false
612-
end,
613-
kinds
614-
) and macro_exported?(module, name, arity) ->
615-
# public macro found - return the expander
616-
proper_name = :"MACRO-#{name}"
617-
proper_arity = arity + 1
618-
Function.capture(module, proper_name, proper_arity)
619-
620-
:lists.any(
621-
fn
622-
:def -> true
623-
:defp -> true
624-
_ -> false
625-
end,
626-
kinds
627-
) and function_exported?(module, name, arity) ->
628-
Function.capture(module, name, arity)
629-
630-
true ->
631-
IO.puts(
632-
"No local macro found for #{name}/#{arity} in #{module} with kinds: #{inspect(kinds)}"
633-
)
634-
635-
false
636-
end
637-
end
638-
639-
fun when is_function(fun, 5) ->
640-
# If we have a custom local resolver, use it.
641-
fun
592+
# When allow_locals is a callback, we don't need to pass module macros as extra
593+
# because the callback will handle local macro resolution
594+
extra =
595+
if is_function(allow_locals, 5) do
596+
[]
597+
else
598+
case allow_locals and function_exported?(module, :__info__, 1) do
599+
true -> [{module, module.__info__(:macros)}]
600+
false -> []
601+
end
642602
end
643603

644-
trace = Keyword.get(opts, :trace, true)
645-
646604
case :elixir_dispatch.expand_import(
647605
meta,
648606
name,
649607
arity,
650608
env,
609+
extra,
651610
allow_locals,
652611
trace
653612
) do

lib/elixir/src/elixir_dispatch.erl

Lines changed: 8 additions & 8 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/6, 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").
@@ -29,7 +29,7 @@ default_requires() ->
2929
find_import(Meta, Name, Arity, E) ->
3030
Tuple = {Name, Arity},
3131

32-
case find_import_by_name_arity(Meta, Tuple, E) of
32+
case find_import_by_name_arity(Meta, Tuple, [], E) of
3333
{function, Receiver} ->
3434
elixir_env:trace({imported_function, Meta, Receiver, Name, Arity}, E),
3535
Receiver;
@@ -56,7 +56,7 @@ find_imports(Meta, Name, E) ->
5656

5757
import_function(Meta, Name, Arity, E) ->
5858
Tuple = {Name, Arity},
59-
case find_import_by_name_arity(Meta, Tuple, E) of
59+
case find_import_by_name_arity(Meta, Tuple, [], E) of
6060
{function, Receiver} ->
6161
elixir_env:trace({imported_function, Meta, Receiver, Name, Arity}, E),
6262
elixir_import:record(Tuple, Receiver, ?key(E, module), ?key(E, function)),
@@ -115,7 +115,7 @@ dispatch_import(Meta, Name, Args, S, E, Callback) ->
115115
_ -> false
116116
end,
117117

118-
case expand_import(Meta, Name, Arity, E, AllowLocals, true) of
118+
case expand_import(Meta, Name, Arity, E, [], AllowLocals, true) of
119119
{macro, Receiver, Expander} ->
120120
check_deprecated(macro, Meta, Receiver, Name, Arity, E),
121121
Caller = {?line(Meta), S, E},
@@ -159,10 +159,10 @@ dispatch_require(_Meta, Receiver, Name, _Args, _S, _E, Callback) ->
159159

160160
%% Macros expansion
161161

162-
expand_import(Meta, Name, Arity, E, AllowLocals, Trace) ->
162+
expand_import(Meta, Name, Arity, E, Extra, AllowLocals, Trace) ->
163163
Tuple = {Name, Arity},
164164
Module = ?key(E, module),
165-
Dispatch = find_import_by_name_arity(Meta, Tuple, E),
165+
Dispatch = find_import_by_name_arity(Meta, Tuple, Extra, E),
166166

167167
case Dispatch of
168168
{ambiguous, Ambiguous} ->
@@ -303,13 +303,13 @@ find_imports_by_name(Name, [{ImportName, _} | Imports], Acc, Mod, Meta, E) when
303303
find_imports_by_name(_Name, _Imports, Acc, _Mod, _Meta, _E) ->
304304
Acc.
305305

306-
find_import_by_name_arity(Meta, {_Name, Arity} = Tuple, E) ->
306+
find_import_by_name_arity(Meta, {_Name, Arity} = Tuple, Extra, E) ->
307307
case is_import(Meta, Arity) of
308308
{import, _} = Import ->
309309
Import;
310310
false ->
311311
Funs = ?key(E, functions),
312-
Macs = ?key(E, macros),
312+
Macs = Extra ++ ?key(E, macros),
313313
FunMatch = find_import_by_name_arity(Tuple, Funs),
314314
MacMatch = find_import_by_name_arity(Tuple, Macs),
315315

0 commit comments

Comments
 (0)