Skip to content

Commit e62a2d7

Browse files
sabiwarajosevalim
authored andcommitted
Fix algebra edge case on :erlang.binary_to_atom/2 (#12163)
1 parent 61b2601 commit e62a2d7

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

lib/elixir/lib/code/normalizer.ex

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,14 +77,22 @@ defmodule Code.Normalizer do
7777
dot_meta = patch_meta_line(dot_meta, state.parent_meta)
7878
call_meta = patch_meta_line(call_meta, dot_meta)
7979

80+
utf8 =
81+
if args == [] or interpolated?(string) do
82+
# a non-normalized :utf8 atom signals an atom interpolation
83+
:utf8
84+
else
85+
normalize_literal(:utf8, [], state)
86+
end
87+
8088
string =
8189
if state.escape do
8290
normalize_bitstring(string, state, true)
8391
else
8492
normalize_bitstring(string, state)
8593
end
8694

87-
{{:., dot_meta, [:erlang, :binary_to_atom]}, call_meta, [string, :utf8]}
95+
{{:., dot_meta, [:erlang, :binary_to_atom]}, call_meta, [string, utf8]}
8896
end
8997

9098
# Charlists with interpolations

lib/elixir/test/elixir/code_normalizer/quoted_ast_test.exs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -656,6 +656,13 @@ defmodule Code.Normalizer.QuotedASTTest do
656656

657657
assert quoted_to_string(quote(do: :"one\n\"#{2}\"\nthree")) == ~S[:"one\n\"#{2}\"\nthree"]
658658
end
659+
660+
test ":erlang.binary_to_atom/2 edge cases" do
661+
assert quoted_to_string(quote(do: :erlang.binary_to_atom(<<>>, :utf8))) == ~S[:""]
662+
663+
assert quoted_to_string(quote(do: :erlang.binary_to_atom(<<1>>, :utf8))) ==
664+
~S":erlang.binary_to_atom(<<1>>, :utf8)"
665+
end
659666
end
660667

661668
describe "quoted_to_algebra/2 does not escape" do

0 commit comments

Comments
 (0)