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
252257expand_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
271268expand_quoted (Meta , Receiver , Name , Arity , Quoted , S , E ) ->
0 commit comments