Skip to content

Commit 824952f

Browse files
committed
Auto merge of #44386 - est31:master, r=pnkfelix
Fix mispositioned error indicators Fixes #38384 Most of the Rust community uses 4 spaces for indentation, but there are also tab users of Rust (including myself!). This patch fixes a bug in error printing which mispositions error indicators when near code with tabs. The code attempted to fix the issue by replacing spaces with tabs, but it sadly wasn't enough, as sometimes you may not print spaces but _ or ^ instead. This patch employs the reverse strategy: it replaces each tab with a space, so that the number of _ and ^ and spaces in error indicators below the code snippet line up perfectly. In a study [1] preceeding this patch, we could see that this strategy is also chosen by gcc version 6.3.0. Its not perfect, as the output is not beautiful, but its the easiest to implement. If anyone wants to improve on this, be my guest! This patch is meant as improvement of the status quo, not as perfect end status. It fixes the actual issue of mispositioning error indicators. [1]: #38384 (comment)
2 parents c3eccc6 + d582810 commit 824952f

File tree

7 files changed

+82
-13
lines changed

7 files changed

+82
-13
lines changed

src/librustc_errors/styled_buffer.rs

+7-10
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,11 @@ impl StyledBuffer {
2626
}
2727
}
2828

29-
pub fn copy_tabs(&mut self, row: usize) {
30-
if row < self.text.len() {
31-
for i in row + 1..self.text.len() {
32-
for j in 0..self.text[i].len() {
33-
if self.text[row].len() > j && self.text[row][j] == '\t' &&
34-
self.text[i][j] == ' ' {
35-
self.text[i][j] = '\t';
36-
}
29+
fn replace_tabs(&mut self) {
30+
for line in self.text.iter_mut() {
31+
for c in line.iter_mut() {
32+
if *c == '\t' {
33+
*c = ' ';
3734
}
3835
}
3936
}
@@ -43,8 +40,8 @@ impl StyledBuffer {
4340
let mut output: Vec<Vec<StyledString>> = vec![];
4441
let mut styled_vec: Vec<StyledString> = vec![];
4542

46-
// before we render, do a little patch-up work to support tabs
47-
self.copy_tabs(3);
43+
// before we render, replace tabs with spaces
44+
self.replace_tabs();
4845

4946
for (row, row_style) in self.text.iter().zip(&self.styles) {
5047
let mut current_style = Style::NoStyle;

src/test/ui/codemap_tests/tab.rs

+4
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,7 @@
1313
fn main() {
1414
bar;
1515
}
16+
17+
fn foo() {
18+
"bar boo"
19+
}

src/test/ui/codemap_tests/tab.stderr

+14-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,19 @@
11
error[E0425]: cannot find value `bar` in this scope
22
--> $DIR/tab.rs:14:2
33
|
4-
14 | \tbar;
5-
| \t^^^ not found in this scope
4+
14 | bar;
5+
| ^^^ not found in this scope
66

7-
error: aborting due to previous error
7+
error[E0308]: mismatched types
8+
--> $DIR/tab.rs:18:2
9+
|
10+
17 | fn foo() {
11+
| - help: try adding a return type: `-> &'static str `
12+
18 | "bar boo"
13+
| ^^^^^^^^^^^ expected (), found reference
14+
|
15+
= note: expected type `()`
16+
found type `&'static str`
17+
18+
error: aborting due to 2 previous errors
819

src/test/ui/codemap_tests/tab_2.rs

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// ignore-tidy-tab
12+
13+
fn main() {
14+
""";
15+
}
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
error: unterminated double quote string
2+
--> $DIR/tab_2.rs:14:7
3+
|
4+
14 | """;
5+
| _______^
6+
15 | | }
7+
| |__^
8+
9+
error: aborting due to previous error
10+

src/test/ui/codemap_tests/tab_3.rs

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// ignore-tidy-tab
12+
13+
fn main() {
14+
let some_vec = vec!["hi"];
15+
some_vec.into_iter();
16+
{
17+
println!("{:?}", some_vec);
18+
}
19+
}
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
error[E0382]: use of moved value: `some_vec`
2+
--> $DIR/tab_3.rs:17:20
3+
|
4+
15 | some_vec.into_iter();
5+
| -------- value moved here
6+
16 | {
7+
17 | println!("{:?}", some_vec);
8+
| ^^^^^^^^ value used here after move
9+
|
10+
= note: move occurs because `some_vec` has type `std::vec::Vec<&str>`, which does not implement the `Copy` trait
11+
12+
error: aborting due to previous error
13+

0 commit comments

Comments
 (0)