@@ -200,8 +200,8 @@ defmodule Kernel.Utils do
200
200
201
201
defp extract_refs_from_args ( args ) do
202
202
Macro . postwalk ( args , [ ] , fn
203
- { ref , _meta , context } = var , acc when is_atom ( ref ) and is_atom ( context ) ->
204
- { var , [ { ref , context } | acc ] }
203
+ { ref , meta , context } = var , acc when is_atom ( ref ) and is_atom ( context ) ->
204
+ { var , [ { ref , var_context ( meta , context ) } | acc ] }
205
205
206
206
node , acc ->
207
207
{ node , acc }
@@ -211,8 +211,8 @@ defmodule Kernel.Utils do
211
211
# Finds every reference to `refs` in `guard` and wraps them in an unquote.
212
212
defp unquote_every_ref ( guard , refs ) do
213
213
Macro . postwalk ( guard , fn
214
- { ref , _meta , context } = var when is_atom ( ref ) and is_atom ( context ) ->
215
- case { ref , context } in refs do
214
+ { ref , meta , context } = var when is_atom ( ref ) and is_atom ( context ) ->
215
+ case { ref , var_context ( meta , context ) } in refs do
216
216
true -> literal_unquote ( var )
217
217
false -> var
218
218
end
@@ -226,17 +226,19 @@ defmodule Kernel.Utils do
226
226
defp unquote_refs_once ( guard , refs ) do
227
227
{ _ , used_refs } =
228
228
Macro . postwalk ( guard , [ ] , fn
229
- { ref , _meta , context } = var , acc when is_atom ( ref ) and is_atom ( context ) ->
230
- case { ref , context } in refs and { ref , context } not in acc do
231
- true -> { var , [ { ref , context } | acc ] }
229
+ { ref , meta , context } = var , acc when is_atom ( ref ) and is_atom ( context ) ->
230
+ pair = { ref , var_context ( meta , context ) }
231
+
232
+ case pair in refs and pair not in acc do
233
+ true -> { var , [ pair | acc ] }
232
234
false -> { var , acc }
233
235
end
234
236
235
237
node , acc ->
236
238
{ node , acc }
237
239
end )
238
240
239
- vars = for { ref , context } <- :lists . reverse ( used_refs ) , do: { ref , [ ] , context }
241
+ vars = for { ref , context } <- :lists . reverse ( used_refs ) , do: context_to_var ( ref , context )
240
242
exprs = for var <- vars , do: literal_unquote ( var )
241
243
242
244
quote do
@@ -252,4 +254,14 @@ defmodule Kernel.Utils do
252
254
defp literal_unquote ( ast ) do
253
255
{ :unquote , [ ] , List . wrap ( ast ) }
254
256
end
257
+
258
+ defp context_to_var ( ref , ctx ) when is_atom ( ctx ) , do: { ref , [ ] , ctx }
259
+ defp context_to_var ( ref , ctx ) when is_integer ( ctx ) , do: { ref , [ counter: ctx ] , nil }
260
+
261
+ defp var_context ( meta , kind ) do
262
+ case :lists . keyfind ( :counter , 1 , meta ) do
263
+ { :counter , counter } -> counter
264
+ false -> kind
265
+ end
266
+ end
255
267
end
0 commit comments