Skip to content

Recompile regexes when escaped from module attributes #14381

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Mar 30, 2025
Merged
Show file tree
Hide file tree
Changes from 3 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
47 changes: 33 additions & 14 deletions lib/elixir/lib/kernel.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3723,26 +3723,33 @@ defmodule Kernel do

case function? do
true ->
value =
case Module.__get_attribute__(env.module, name, line, false) do
{_, doc} when doc_attr? -> doc
other -> other
end

try do
:elixir_quote.escape(value, :none, false)
rescue
ex in [ArgumentError] ->
raise ArgumentError,
"cannot inject attribute @#{name} into function/macro because " <>
Exception.message(ex)
case Module.__get_attribute__(env.module, name, line, false) do
{_, doc} when doc_attr? ->
do_at_escape(name, doc)

%{__struct__: Regex, source: source, opts: opts} = regex ->
# TODO: Remove this in Elixir v2.0
IO.warn(
"storing and reading regexes from module attributes is deprecated, " <>
"inline the regex inside the function definition instead",
env
)

if :erlang.system_info(:otp_release) < [?2, ?8] do
do_at_escape(name, regex)
else
quote(do: Regex.compile!(unquote(source), unquote(opts)))
end

value ->
do_at_escape(name, value)
end

false when doc_attr? ->
quote do
case Module.__get_attribute__(__MODULE__, unquote(name), unquote(line), false) do
{_, doc} -> doc
other -> other
value -> value
end
end

Expand Down Expand Up @@ -3777,6 +3784,17 @@ defmodule Kernel do
raise ArgumentError, "expected 0 or 1 argument for @#{name}, got: #{length(args)}"
end

defp do_at_escape(name, value) do
try do
:elixir_quote.escape(value, :none, false)
rescue
ex in [ArgumentError] ->
raise ArgumentError,
"cannot inject attribute @#{name} into function/macro because " <>
Exception.message(ex)
end
end

# Those are always compile-time dependencies, so we can skip the trace.
defp collect_traces(:before_compile, arg, _env), do: {arg, []}
defp collect_traces(:after_compile, arg, _env), do: {arg, []}
Expand Down Expand Up @@ -6500,6 +6518,7 @@ defmodule Kernel do
end

defp compile_regex(binary_or_tuple, options) do
# TODO: Remove this when we require Erlang/OTP 28+
case is_binary(binary_or_tuple) and :erlang.system_info(:otp_release) < [?2, ?8] do
true ->
Macro.escape(Regex.compile!(binary_or_tuple, :binary.list_to_bin(options)))
Expand Down
19 changes: 19 additions & 0 deletions lib/elixir/test/elixir/regex_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,25 @@ defmodule RegexTest do

doctest Regex

if System.otp_release() >= "28" do
test "module attribute" do
assert ExUnit.CaptureIO.capture_io(:stderr, fn ->
defmodule ModAttr do
@regex ~r/example/
def regex, do: @regex

@bare_regex :erlang.term_to_binary(@regex)
def bare_regex, do: :erlang.binary_to_term(@bare_regex)

# We don't rewrite outside of functions
assert @regex.re_pattern == :erlang.binary_to_term(@bare_regex).re_pattern
end

assert ModAttr.regex().re_pattern != ModAttr.bare_regex().re_pattern
end) =~ "storing and reading regexes from module attributes is deprecated"
end
end

test "multiline" do
refute Regex.match?(~r/^b$/, "a\nb\nc")
assert Regex.match?(~r/^b$/m, "a\nb\nc")
Expand Down
Loading