Skip to content

Commit 391bb68

Browse files
authored
Unrolled build for #144609
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``
2 parents 5529041 + aa97672 commit 391bb68

File tree

6 files changed

+64
-54
lines changed

6 files changed

+64
-54
lines changed

compiler/rustc_errors/src/emitter.rs

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -713,8 +713,7 @@ impl HumanEmitter {
713713
Style::LineNumber,
714714
);
715715
}
716-
buffer.puts(line_offset, 0, &self.maybe_anonymized(line_index), Style::LineNumber);
717-
716+
self.draw_line_num(buffer, line_index, line_offset, width_offset - 3);
718717
self.draw_col_separator_no_space(buffer, line_offset, width_offset - 2);
719718
left
720719
}
@@ -2128,11 +2127,11 @@ impl HumanEmitter {
21282127
// Account for a suggestion to completely remove a line(s) with whitespace (#94192).
21292128
let line_end = sm.lookup_char_pos(parts[0].span.hi()).line;
21302129
for line in line_start..=line_end {
2131-
buffer.puts(
2130+
self.draw_line_num(
2131+
&mut buffer,
2132+
line,
21322133
row_num - 1 + line - line_start,
2133-
0,
2134-
&self.maybe_anonymized(line),
2135-
Style::LineNumber,
2134+
max_line_num_len,
21362135
);
21372136
buffer.puts(
21382137
row_num - 1 + line - line_start,
@@ -2612,12 +2611,7 @@ impl HumanEmitter {
26122611
// For more info: https://github.com/rust-lang/rust/issues/92741
26132612
let lines_to_remove = file_lines.lines.iter().take(file_lines.lines.len() - 1);
26142613
for (index, line_to_remove) in lines_to_remove.enumerate() {
2615-
buffer.puts(
2616-
*row_num - 1,
2617-
0,
2618-
&self.maybe_anonymized(line_num + index),
2619-
Style::LineNumber,
2620-
);
2614+
self.draw_line_num(buffer, line_num + index, *row_num - 1, max_line_num_len);
26212615
buffer.puts(*row_num - 1, max_line_num_len + 1, "- ", Style::Removal);
26222616
let line = normalize_whitespace(
26232617
&file_lines.file.get_line(line_to_remove.line_index).unwrap(),
@@ -2634,11 +2628,11 @@ impl HumanEmitter {
26342628
let last_line_index = file_lines.lines[file_lines.lines.len() - 1].line_index;
26352629
let last_line = &file_lines.file.get_line(last_line_index).unwrap();
26362630
if last_line != line_to_add {
2637-
buffer.puts(
2631+
self.draw_line_num(
2632+
buffer,
2633+
line_num + file_lines.lines.len() - 1,
26382634
*row_num - 1,
2639-
0,
2640-
&self.maybe_anonymized(line_num + file_lines.lines.len() - 1),
2641-
Style::LineNumber,
2635+
max_line_num_len,
26422636
);
26432637
buffer.puts(*row_num - 1, max_line_num_len + 1, "- ", Style::Removal);
26442638
buffer.puts(
@@ -2661,7 +2655,7 @@ impl HumanEmitter {
26612655
// 2 - .await
26622656
// |
26632657
// *row_num -= 1;
2664-
buffer.puts(*row_num, 0, &self.maybe_anonymized(line_num), Style::LineNumber);
2658+
self.draw_line_num(buffer, line_num, *row_num, max_line_num_len);
26652659
buffer.puts(*row_num, max_line_num_len + 1, "+ ", Style::Addition);
26662660
buffer.append(*row_num, &normalize_whitespace(line_to_add), Style::NoStyle);
26672661
} else {
@@ -2671,7 +2665,7 @@ impl HumanEmitter {
26712665
*row_num -= 2;
26722666
}
26732667
} else if is_multiline {
2674-
buffer.puts(*row_num, 0, &self.maybe_anonymized(line_num), Style::LineNumber);
2668+
self.draw_line_num(buffer, line_num, *row_num, max_line_num_len);
26752669
match &highlight_parts {
26762670
[SubstitutionHighlight { start: 0, end }] if *end == line_to_add.len() => {
26772671
buffer.puts(*row_num, max_line_num_len + 1, "+ ", Style::Addition);
@@ -2702,11 +2696,11 @@ impl HumanEmitter {
27022696
Style::NoStyle,
27032697
);
27042698
} else if let DisplaySuggestion::Add = show_code_change {
2705-
buffer.puts(*row_num, 0, &self.maybe_anonymized(line_num), Style::LineNumber);
2699+
self.draw_line_num(buffer, line_num, *row_num, max_line_num_len);
27062700
buffer.puts(*row_num, max_line_num_len + 1, "+ ", Style::Addition);
27072701
buffer.append(*row_num, &normalize_whitespace(line_to_add), Style::NoStyle);
27082702
} else {
2709-
buffer.puts(*row_num, 0, &self.maybe_anonymized(line_num), Style::LineNumber);
2703+
self.draw_line_num(buffer, line_num, *row_num, max_line_num_len);
27102704
self.draw_col_separator(buffer, *row_num, max_line_num_len + 1);
27112705
buffer.append(*row_num, &normalize_whitespace(line_to_add), Style::NoStyle);
27122706
}
@@ -3016,6 +3010,22 @@ impl HumanEmitter {
30163010
OutputTheme::Unicode => "…",
30173011
}
30183012
}
3013+
3014+
fn draw_line_num(
3015+
&self,
3016+
buffer: &mut StyledBuffer,
3017+
line_num: usize,
3018+
line_offset: usize,
3019+
max_line_num_len: usize,
3020+
) {
3021+
let line_num = self.maybe_anonymized(line_num);
3022+
buffer.puts(
3023+
line_offset,
3024+
max_line_num_len.saturating_sub(str_width(&line_num)),
3025+
&line_num,
3026+
Style::LineNumber,
3027+
);
3028+
}
30193029
}
30203030

30213031
#[derive(Debug, Clone, Copy)]

compiler/rustc_parse/src/parser/tests.rs

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2114,15 +2114,15 @@ fn foo() {
21142114
error: foo
21152115
--> test.rs:3:6
21162116
|
2117-
3 | X0 Y0 Z0
2117+
3 | X0 Y0 Z0
21182118
| _______^
2119-
4 | | X1 Y1 Z1
2119+
4 | | X1 Y1 Z1
21202120
| | ____^____-
21212121
| ||____|
21222122
| | `X` is a good letter
2123-
5 | | 1
2124-
6 | | 2
2125-
7 | | 3
2123+
5 | | 1
2124+
6 | | 2
2125+
7 | | 3
21262126
... |
21272127
15 | | X2 Y2 Z2
21282128
16 | | X3 Y3 Z3
@@ -2133,15 +2133,15 @@ error: foo
21332133
error: foo
21342134
╭▸ test.rs:3:6
21352135
2136-
3 │ X0 Y0 Z0
2136+
3 │ X0 Y0 Z0
21372137
│ ┏━━━━━━━┛
2138-
4 │ ┃ X1 Y1 Z1
2138+
4 │ ┃ X1 Y1 Z1
21392139
│ ┃┌────╿────┘
21402140
│ ┗│━━━━┥
21412141
│ │ `X` is a good letter
2142-
5 │ │ 1
2143-
6 │ │ 2
2144-
7 │ │ 3
2142+
5 │ │ 1
2143+
6 │ │ 2
2144+
7 │ │ 3
21452145
‡ │
21462146
15 │ │ X2 Y2 Z2
21472147
16 │ │ X3 Y3 Z3
@@ -2189,15 +2189,15 @@ fn foo() {
21892189
error: foo
21902190
--> test.rs:3:6
21912191
|
2192-
3 | X0 Y0 Z0
2192+
3 | X0 Y0 Z0
21932193
| _______^
2194-
4 | | 1
2195-
5 | | 2
2196-
6 | | 3
2197-
7 | | X1 Y1 Z1
2194+
4 | | 1
2195+
5 | | 2
2196+
6 | | 3
2197+
7 | | X1 Y1 Z1
21982198
| | _________-
2199-
8 | || 4
2200-
9 | || 5
2199+
8 | || 4
2200+
9 | || 5
22012201
10 | || 6
22022202
11 | || X2 Y2 Z2
22032203
| ||__________- `Z` is a good letter too
@@ -2211,15 +2211,15 @@ error: foo
22112211
error: foo
22122212
╭▸ test.rs:3:6
22132213
2214-
3 │ X0 Y0 Z0
2214+
3 │ X0 Y0 Z0
22152215
│ ┏━━━━━━━┛
2216-
4 │ ┃ 1
2217-
5 │ ┃ 2
2218-
6 │ ┃ 3
2219-
7 │ ┃ X1 Y1 Z1
2216+
4 │ ┃ 1
2217+
5 │ ┃ 2
2218+
6 │ ┃ 3
2219+
7 │ ┃ X1 Y1 Z1
22202220
│ ┃┌─────────┘
2221-
8 │ ┃│ 4
2222-
9 │ ┃│ 5
2221+
8 │ ┃│ 4
2222+
9 │ ┃│ 5
22232223
10 │ ┃│ 6
22242224
11 │ ┃│ X2 Y2 Z2
22252225
│ ┃└──────────┘ `Z` is a good letter too

tests/run-make/crate-loading-crate-depends-on-itself/foo.stderr

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,19 @@ error[E0277]: the trait bound `foo::Struct: Trait` is not satisfied
77
note: there are multiple different versions of crate `foo` in the dependency graph
88
--> foo-current.rs:7:1
99
|
10-
4 | extern crate foo;
10+
4 | extern crate foo;
1111
| ----------------- one version of crate `foo` used here, as a direct dependency of the current crate
12-
5 |
13-
6 | pub struct Struct;
12+
5 |
13+
6 | pub struct Struct;
1414
| ----------------- this type implements the required trait
15-
7 | pub trait Trait {}
15+
7 | pub trait Trait {}
1616
| ^^^^^^^^^^^^^^^ this is the required trait
1717
|
1818
::: foo-prev.rs:X:Y
1919
|
20-
4 | pub struct Struct;
20+
4 | pub struct Struct;
2121
| ----------------- this type doesn't implement the required trait
22-
5 | pub trait Trait {}
22+
5 | pub trait Trait {}
2323
| --------------- this is the found trait
2424
= note: two types coming from two different versions of the same crate are different types even if they look the same
2525
= help: you can use `cargo tree` to explore your dependency tree

tests/rustdoc-ui/doctest/standalone-warning-2024.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ error: unknown attribute `standalone`
1515
note: the lint level is defined here
1616
--> $DIR/standalone-warning-2024.rs:9:9
1717
|
18-
9 | #![deny(warnings)]
18+
9 | #![deny(warnings)]
1919
| ^^^^^^^^
2020
= note: `#[deny(rustdoc::invalid_codeblock_attributes)]` implied by `#[deny(warnings)]`
2121

tests/ui/compiletest-self-test/ui-testing-optout.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ error[E0412]: cannot find type `D` in this scope
1616
error[E0412]: cannot find type `F` in this scope
1717
--> $DIR/ui-testing-optout.rs:92:10
1818
|
19-
4 | type A = B;
19+
4 | type A = B;
2020
| ----------- similarly named type alias `A` defined here
2121
...
2222
92 | type E = F;

tests/ui/modules/issue-107649.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ error[E0277]: `Dummy` doesn't implement `Debug`
99
help: consider annotating `Dummy` with `#[derive(Debug)]`
1010
--> $DIR/auxiliary/dummy_lib.rs:2:1
1111
|
12-
2 + #[derive(Debug)]
13-
3 | pub struct Dummy;
12+
2 + #[derive(Debug)]
13+
3 | pub struct Dummy;
1414
|
1515

1616
error: aborting due to 1 previous error

0 commit comments

Comments
 (0)