diff options
author | sabiwara <sabiwara@gmail.com> | 2023-01-10 18:50:34 +0900 |
---|---|---|
committer | José Valim <jose.valim@dashbit.co> | 2023-01-10 10:50:51 +0100 |
commit | ff2ea697103c9997e215d23e87add800a36522a1 (patch) | |
tree | 545eeb54eb94b929a302d955130bb0eca23bfe9e | |
parent | 2146bb449ff42f9e9d235280d2541f409ffcca28 (diff) | |
download | elixir-ff2ea697103c9997e215d23e87add800a36522a1.tar.gz |
Fix Macro.to_string/1 for large negative integers (#12326)
-rw-r--r-- | lib/elixir/lib/code/formatter.ex | 4 | ||||
-rw-r--r-- | lib/elixir/test/elixir/macro_test.exs | 10 |
2 files changed, 14 insertions, 0 deletions
diff --git a/lib/elixir/lib/code/formatter.ex b/lib/elixir/lib/code/formatter.ex index 604133123..3ff668da6 100644 --- a/lib/elixir/lib/code/formatter.ex +++ b/lib/elixir/lib/code/formatter.ex @@ -1583,6 +1583,10 @@ defmodule Code.Formatter do insert_underscores(int_part) <> "." <> decimal_part end + defp insert_underscores("-" <> digits) do + "-" <> insert_underscores(digits) + end + defp insert_underscores(digits) do cond do digits =~ "_" -> diff --git a/lib/elixir/test/elixir/macro_test.exs b/lib/elixir/test/elixir/macro_test.exs index 7820166b0..f161e5618 100644 --- a/lib/elixir/test/elixir/macro_test.exs +++ b/lib/elixir/test/elixir/macro_test.exs @@ -429,6 +429,16 @@ defmodule MacroTest do test "converts quoted to string" do assert Macro.to_string(quote do: hello(world)) == "hello(world)" end + + test "large number literals" do + # with quote + assert Macro.to_string(quote do: 576_460_752_303_423_455) == "576_460_752_303_423_455" + assert Macro.to_string(quote do: -576_460_752_303_423_455) == "-576_460_752_303_423_455" + + # without quote + assert Macro.to_string(576_460_752_303_423_455) == "576_460_752_303_423_455" + assert Macro.to_string(-576_460_752_303_423_455) == "-576_460_752_303_423_455" + end end describe "to_string/2" do |