Skip to content

Commit

Permalink
Do not precompile regexes on Erlang/OTP 28
Browse files Browse the repository at this point in the history
  • Loading branch information
josevalim committed Jan 21, 2025
1 parent 329442c commit f29e18b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
26 changes: 17 additions & 9 deletions lib/elixir/lib/kernel.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6437,29 +6437,37 @@ defmodule Kernel do
"""
defmacro sigil_r(term, modifiers)

defmacro sigil_r({:<<>>, _meta, [string]}, options) when is_binary(string) do
binary = :elixir_interpolation.unescape_string(string, &regex_unescape_map/1)
regex = Regex.compile!(binary, :binary.list_to_bin(options))
Macro.escape(regex)
defmacro sigil_r({:<<>>, _meta, [binary]}, options) when is_binary(binary) do
binary = :elixir_interpolation.unescape_string(binary, &regex_unescape_map/1)
compile_regex(binary, options)
end

defmacro sigil_r({:<<>>, meta, pieces}, options) do
binary = {:<<>>, meta, unescape_tokens(pieces, &regex_unescape_map/1)}
quote(do: Regex.compile!(unquote(binary), unquote(:binary.list_to_bin(options))))
tuple = {:<<>>, meta, unescape_tokens(pieces, &regex_unescape_map/1)}
compile_regex(tuple, options)
end

defp regex_unescape_map(:newline), do: true
defp regex_unescape_map(_), do: false

@doc false
defmacro sigil_R({:<<>>, _meta, [string]}, options) when is_binary(string) do
defmacro sigil_R({:<<>>, _meta, [binary]}, options) when is_binary(binary) do
IO.warn(
"~R/.../ is deprecated, use ~r/.../ instead",
Macro.Env.stacktrace(__CALLER__)
)

regex = Regex.compile!(string, :binary.list_to_bin(options))
Macro.escape(regex)
compile_regex(binary, options)
end

defp compile_regex(binary_or_tuple, options) do
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)))

false ->
quote(do: Regex.compile!(unquote(binary_or_tuple), unquote(:binary.list_to_bin(options))))
end
end

@doc ~S"""
Expand Down
4 changes: 1 addition & 3 deletions lib/ex_unit/lib/ex_unit/doc_test.ex
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,6 @@ defmodule ExUnit.DocTest do
suite run.
"""

@opaque_type_regex ~r/#[\w\.]+</

defmodule Error do
@moduledoc """
Exception raised when there's an error with the syntax or semantics of a doctest.
Expand Down Expand Up @@ -554,7 +552,7 @@ defmodule ExUnit.DocTest do
message = "Doctest did not compile, got: #{ex_message}"

message =
if e.__struct__ == TokenMissingError and expr =~ @opaque_type_regex do
if e.__struct__ == TokenMissingError and expr =~ ~r/#[\w\.]+</ do
message <>
"""
\nIf you are planning to assert on the result of an iex> expression \
Expand Down

0 comments on commit f29e18b

Please sign in to comment.