Skip to content
Open
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
2 changes: 1 addition & 1 deletion lib/eex/lib/eex/engine.ex
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ defmodule EEx.Engine do
def handle_expr(state, "=", ast) do
check_state!(state)
%{binary: binary, dynamic: dynamic, vars_count: vars_count} = state
var = Macro.var(:"arg#{vars_count}", __MODULE__)
var = Macro.var(String.to_unsafe_atom("arg#{vars_count}"), __MODULE__)

ast =
quote do
Expand Down
34 changes: 17 additions & 17 deletions lib/elixir/lib/base.ex
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ defmodule Base do
end

for {base, alphabet} <- [upper: b16_alphabet, lower: to_lower_enc.(b16_alphabet)] do
name = :"encode16#{base}"
name = String.to_unsafe_atom("encode16#{base}")
encoded = to_encode_list.(alphabet)

@compile {:inline, [{name, 1}]}
Expand Down Expand Up @@ -617,10 +617,10 @@ defmodule Base do
upper = Enum.with_index(b16_alphabet)

for {base, alphabet} <- [upper: upper, lower: to_lower_dec.(upper), mixed: to_mixed_dec.(upper)] do
decode_name = :"decode16#{base}!"
validate_name = :"validate16#{base}?"
valid_char_name = :"valid_char16#{base}?"
valid_word_name = :"valid_word16#{base}?"
decode_name = String.to_unsafe_atom("decode16#{base}!")
validate_name = String.to_unsafe_atom("validate16#{base}?")
valid_char_name = String.to_unsafe_atom("valid_char16#{base}?")
valid_word_name = String.to_unsafe_atom("valid_word16#{base}?")

{min, decoded} = to_decode_list.(alphabet)

Expand Down Expand Up @@ -735,7 +735,7 @@ defmodule Base do
end

for {base, alphabet} <- [base: b64_alphabet, url: b64url_alphabet] do
name = :"encode64#{base}"
name = String.to_unsafe_atom("encode64#{base}")
encoded = to_encode_list.(alphabet)

@compile {:inline, [{name, 1}]}
Expand Down Expand Up @@ -1001,12 +1001,12 @@ defmodule Base do
end

for {base, alphabet} <- [base: b64_alphabet, url: b64url_alphabet] do
decode_name = :"decode64#{base}!"
decode_name = String.to_unsafe_atom("decode64#{base}!")

validate_name = :"validate64#{base}?"
validate_main_name = :"validate_main64#{validate_name}?"
valid_char_name = :"valid_char64#{base}?"
valid_word_name = :"valid_word64#{base}?"
validate_name = String.to_unsafe_atom("validate64#{base}?")
validate_main_name = String.to_unsafe_atom("validate_main64#{validate_name}?")
valid_char_name = String.to_unsafe_atom("valid_char64#{base}?")
valid_word_name = String.to_unsafe_atom("valid_word64#{base}?")
{min, decoded} = alphabet |> Enum.with_index() |> to_decode_list.()

# SWAR fast path: 7 bytes per stride, validated via `valid_word64<base>?`
Expand Down Expand Up @@ -1314,7 +1314,7 @@ defmodule Base do
hexupper: b32hex_alphabet,
hexlower: to_lower_enc.(b32hex_alphabet)
] do
name = :"encode32#{base}"
name = String.to_unsafe_atom("encode32#{base}")
encoded = to_encode_list.(alphabet)

@compile {:inline, [{name, 1}]}
Expand Down Expand Up @@ -1653,16 +1653,16 @@ defmodule Base do
hexlower: to_lower_dec.(hexupper),
hexmixed: to_mixed_dec.(hexupper)
] do
decode_name = :"decode32#{base}!"
validate_name = :"validate32#{base}?"
validate_main_name = :"validate_main32#{validate_name}?"
valid_char_name = :"valid_char32#{base}?"
decode_name = String.to_unsafe_atom("decode32#{base}!")
validate_name = String.to_unsafe_atom("validate32#{base}?")
validate_main_name = String.to_unsafe_atom("validate_main32#{validate_name}?")
valid_char_name = String.to_unsafe_atom("valid_char32#{base}?")
{min, decoded} = to_decode_list.(alphabet)

# SWAR fast path: 7 bytes per stride, validated via `valid_word32<base>?`
# in the body. Tail leftover (1-6 bytes after a 7-byte stride hits an
# 8-byte-multiple `main`) recurses through the single-byte clause.
valid_word_name = :"valid_word32#{base}?"
valid_word_name = String.to_unsafe_atom("valid_word32#{base}?")

defp unquote(validate_main_name)(<<w::56, rest::binary>>),
do: unquote(valid_word_name)(w) and unquote(validate_main_name)(rest)
Expand Down
6 changes: 6 additions & 0 deletions lib/elixir/lib/code.ex
Original file line number Diff line number Diff line change
Expand Up @@ -767,6 +767,12 @@ defmodule Code do
* `:migrate` (since v1.18.0) - when `true`, sets all other migration options
to `true` by default. Defaults to `false`.
* `:migrate_atom_interpolations` (since v1.21.0) - when `true`, rewrites
deprecated atom interpolations to explicit calls to `String.to_unsafe_atom/1`.
For example, `:"foo_#{bar}"` becomes `String.to_unsafe_atom("foo_#{bar}")`
and `["foo_#{bar}": 1]` becomes `[{String.to_unsafe_atom("foo_#{bar}"), 1}]`.
Defaults to the value of the `:migrate` option. This option changes the AST.
* `:migrate_bitstring_modifiers` (since v1.18.0) - when `true`,
removes unnecessary parentheses in known bitstring
[modifiers](`<<>>/1`), for example `<<foo::binary()>>`
Expand Down
18 changes: 13 additions & 5 deletions lib/elixir/lib/code/formatter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ defmodule Code.Formatter do
file = Keyword.get(opts, :file, nil)
sigils = Keyword.get(opts, :sigils, [])
migrate = Keyword.get(opts, :migrate, false)
migrate_atom_interpolations = Keyword.get(opts, :migrate_atom_interpolations, migrate)
migrate_bitstring_modifiers = Keyword.get(opts, :migrate_bitstring_modifiers, migrate)
migrate_call_parens_on_pipe = Keyword.get(opts, :migrate_call_parens_on_pipe, migrate)
migrate_charlists_as_sigils = Keyword.get(opts, :migrate_charlists_as_sigils, migrate)
Expand Down Expand Up @@ -223,6 +224,7 @@ defmodule Code.Formatter do
comments: comments,
sigils: sigils,
file: file,
migrate_atom_interpolations: migrate_atom_interpolations,
migrate_bitstring_modifiers: migrate_bitstring_modifiers,
migrate_call_parens_on_pipe: migrate_call_parens_on_pipe,
migrate_charlists_as_sigils: migrate_charlists_as_sigils,
Expand Down Expand Up @@ -334,14 +336,20 @@ defmodule Code.Formatter do
end

defp quoted_to_algebra(
{{:., _, [:erlang, :binary_to_atom]}, _, [{:<<>>, _, entries}, :utf8]} = quoted,
{{:., _, [:erlang, :binary_to_atom]}, _, [{:<<>>, _, entries} = bitstring, :utf8]} =
quoted,
context,
state
) do
if interpolated?(entries) do
interpolation_to_algebra(entries, @double_quote, state, ":\"", @double_quote)
else
remote_to_algebra(quoted, context, state)
cond do
not interpolated?(entries) ->
remote_to_algebra(quoted, context, state)

state.migrate_atom_interpolations ->
quoted_to_algebra(quote(do: String.to_unsafe_atom(unquote(bitstring))), context, state)

true ->
interpolation_to_algebra(entries, @double_quote, state, ":\"", @double_quote)
end
end

Expand Down
8 changes: 4 additions & 4 deletions lib/elixir/lib/io/ansi.ex
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ defmodule IO.ANSI do

for font_n <- [1, 2, 3, 4, 5, 6, 7, 8, 9] do
@doc "Sets alternative font #{font_n}."
defsequence.(:"font_#{font_n}", font_n + 10, "m")
defsequence.(String.to_unsafe_atom("font_#{font_n}"), font_n + 10, "m")
end

@doc "Normal color or intensity."
Expand Down Expand Up @@ -193,13 +193,13 @@ defmodule IO.ANSI do
defsequence.(color, code + 30, "m")

@doc "Sets foreground color to light #{color}."
defsequence.(:"light_#{color}", code + 90, "m")
defsequence.(String.to_unsafe_atom("light_#{color}"), code + 90, "m")

@doc "Sets background color to #{color}."
defsequence.(:"#{color}_background", code + 40, "m")
defsequence.(String.to_unsafe_atom("#{color}_background"), code + 40, "m")

@doc "Sets background color to light #{color}."
defsequence.(:"light_#{color}_background", code + 100, "m")
defsequence.(String.to_unsafe_atom("light_#{color}_background"), code + 100, "m")
end

@doc "Default text color."
Expand Down
4 changes: 2 additions & 2 deletions lib/elixir/lib/macro/env.ex
Original file line number Diff line number Diff line change
Expand Up @@ -213,13 +213,13 @@ defmodule Macro.Env do
@doc false
@deprecated "Use Macro.Env.expand_alias/4 instead"
def fetch_alias(%{__struct__: Macro.Env, aliases: aliases}, atom) when is_atom(atom),
do: Keyword.fetch(aliases, :"Elixir.#{atom}")
do: Keyword.fetch(aliases, String.to_unsafe_atom("Elixir.#{atom}"))

@doc false
@deprecated "Use Macro.Env.expand_alias/4 instead"
def fetch_macro_alias(%{__struct__: Macro.Env, macro_aliases: aliases}, atom)
when is_atom(atom),
do: Keyword.fetch(aliases, :"Elixir.#{atom}")
do: Keyword.fetch(aliases, String.to_unsafe_atom("Elixir.#{atom}"))

@doc """
Returns the modules from which the given `{name, arity}` was
Expand Down
5 changes: 3 additions & 2 deletions lib/elixir/lib/module.ex
Original file line number Diff line number Diff line change
Expand Up @@ -1192,7 +1192,7 @@ defmodule Module do
defp expand_key(key, counters) do
case counters do
%{^key => count} when is_integer(count) and count >= 1 ->
{{:"#{key}#{count}", [], Elixir}, Map.put(counters, key, count - 1)}
{{String.to_unsafe_atom("#{key}#{count}"), [], Elixir}, Map.put(counters, key, count - 1)}

_ ->
{{key, [], Elixir}, counters}
Expand Down Expand Up @@ -1225,7 +1225,8 @@ defmodule Module do
defp merge_signature({var, _, _} = older, {var, _, _}, _), do: older

# Otherwise, returns a generic guess
defp merge_signature({_, meta, _}, _newer, i), do: {:"arg#{i}", meta, Elixir}
defp merge_signature({_, meta, _}, _newer, i),
do: {String.to_unsafe_atom("arg#{i}"), meta, Elixir}

@doc """
Checks if the module defines the given function or macro.
Expand Down
3 changes: 2 additions & 1 deletion lib/elixir/test/elixir/file/stream_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ defmodule File.StreamTest do
:erpc.call(node, File, :stream!, [src, modes, lines_or_bytes])
end

distributed_node = :"secondary@#{node() |> Atom.to_string() |> :binary.split("@") |> tl()}"
distributed_node =
String.to_unsafe_atom("secondary@#{node() |> Atom.to_string() |> :binary.split("@") |> tl()}")

for {type, node} <- [local: node(), distributed: distributed_node] do
describe "#{type} node" do
Expand Down
15 changes: 9 additions & 6 deletions lib/elixir/test/elixir/module/types/descr_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ defmodule Module.Types.DescrTest do
opt_difference(
acc,
open_map([
{:k, open_map([{:"value#{index}", integer()}])},
{:"field#{index}", integer()}
{:k, open_map([{String.to_unsafe_atom("value#{index}"), integer()}])},
{String.to_unsafe_atom("field#{index}"), integer()}
])
)
end)
Expand All @@ -45,7 +45,7 @@ defmodule Module.Types.DescrTest do
opt_difference(
acc,
open_tuple([
open_tuple([atom([:"value#{index}"])]),
open_tuple([atom([String.to_unsafe_atom("value#{index}")])]),
integer()
])
)
Expand Down Expand Up @@ -3954,7 +3954,10 @@ defmodule Module.Types.DescrTest do
tuple([atom([:font_style]), atom([:italic])]),
Enum.reduce(
for elem1 <- 1..5, elem2 <- 1..5 do
tuple([atom([:"f#{elem1}"]), atom([:"s#{elem2}"])])
tuple([
atom([String.to_unsafe_atom("f#{elem1}")]),
atom([String.to_unsafe_atom("s#{elem2}")])
])
end,
&opt_union/2
)
Expand Down Expand Up @@ -4008,7 +4011,7 @@ defmodule Module.Types.DescrTest do
{:notifications, boolean()}
] ++
Enum.map(1..50, fn i ->
{:"field_#{i}", atom([:"value_#{i}"])}
{String.to_unsafe_atom("field_#{i}"), atom([String.to_unsafe_atom("value_#{i}")])}
end)
)

Expand All @@ -4021,7 +4024,7 @@ defmodule Module.Types.DescrTest do

expected =
for i <- 1..50 do
name = :"name_#{i}"
name = String.to_unsafe_atom("name_#{i}")
closed_map([__struct__: atom([name])] ++ [{name, binary()}])
end
|> Enum.reduce(&opt_union/2)
Expand Down
7 changes: 4 additions & 3 deletions lib/elixir/test/elixir/registry/duplicate_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ defmodule Registry.DuplicateTest do
partitions = config.partitions

listeners =
List.wrap(config[:base_listener]) |> Enum.map(&:"#{&1}_#{partitions}_#{inspect(keys)}")
List.wrap(config[:base_listener])
|> Enum.map(&String.to_unsafe_atom("#{&1}_#{partitions}_#{inspect(keys)}"))

name = :"#{config.test}_#{partitions}_#{inspect(keys)}"
name = String.to_unsafe_atom("#{config.test}_#{partitions}_#{inspect(keys)}")
opts = [keys: config.keys, name: name, partitions: partitions, listeners: listeners]
{:ok, _} = start_supervised({Registry, opts})
%{registry: name, listeners: listeners}
Expand Down Expand Up @@ -476,7 +477,7 @@ defmodule Registry.DuplicateTest do
end

test "rejects invalid tuple syntax", %{partitions: partitions} do
name = :"test_invalid_tuple_#{partitions}"
name = String.to_unsafe_atom("test_invalid_tuple_#{partitions}")

assert_raise ArgumentError, ~r/expected :keys to be given and be one of/, fn ->
Registry.start_link(keys: {:duplicate, :invalid}, name: name, partitions: partitions)
Expand Down
7 changes: 5 additions & 2 deletions lib/elixir/test/elixir/registry/unique_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@ defmodule Registry.UniqueTest do

setup config do
partitions = config.partitions
listeners = List.wrap(config[:base_listener]) |> Enum.map(&:"#{&1}_#{partitions}")
name = :"#{config.test}_#{partitions}"

listeners =
List.wrap(config[:base_listener]) |> Enum.map(&String.to_unsafe_atom("#{&1}_#{partitions}"))

name = String.to_unsafe_atom("#{config.test}_#{partitions}")
opts = [keys: @keys, name: name, partitions: partitions, listeners: listeners]
{:ok, _} = start_supervised({Registry, opts})
%{registry: name, listeners: listeners}
Expand Down
7 changes: 4 additions & 3 deletions lib/elixir/test/elixir/registry_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ defmodule Registry.Test do
partitions = config.partitions

listeners =
List.wrap(config[:base_listener]) |> Enum.map(&:"#{&1}_#{partitions}_#{inspect(keys)}")
List.wrap(config[:base_listener])
|> Enum.map(&String.to_unsafe_atom("#{&1}_#{partitions}_#{inspect(keys)}"))

name = :"#{config.test}_#{partitions}_#{inspect(keys)}"
name = String.to_unsafe_atom("#{config.test}_#{partitions}_#{inspect(keys)}")
opts = [keys: keys, name: name, partitions: partitions, listeners: listeners]
{:ok, _} = start_supervised({Registry, opts})
%{registry: name, listeners: listeners}
Expand Down Expand Up @@ -95,7 +96,7 @@ defmodule Registry.LockTest do
setup config do
keys = config.keys
partitions = config.partitions
name = :"#{config.test}_#{keys}_#{partitions}"
name = String.to_unsafe_atom("#{config.test}_#{keys}_#{partitions}")
opts = [keys: keys, name: name, partitions: partitions]
{:ok, _} = start_supervised({Registry, opts})
%{registry: name}
Expand Down
2 changes: 1 addition & 1 deletion lib/elixir/test/elixir/typespec_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,7 @@ defmodule TypespecTest do
assert {:type, _, :map_field_exact, [{:atom, _, :message}, {:type, _, :term, []}]} = arg2
end

@fields Enum.map(10..42, &{:"f#{&1}", :ok})
@fields Enum.map(10..42, &{String.to_unsafe_atom("f#{&1}"), :ok})

test "@type with a large struct" do
bytecode =
Expand Down
4 changes: 2 additions & 2 deletions lib/elixir/test/elixir/uri_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -464,8 +464,8 @@ defmodule URITest do
end

test "base without host" do
assert URI.merge("tag:example", "foo") |> to_string == "tag:foo"
assert URI.merge("tag:example", "#fragment") |> to_string == "tag:example#fragment"
assert URI.merge("tag:example", "foo") |> to_string() == "tag:foo"
assert URI.merge("tag:example", "#fragment") |> to_string() == "tag:example#fragment"
end

test "base without host and path" do
Expand Down
2 changes: 1 addition & 1 deletion lib/ex_unit/lib/ex_unit/assertions.ex
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ defmodule ExUnit.Assertions do

case Macro.expand_once(expr, env) do
^expr when not reserved? and not all_quoted_literals? ->
vars = for i <- 1..arity, do: Macro.var(:"arg#{i}", __MODULE__)
vars = for i <- 1..arity, do: Macro.var(String.to_unsafe_atom("arg#{i}"), __MODULE__)

quoted =
quote do
Expand Down
12 changes: 8 additions & 4 deletions lib/ex_unit/lib/ex_unit/callbacks.ex
Original file line number Diff line number Diff line change
Expand Up @@ -291,8 +291,11 @@ defmodule ExUnit.Callbacks do

name =
case Module.get_attribute(module, :ex_unit_describe) do
{_line, _message, counter} -> :"__ex_unit_setup_#{counter}_#{length(setup)}"
nil -> :"__ex_unit_setup_#{length(setup)}"
{_line, _message, counter} ->
String.to_unsafe_atom("__ex_unit_setup_#{counter}_#{length(setup)}")

nil ->
String.to_unsafe_atom("__ex_unit_setup_#{length(setup)}")
end

Module.put_attribute(module, :ex_unit_setup, [name | setup])
Expand Down Expand Up @@ -425,7 +428,7 @@ defmodule ExUnit.Callbacks do
def __setup_all__(module) do
no_describe!(module)
setup_all = Module.get_attribute(module, :ex_unit_setup_all)
name = :"__ex_unit_setup_all_#{length(setup_all)}"
name = String.to_unsafe_atom("__ex_unit_setup_all_#{length(setup_all)}")
Module.put_attribute(module, :ex_unit_setup_all, [name | setup_all])
name
end
Expand Down Expand Up @@ -828,7 +831,8 @@ defmodule ExUnit.Callbacks do
{nil, nil}

callbacks ->
{:"__ex_unit_describe_#{map_size(used_describes)}", compile_setup(callbacks, :setup)}
{String.to_unsafe_atom("__ex_unit_describe_#{map_size(used_describes)}"),
compile_setup(callbacks, :setup)}
end

used_describes = Map.put(used_describes, message, name)
Expand Down
2 changes: 1 addition & 1 deletion lib/ex_unit/test/ex_unit/failures_manifest_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ defmodule ExUnit.FailuresManifestTest do
%ExUnit.Test{
state: state,
module: SomeMod,
name: :"test #{System.unique_integer()}",
name: String.to_unsafe_atom("test #{System.unique_integer()}"),
tags: %{file: file}
}
end
Expand Down
Loading
Loading