Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions lib/elixir/src/elixir_expand.erl
Original file line number Diff line number Diff line change
Expand Up @@ -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}),
Expand Down
31 changes: 31 additions & 0 deletions lib/elixir/test/elixir/kernel/errors_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
[
Expand Down
Loading