about summary refs log tree commit diff
diff options
context:
space:
mode:
authorScott Schafer <schaferjscott@gmail.com>2025-05-30 16:03:32 -0600
committerScott Schafer <schaferjscott@gmail.com>2025-07-28 16:32:11 -0600
commitaa9767290e46c9c73fa1fd3abb52277b4d07cfae (patch)
treeac01fa74da3b08db6852d998199a2c1f89ef1688
parente3514bde96d2d13586337a48db77fa64b850d249 (diff)
downloadrust-aa9767290e46c9c73fa1fd3abb52277b4d07cfae.tar.gz
rust-aa9767290e46c9c73fa1fd3abb52277b4d07cfae.zip
feat: Right align line numbers
-rw-r--r--compiler/rustc_errors/src/emitter.rs50
-rw-r--r--compiler/rustc_parse/src/parser/tests.rs48
-rw-r--r--tests/run-make/crate-loading-crate-depends-on-itself/foo.stderr12
-rw-r--r--tests/rustdoc-ui/doctest/standalone-warning-2024.stderr2
-rw-r--r--tests/ui/compiletest-self-test/ui-testing-optout.stderr2
-rw-r--r--tests/ui/modules/issue-107649.stderr4
6 files changed, 64 insertions, 54 deletions
diff --git a/compiler/rustc_errors/src/emitter.rs b/compiler/rustc_errors/src/emitter.rs
index 95400ac2ca3..46a4a186824 100644
--- a/compiler/rustc_errors/src/emitter.rs
+++ b/compiler/rustc_errors/src/emitter.rs
@@ -713,8 +713,7 @@ impl HumanEmitter {
                 Style::LineNumber,
             );
         }
-        buffer.puts(line_offset, 0, &self.maybe_anonymized(line_index), Style::LineNumber);
-
+        self.draw_line_num(buffer, line_index, line_offset, width_offset - 3);
         self.draw_col_separator_no_space(buffer, line_offset, width_offset - 2);
         left
     }
@@ -2128,11 +2127,11 @@ impl HumanEmitter {
                 // Account for a suggestion to completely remove a line(s) with whitespace (#94192).
                 let line_end = sm.lookup_char_pos(parts[0].span.hi()).line;
                 for line in line_start..=line_end {
-                    buffer.puts(
+                    self.draw_line_num(
+                        &mut buffer,
+                        line,
                         row_num - 1 + line - line_start,
-                        0,
-                        &self.maybe_anonymized(line),
-                        Style::LineNumber,
+                        max_line_num_len,
                     );
                     buffer.puts(
                         row_num - 1 + line - line_start,
@@ -2612,12 +2611,7 @@ impl HumanEmitter {
             // For more info: https://github.com/rust-lang/rust/issues/92741
             let lines_to_remove = file_lines.lines.iter().take(file_lines.lines.len() - 1);
             for (index, line_to_remove) in lines_to_remove.enumerate() {
-                buffer.puts(
-                    *row_num - 1,
-                    0,
-                    &self.maybe_anonymized(line_num + index),
-                    Style::LineNumber,
-                );
+                self.draw_line_num(buffer, line_num + index, *row_num - 1, max_line_num_len);
                 buffer.puts(*row_num - 1, max_line_num_len + 1, "- ", Style::Removal);
                 let line = normalize_whitespace(
                     &file_lines.file.get_line(line_to_remove.line_index).unwrap(),
@@ -2634,11 +2628,11 @@ impl HumanEmitter {
             let last_line_index = file_lines.lines[file_lines.lines.len() - 1].line_index;
             let last_line = &file_lines.file.get_line(last_line_index).unwrap();
             if last_line != line_to_add {
-                buffer.puts(
+                self.draw_line_num(
+                    buffer,
+                    line_num + file_lines.lines.len() - 1,
                     *row_num - 1,
-                    0,
-                    &self.maybe_anonymized(line_num + file_lines.lines.len() - 1),
-                    Style::LineNumber,
+                    max_line_num_len,
                 );
                 buffer.puts(*row_num - 1, max_line_num_len + 1, "- ", Style::Removal);
                 buffer.puts(
@@ -2661,7 +2655,7 @@ impl HumanEmitter {
                     // 2 -     .await
                     //   |
                     // *row_num -= 1;
-                    buffer.puts(*row_num, 0, &self.maybe_anonymized(line_num), Style::LineNumber);
+                    self.draw_line_num(buffer, line_num, *row_num, max_line_num_len);
                     buffer.puts(*row_num, max_line_num_len + 1, "+ ", Style::Addition);
                     buffer.append(*row_num, &normalize_whitespace(line_to_add), Style::NoStyle);
                 } else {
@@ -2671,7 +2665,7 @@ impl HumanEmitter {
                 *row_num -= 2;
             }
         } else if is_multiline {
-            buffer.puts(*row_num, 0, &self.maybe_anonymized(line_num), Style::LineNumber);
+            self.draw_line_num(buffer, line_num, *row_num, max_line_num_len);
             match &highlight_parts {
                 [SubstitutionHighlight { start: 0, end }] if *end == line_to_add.len() => {
                     buffer.puts(*row_num, max_line_num_len + 1, "+ ", Style::Addition);
@@ -2702,11 +2696,11 @@ impl HumanEmitter {
                 Style::NoStyle,
             );
         } else if let DisplaySuggestion::Add = show_code_change {
-            buffer.puts(*row_num, 0, &self.maybe_anonymized(line_num), Style::LineNumber);
+            self.draw_line_num(buffer, line_num, *row_num, max_line_num_len);
             buffer.puts(*row_num, max_line_num_len + 1, "+ ", Style::Addition);
             buffer.append(*row_num, &normalize_whitespace(line_to_add), Style::NoStyle);
         } else {
-            buffer.puts(*row_num, 0, &self.maybe_anonymized(line_num), Style::LineNumber);
+            self.draw_line_num(buffer, line_num, *row_num, max_line_num_len);
             self.draw_col_separator(buffer, *row_num, max_line_num_len + 1);
             buffer.append(*row_num, &normalize_whitespace(line_to_add), Style::NoStyle);
         }
@@ -3016,6 +3010,22 @@ impl HumanEmitter {
             OutputTheme::Unicode => "…",
         }
     }
+
+    fn draw_line_num(
+        &self,
+        buffer: &mut StyledBuffer,
+        line_num: usize,
+        line_offset: usize,
+        max_line_num_len: usize,
+    ) {
+        let line_num = self.maybe_anonymized(line_num);
+        buffer.puts(
+            line_offset,
+            max_line_num_len.saturating_sub(str_width(&line_num)),
+            &line_num,
+            Style::LineNumber,
+        );
+    }
 }
 
 #[derive(Debug, Clone, Copy)]
diff --git a/compiler/rustc_parse/src/parser/tests.rs b/compiler/rustc_parse/src/parser/tests.rs
index 15679d23bc5..43a1d779a75 100644
--- a/compiler/rustc_parse/src/parser/tests.rs
+++ b/compiler/rustc_parse/src/parser/tests.rs
@@ -2114,15 +2114,15 @@ fn foo() {
 error: foo
   --> test.rs:3:6
    |
-3  |      X0 Y0 Z0
+ 3 |      X0 Y0 Z0
    |  _______^
-4  | |    X1 Y1 Z1
+ 4 | |    X1 Y1 Z1
    | | ____^____-
    | ||____|
    |  |    `X` is a good letter
-5  |  | 1
-6  |  | 2
-7  |  | 3
+ 5 |  | 1
+ 6 |  | 2
+ 7 |  | 3
 ...   |
 15 |  |   X2 Y2 Z2
 16 |  |   X3 Y3 Z3
@@ -2133,15 +2133,15 @@ error: foo
 error: foo
    ╭▸ test.rs:3:6

-3  │      X0 Y0 Z0
+ 3 │      X0 Y0 Z0
    │ ┏━━━━━━━┛
-4  │ ┃    X1 Y1 Z1
+ 4 │ ┃    X1 Y1 Z1
    │ ┃┌────╿────┘
    │ ┗│━━━━┥
    │  │    `X` is a good letter
-5  │  │ 1
-6  │  │ 2
-7  │  │ 3
+ 5 │  │ 1
+ 6 │  │ 2
+ 7 │  │ 3
    ‡  │
 15 │  │   X2 Y2 Z2
 16 │  │   X3 Y3 Z3
@@ -2189,15 +2189,15 @@ fn foo() {
 error: foo
   --> test.rs:3:6
    |
-3  |      X0 Y0 Z0
+ 3 |      X0 Y0 Z0
    |  _______^
-4  | |  1
-5  | |  2
-6  | |  3
-7  | |    X1 Y1 Z1
+ 4 | |  1
+ 5 | |  2
+ 6 | |  3
+ 7 | |    X1 Y1 Z1
    | | _________-
-8  | || 4
-9  | || 5
+ 8 | || 4
+ 9 | || 5
 10 | || 6
 11 | ||   X2 Y2 Z2
    | ||__________- `Z` is a good letter too
@@ -2211,15 +2211,15 @@ error: foo
 error: foo
    ╭▸ test.rs:3:6

-3  │      X0 Y0 Z0
+ 3 │      X0 Y0 Z0
    │ ┏━━━━━━━┛
-4  │ ┃  1
-5  │ ┃  2
-6  │ ┃  3
-7  │ ┃    X1 Y1 Z1
+ 4 │ ┃  1
+ 5 │ ┃  2
+ 6 │ ┃  3
+ 7 │ ┃    X1 Y1 Z1
    │ ┃┌─────────┘
-8  │ ┃│ 4
-9  │ ┃│ 5
+ 8 │ ┃│ 4
+ 9 │ ┃│ 5
 10 │ ┃│ 6
 11 │ ┃│   X2 Y2 Z2
    │ ┃└──────────┘ `Z` is a good letter too
diff --git a/tests/run-make/crate-loading-crate-depends-on-itself/foo.stderr b/tests/run-make/crate-loading-crate-depends-on-itself/foo.stderr
index 7f131153540..9433a0ba00e 100644
--- a/tests/run-make/crate-loading-crate-depends-on-itself/foo.stderr
+++ b/tests/run-make/crate-loading-crate-depends-on-itself/foo.stderr
@@ -7,19 +7,19 @@ error[E0277]: the trait bound `foo::Struct: Trait` is not satisfied
 note: there are multiple different versions of crate `foo` in the dependency graph
   --> foo-current.rs:7:1
    |
-4  | extern crate foo;
+ 4 | extern crate foo;
    | ----------------- one version of crate `foo` used here, as a direct dependency of the current crate
-5  |
-6  | pub struct Struct;
+ 5 |
+ 6 | pub struct Struct;
    | ----------------- this type implements the required trait
-7  | pub trait Trait {}
+ 7 | pub trait Trait {}
    | ^^^^^^^^^^^^^^^ this is the required trait
    |
   ::: foo-prev.rs:X:Y
    |
-4  | pub struct Struct;
+ 4 | pub struct Struct;
    | ----------------- this type doesn't implement the required trait
-5  | pub trait Trait {}
+ 5 | pub trait Trait {}
    | --------------- this is the found trait
    = note: two types coming from two different versions of the same crate are different types even if they look the same
    = help: you can use `cargo tree` to explore your dependency tree
diff --git a/tests/rustdoc-ui/doctest/standalone-warning-2024.stderr b/tests/rustdoc-ui/doctest/standalone-warning-2024.stderr
index bfc1e919404..ce65557c2c4 100644
--- a/tests/rustdoc-ui/doctest/standalone-warning-2024.stderr
+++ b/tests/rustdoc-ui/doctest/standalone-warning-2024.stderr
@@ -15,7 +15,7 @@ error: unknown attribute `standalone`
 note: the lint level is defined here
   --> $DIR/standalone-warning-2024.rs:9:9
    |
-9  | #![deny(warnings)]
+ 9 | #![deny(warnings)]
    |         ^^^^^^^^
    = note: `#[deny(rustdoc::invalid_codeblock_attributes)]` implied by `#[deny(warnings)]`
 
diff --git a/tests/ui/compiletest-self-test/ui-testing-optout.stderr b/tests/ui/compiletest-self-test/ui-testing-optout.stderr
index 652c472c0bc..f1d03eab14a 100644
--- a/tests/ui/compiletest-self-test/ui-testing-optout.stderr
+++ b/tests/ui/compiletest-self-test/ui-testing-optout.stderr
@@ -16,7 +16,7 @@ error[E0412]: cannot find type `D` in this scope
 error[E0412]: cannot find type `F` in this scope
   --> $DIR/ui-testing-optout.rs:92:10
    |
-4  | type A = B;
+ 4 | type A = B;
    | ----------- similarly named type alias `A` defined here
 ...
 92 | type E = F;
diff --git a/tests/ui/modules/issue-107649.stderr b/tests/ui/modules/issue-107649.stderr
index 802ac669a10..49d7cb4e0aa 100644
--- a/tests/ui/modules/issue-107649.stderr
+++ b/tests/ui/modules/issue-107649.stderr
@@ -9,8 +9,8 @@ error[E0277]: `Dummy` doesn't implement `Debug`
 help: consider annotating `Dummy` with `#[derive(Debug)]`
    --> $DIR/auxiliary/dummy_lib.rs:2:1
     |
-2   + #[derive(Debug)]
-3   | pub struct Dummy;
+  2 + #[derive(Debug)]
+  3 | pub struct Dummy;
     |
 
 error: aborting due to 1 previous error