Skip to content

Commit dec2733

Browse files
Ensure Code.string_to_quoted input only needs the List.Chars protocol (#11344)
1 parent f2ad982 commit dec2733

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

lib/elixir/lib/code.ex

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -972,8 +972,7 @@ defmodule Code do
972972
@doc since: "1.13.0"
973973
@spec string_to_quoted_with_comments(List.Chars.t(), keyword) ::
974974
{:ok, Macro.t(), list(map())} | {:error, {location :: keyword, term, term}}
975-
def string_to_quoted_with_comments(string, opts \\ [])
976-
when is_binary(string) and is_list(opts) do
975+
def string_to_quoted_with_comments(string, opts \\ []) when is_list(opts) do
977976
charlist = to_charlist(string)
978977
file = Keyword.get(opts, :file, "nofile")
979978
line = Keyword.get(opts, :line, 1)
@@ -1003,7 +1002,9 @@ defmodule Code do
10031002
@doc since: "1.13.0"
10041003
@spec string_to_quoted_with_comments!(List.Chars.t(), keyword) :: {Macro.t(), list(map())}
10051004
def string_to_quoted_with_comments!(string, opts \\ []) do
1006-
case string_to_quoted_with_comments(string, opts) do
1005+
charlist = to_charlist(string)
1006+
1007+
case string_to_quoted_with_comments(charlist, opts) do
10071008
{:ok, forms, comments} ->
10081009
{forms, comments}
10091010

@@ -1013,7 +1014,7 @@ defmodule Code do
10131014
Keyword.get(opts, :file, "nofile"),
10141015
error,
10151016
token,
1016-
{string, Keyword.get(opts, :line, 1), Keyword.get(opts, :column, 1)}
1017+
{charlist, Keyword.get(opts, :line, 1), Keyword.get(opts, :column, 1)}
10171018
)
10181019
end
10191020
end

lib/elixir/test/elixir/code_test.exs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,13 @@ defmodule CodeTest do
166166
end
167167
end
168168

169+
test "string_to_quoted only requires the List.Chars protocol implementation to work" do
170+
assert {:ok, 1.23} = Code.string_to_quoted(1.23)
171+
assert 1.23 = Code.string_to_quoted!(1.23)
172+
assert {:ok, 1.23, []} = Code.string_to_quoted_with_comments(1.23)
173+
assert {1.23, []} = Code.string_to_quoted_with_comments!(1.23)
174+
end
175+
169176
test "compile source" do
170177
assert __MODULE__.__info__(:compile)[:source] == String.to_charlist(__ENV__.file)
171178
end

0 commit comments

Comments
 (0)