From ff2ea697103c9997e215d23e87add800a36522a1 Mon Sep 17 00:00:00 2001 From: sabiwara Date: Tue, 10 Jan 2023 18:50:34 +0900 Subject: Fix Macro.to_string/1 for large negative integers (#12326) --- lib/elixir/lib/code/formatter.ex | 4 ++++ lib/elixir/test/elixir/macro_test.exs | 10 ++++++++++ 2 files changed, 14 insertions(+) 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 -- cgit v1.2.1