Skip to content

Commit 67a672e

Browse files
author
José Valim
committed
Guarantee nullary funs/macros are allowed in guards, closes #1997
1 parent 54e8ea1 commit 67a672e

File tree

3 files changed

+12
-11
lines changed

3 files changed

+12
-11
lines changed

lib/elixir/lib/macro.ex

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -672,19 +672,18 @@ defmodule Macro do
672672

673673
defp do_expand_once({ atom, meta, args } = original, env)
674674
when is_atom(atom) and is_list(args) and is_list(meta) do
675-
module = env.module
676-
677-
extra = if function_exported?(module, :__info__, 1) do
678-
[{ module, module.__info__(:macros) }]
679-
else
680-
[]
681-
end
682-
683675
arity = length(args)
684676

685677
if :elixir_import.special_form(atom, arity) do
686678
{ original, false }
687679
else
680+
module = env.module
681+
extra = if function_exported?(module, :__info__, 1) do
682+
[{ module, module.__info__(:macros) }]
683+
else
684+
[]
685+
end
686+
688687
expand = :elixir_dispatch.expand_import(meta, { atom, length(args) }, args,
689688
:elixir_env.ex_to_env(env), extra)
690689

lib/elixir/src/elixir_exp.erl

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -296,9 +296,6 @@ expand({ Name, Meta, Kind } = Var, #elixir_env{vars=Vars} = E) when is_atom(Name
296296

297297
compile_error(Meta, E#elixir_env.file, "expected var ~ts~ts to expand to an existing "
298298
"variable or be a part of a match", [Name, Extra]);
299-
E#elixir_env.context == guard ->
300-
compile_error(Meta, E#elixir_env.file, "unknown variable ~ts or cannot invoke "
301-
"function ~ts/0 inside guard", [Name, Name]);
302299
true ->
303300
expand({ Name, Meta, [] }, E)
304301
end

lib/elixir/test/elixir/kernel/expansion_test.exs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,11 @@ defmodule Kernel.ExpansionTest do
162162
quote(do: :"Elixir.Hello".a())
163163
end
164164

165+
test "locals: in guards" do
166+
assert expand(quote(do: fn pid when pid == self -> pid end)) ==
167+
quote(do: fn pid when __op__(:==, pid, :erlang.self()) -> pid end)
168+
end
169+
165170
test "locals: custom imports" do
166171
assert expand(quote do: (import Kernel.ExpansionTarget; seventeen)) ==
167172
quote do: (import :"Elixir.Kernel.ExpansionTarget", []; 17)

0 commit comments

Comments
 (0)