File 0505-Fix-printing-of-line-numbers-greater-than-9999.patch of Package erlang

From e5fec25324446e8c15e78920f1b3f08112921a0d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Bj=C3=B6rn=20Gustavsson?= <bjorn@erlang.org>
Date: Thu, 29 Sep 2022 12:20:00 +0200
Subject: [PATCH] Fix printing of line numbers greater than 9999

Closes #6332
---
 lib/compiler/src/sys_messages.erl   | 22 ++++++++++++++++------
 lib/compiler/test/compile_SUITE.erl | 21 ++++++++++++++++++++-
 2 files changed, 36 insertions(+), 7 deletions(-)

diff --git a/lib/compiler/src/sys_messages.erl b/lib/compiler/src/sys_messages.erl
index 5a6928f60d..779c07aeca 100644
--- a/lib/compiler/src/sys_messages.erl
+++ b/lib/compiler/src/sys_messages.erl
@@ -125,10 +125,20 @@ line_prefix() ->
     "% ".
 
 fmt_line(L, Text) ->
-    [line_prefix(), io_lib:format("~4.ts| ", [line_to_txt(L)]), Text, "\n"].
-
-line_to_txt(0) -> "";
-line_to_txt(L) -> integer_to_list(L).
+    {LineText, LineTextLength} = line_to_txt(L),
+    [line_prefix(),
+     io_lib:format("~*.ts| ", [LineTextLength, LineText]),
+     Text, "\n"].
+
+line_to_txt(L) ->
+    LineText = integer_to_list(abs(L)),
+    Length = max(4, length(LineText)),
+    if
+        L < 0 ->
+            {"", Length};
+        true ->
+            {LineText, Length}
+    end.
 
 decorate([{Line, Text} = L | Ls], StartLine, StartCol, EndLine, EndCol) when
   Line =:= StartLine, EndLine =:= StartLine ->
@@ -147,8 +157,8 @@ decorate([], _StartLine, _StartCol, _EndLine, _EndCol) ->
 %% don't produce empty decoration lines
 decorate("", L, Ls, StartLine, StartCol, EndLine, EndCol) ->
     [L | decorate(Ls, StartLine, StartCol, EndLine, EndCol)];
-decorate(Text, L, Ls, StartLine, StartCol, EndLine, EndCol) ->
-    [L, {0, Text} | decorate(Ls, StartLine, StartCol, EndLine, EndCol)].
+decorate(Text, {Line, _} = L, Ls, StartLine, StartCol, EndLine, EndCol) ->
+    [L, {-Line, Text} | decorate(Ls, StartLine, StartCol, EndLine, EndCol)].
 
 %% End typically points to the first position after the actual region.
 %% If End = Start, we adjust it to Start+1 to mark at least one character
diff --git a/lib/compiler/test/compile_SUITE.erl b/lib/compiler/test/compile_SUITE.erl
index 8cf14ab671..abd873b8d2 100644
--- a/lib/compiler/test/compile_SUITE.erl
+++ b/lib/compiler/test/compile_SUITE.erl
@@ -1484,8 +1484,9 @@ do_warnings_2([], Next, F) ->
 
 message_printing(Config) ->
     DataDir = proplists:get_value(data_dir, Config),
-    BadEncFile = filename:join(DataDir, "bad_enc.erl"),
+    PrivDir = proplists:get_value(priv_dir, Config),
 
+    BadEncFile = filename:join(DataDir, "bad_enc.erl"),
     {error,BadEncErrors, []} = compile:file(BadEncFile, [return]),
 
     [":7:15: cannot parse file, giving up\n"
@@ -1511,6 +1512,24 @@ message_printing(Config) ->
      "%     |                      \t^\n\n"
     ] = messages(Latin1Errors),
 
+    LongFile = filename:join(PrivDir, "long.erl"),
+    Long = ["-module(long).\n",
+            "-export([foo/0]).\n",
+            "unused() -> ok.\n",
+            lists:duplicate(10000, $\n),
+            "foo() -> bar().\n"],
+    ok = file:write_file(LongFile, Long),
+    {error,LongErrors,LongWarnings} = compile:file(LongFile, [return]),
+    [":10004:10: function bar/0 undefined\n"
+     "% 10004| foo() -> bar().\n"
+     "%      |          ^\n\n"
+    ] = messages(LongErrors),
+    [":3:1: function unused/0 is unused\n"
+     "%    3| unused() -> ok.\n"
+     "%     | ^\n\n"
+    ] = messages(LongWarnings),
+    ok = file:delete(LongFile),
+
     {ok,OldCwd} = file:get_cwd(),
     try
         ok = file:set_cwd(DataDir),
-- 
2.35.3

openSUSE Build Service is sponsored by