diff --git a/lib/elixir/lib/code/identifier.ex b/lib/elixir/lib/code/identifier.ex index 5030d2af0d..aa2f2cf415 100644 --- a/lib/elixir/lib/code/identifier.ex +++ b/lib/elixir/lib/code/identifier.ex @@ -150,11 +150,16 @@ defmodule Code.Identifier do <> end - defp escape_char(char, acc) when char < 0x100 do + defp escape_char(char, acc) when char < 0x80 do <> = <> <> end + defp escape_char(char, acc) when char < 0x100 do + <> = <> + <> + end + defp escape_char(char, acc) when char < 0x10000 do <> = <> <> diff --git a/lib/elixir/test/elixir/macro_test.exs b/lib/elixir/test/elixir/macro_test.exs index 08d98fabe7..3f4f3f9cef 100644 --- a/lib/elixir/test/elixir/macro_test.exs +++ b/lib/elixir/test/elixir/macro_test.exs @@ -1081,6 +1081,14 @@ defmodule MacroTest do end end + test "escapes C1 control characters without changing their encoding" do + for codepoint <- 0x80..0x9F do + string = <> + source = Macro.to_string(string) + assert {^string, []} = Code.eval_string(source) + end + end + test "converts invalid AST with inspect" do assert Macro.to_string(1..3) == "1..3" end