Rollup merge of #144609 - Muscraft:right-align, r=compiler-errors
feat: Right align line numbers As part of my work on getting `annotate-snipptes` to be used as `rustc`'s renderer, I realized that `rustc` left-aligned line numbers, while `annotate-snippets` right-aligned them. This PR switches `rustc` to right-align the line numbers, matching `annotate-snippets`. In practice, this change isn't very noticeable in day-to-day output, as it only shows up when a diagnostic span contains line numbers with different lengths (9->10, 99->100, 999->1000, etc.). `rustc` ``` error[E0412]: cannot find type `F` in this scope --> $DIR/ui-testing-optout.rs:92:10 | 4 | type A = B; | ----------- similarly named type alias `A` defined here ... 92 | type E = F; | ^ help: a type alias with a similar name exists: `A` ``` `annotate-snippets` ``` error[E0412]: cannot find type `F` in this scope --> $DIR/ui-testing-optout.rs:92:10 | 4 | type A = B; | ----------- similarly named type alias `A` defined here ... 92 | type E = F; | ^ help: a type alias with a similar name exists: `A` ``` r? ``@compiler-errors``
This commit is contained in:
@@ -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)]
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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)]`
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user