Skip to content

feat: Right align line numbers #144609

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 30 additions & 20 deletions compiler/rustc_errors/src/emitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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(),
Expand All @@ -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(
Expand All @@ -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 {
Expand All @@ -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);
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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)]
Expand Down
48 changes: 24 additions & 24 deletions compiler/rustc_parse/src/parser/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
12 changes: 6 additions & 6 deletions tests/run-make/crate-loading-crate-depends-on-itself/foo.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/rustdoc-ui/doctest/standalone-warning-2024.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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)]`

Expand Down
2 changes: 1 addition & 1 deletion tests/ui/compiletest-self-test/ui-testing-optout.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/modules/issue-107649.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading