From eb31a96a4436c9b5695e33b9933e5a4c8dd84b31 Mon Sep 17 00:00:00 2001 From: Lukasz Samson Date: Tue, 24 Nov 2026 15:04:43 +0100 Subject: [PATCH 1/2] Reject pins of __CALLER__ and __STACKTRACE__ Assisted-by: Codex:GPT-6 --- lib/elixir/src/elixir_expand.erl | 3 ++ lib/elixir/test/elixir/kernel/errors_test.exs | 29 +++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/lib/elixir/src/elixir_expand.erl b/lib/elixir/src/elixir_expand.erl index 5466e9ebfe..3a994ee9c1 100644 --- a/lib/elixir/src/elixir_expand.erl +++ b/lib/elixir/src/elixir_expand.erl @@ -387,6 +387,9 @@ 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}, _, _} when is_atom(Kind), Name == '__CALLER__' orelse Name == '__STACKTRACE__' -> + file_error(Meta, E, ?MODULE, {invalid_arg_for_pin, Arg}); + {{Name, _, Kind} = Var, #elixir_ex{unused=Unused}, _} when is_atom(Name), is_atom(Kind) -> {{'^', Meta, [Var]}, S#elixir_ex{unused=Unused}, E}; diff --git a/lib/elixir/test/elixir/kernel/errors_test.exs b/lib/elixir/test/elixir/kernel/errors_test.exs index 062eeb0296..c774e88276 100644 --- a/lib/elixir/test/elixir/kernel/errors_test.exs +++ b/lib/elixir/test/elixir/kernel/errors_test.exs @@ -61,6 +61,35 @@ defmodule Kernel.ErrorsTest do ) end + test "cannot pin __CALLER__ in a macro body" do + assert_compile_error( + "invalid argument for unary operator ^, expected an existing variable, got: ^__CALLER__", + """ + defmodule Sample do + defmacro check(value), do: (^__CALLER__ = value) + def run, do: check(nil) + 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( [ From 734d2e821c61d75423fb204eb5dba58db270e13a Mon Sep 17 00:00:00 2001 From: Lukasz Samson Date: Tue, 24 Nov 2026 22:51:03 +0100 Subject: [PATCH 2/2] Reject all compilation environment macros as pin operands Assisted-by: Codex:GPT-6 --- lib/elixir/src/elixir_expand.erl | 11 +++++----- lib/elixir/test/elixir/kernel/errors_test.exs | 22 ++++++++++--------- 2 files changed, 17 insertions(+), 16 deletions(-) diff --git a/lib/elixir/src/elixir_expand.erl b/lib/elixir/src/elixir_expand.erl index 3a994ee9c1..5bd2296540 100644 --- a/lib/elixir/src/elixir_expand.erl +++ b/lib/elixir/src/elixir_expand.erl @@ -387,15 +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}, _, _} when is_atom(Kind), Name == '__CALLER__' orelse Name == '__STACKTRACE__' -> - file_error(Meta, E, ?MODULE, {invalid_arg_for_pin, Arg}); - - {{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 c774e88276..3f03cade02 100644 --- a/lib/elixir/test/elixir/kernel/errors_test.exs +++ b/lib/elixir/test/elixir/kernel/errors_test.exs @@ -61,16 +61,18 @@ defmodule Kernel.ErrorsTest do ) end - test "cannot pin __CALLER__ in a macro body" do - assert_compile_error( - "invalid argument for unary operator ^, expected an existing variable, got: ^__CALLER__", - """ - defmodule Sample do - defmacro check(value), do: (^__CALLER__ = value) - def run, do: check(nil) - 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