diff --git a/lib/elixir/src/elixir_expand.erl b/lib/elixir/src/elixir_expand.erl index 5466e9ebfe..5bd2296540 100644 --- a/lib/elixir/src/elixir_expand.erl +++ b/lib/elixir/src/elixir_expand.erl @@ -387,12 +387,14 @@ expand({'^', Meta, [Arg]}, #elixir_ex{prematch={Prematch, _, _}, vars={_, Write} NoMatchS = S#elixir_ex{prematch=pin, vars={Prematch, Write}}, case expand(Arg, NoMatchS, E#{context := nil}) of - {{Name, _, Kind} = Var, #elixir_ex{unused=Unused}, _} when is_atom(Name), is_atom(Kind) -> + {{Name, _, Kind} = Var, #elixir_ex{unused=Unused}, _} + when is_atom(Name), is_atom(Kind), Name /= '__CALLER__', Name /= '__STACKTRACE__', + Name /= '__MODULE__', Name /= '__DIR__', Name /= '__ENV__' -> {{'^', Meta, [Var]}, S#elixir_ex{unused=Unused}, E}; _ -> - function_error(Meta, E, ?MODULE, {invalid_arg_for_pin, Arg}), - {{'^', Meta, [Arg]}, S#elixir_ex{tainted_function=true}, E} + %% A local macro could be invoked and translate the invalid pin before compilation stops. + file_error(Meta, E, ?MODULE, {invalid_arg_for_pin, Arg}) end; expand({'^', Meta, [Arg]}, S, E) -> function_error(Meta, E, ?MODULE, {pin_outside_of_match, Arg}), diff --git a/lib/elixir/test/elixir/kernel/errors_test.exs b/lib/elixir/test/elixir/kernel/errors_test.exs index 062eeb0296..3f03cade02 100644 --- a/lib/elixir/test/elixir/kernel/errors_test.exs +++ b/lib/elixir/test/elixir/kernel/errors_test.exs @@ -61,6 +61,37 @@ defmodule Kernel.ErrorsTest do ) end + test "cannot pin compilation environment macros in a macro body" do + for name <- ~w(__CALLER__ __MODULE__ __DIR__ __ENV__) do + assert_compile_error( + "invalid argument for unary operator ^, expected an existing variable, got: ^#{name}", + """ + defmodule Sample do + defmacro check(value), do: (^#{name} = value) + def run, do: check(nil) + end + """ + ) + end + end + + test "cannot pin __STACKTRACE__ in a rescue body" do + assert_compile_error( + "invalid argument for unary operator ^, expected an existing variable, got: ^__STACKTRACE__", + """ + defmodule Sample do + def check(value) do + try do + raise "oops" + rescue + _ -> ^__STACKTRACE__ = value + end + end + end + """ + ) + end + test "__STACKTRACE__ in a catch clause guard" do assert_compile_error( [