Skip to content

Commit 9d80e22

Browse files
committed
Display \t in diagnostics code as four spaces
1 parent 71da1c2 commit 9d80e22

File tree

5 files changed

+37
-17
lines changed

5 files changed

+37
-17
lines changed

src/librustc_errors/styled_buffer.rs

+14-3
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,21 @@ impl StyledBuffer {
2727
}
2828

2929
fn replace_tabs(&mut self) {
30-
for line in self.text.iter_mut() {
31-
for c in line.iter_mut() {
30+
for (line_pos, line) in self.text.iter_mut().enumerate() {
31+
let mut tab_pos = vec![];
32+
for (pos, c) in line.iter().enumerate() {
3233
if *c == '\t' {
33-
*c = ' ';
34+
tab_pos.push(pos);
35+
}
36+
}
37+
// start with the tabs at the end of the line to replace them with 4 space chars
38+
for pos in tab_pos.iter().rev() {
39+
assert_eq!(line.remove(*pos), '\t');
40+
// fix the position of the style to match up after replacing the tabs
41+
let s = self.styles[line_pos].remove(*pos);
42+
for _ in 0..4 {
43+
line.insert(*pos, ' ');
44+
self.styles[line_pos].insert(*pos, s);
3445
}
3546
}
3647
}

src/libsyntax_pos/lib.rs

+12-3
Original file line numberDiff line numberDiff line change
@@ -503,13 +503,16 @@ pub enum NonNarrowChar {
503503
ZeroWidth(BytePos),
504504
/// Represents a wide (fullwidth) character
505505
Wide(BytePos),
506+
/// Represents a tab character, represented visually with a width of 4 characters
507+
Tab(BytePos),
506508
}
507509

508510
impl NonNarrowChar {
509511
fn new(pos: BytePos, width: usize) -> Self {
510512
match width {
511513
0 => NonNarrowChar::ZeroWidth(pos),
512514
2 => NonNarrowChar::Wide(pos),
515+
4 => NonNarrowChar::Tab(pos),
513516
_ => panic!("width {} given for non-narrow character", width),
514517
}
515518
}
@@ -518,7 +521,8 @@ impl NonNarrowChar {
518521
pub fn pos(&self) -> BytePos {
519522
match *self {
520523
NonNarrowChar::ZeroWidth(p) |
521-
NonNarrowChar::Wide(p) => p,
524+
NonNarrowChar::Wide(p) |
525+
NonNarrowChar::Tab(p) => p,
522526
}
523527
}
524528

@@ -527,6 +531,7 @@ impl NonNarrowChar {
527531
match *self {
528532
NonNarrowChar::ZeroWidth(_) => 0,
529533
NonNarrowChar::Wide(_) => 2,
534+
NonNarrowChar::Tab(_) => 4,
530535
}
531536
}
532537
}
@@ -538,6 +543,7 @@ impl Add<BytePos> for NonNarrowChar {
538543
match self {
539544
NonNarrowChar::ZeroWidth(pos) => NonNarrowChar::ZeroWidth(pos + rhs),
540545
NonNarrowChar::Wide(pos) => NonNarrowChar::Wide(pos + rhs),
546+
NonNarrowChar::Tab(pos) => NonNarrowChar::Tab(pos + rhs),
541547
}
542548
}
543549
}
@@ -549,6 +555,7 @@ impl Sub<BytePos> for NonNarrowChar {
549555
match self {
550556
NonNarrowChar::ZeroWidth(pos) => NonNarrowChar::ZeroWidth(pos - rhs),
551557
NonNarrowChar::Wide(pos) => NonNarrowChar::Wide(pos - rhs),
558+
NonNarrowChar::Tab(pos) => NonNarrowChar::Tab(pos - rhs),
552559
}
553560
}
554561
}
@@ -868,8 +875,10 @@ impl FileMap {
868875

869876
pub fn record_width(&self, pos: BytePos, ch: char) {
870877
let width = match ch {
871-
'\t' | '\n' =>
872-
// Tabs will consume one column.
878+
'\t' =>
879+
// Tabs will consume 4 columns.
880+
4,
881+
'\n' =>
873882
// Make newlines take one column so that displayed spans can point them.
874883
1,
875884
ch =>

src/test/ui/codemap_tests/tab.stderr

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

77
error[E0308]: mismatched types
88
--> $DIR/tab.rs:18:2
99
|
1010
17 | fn foo() {
1111
| - help: try adding a return type: `-> &'static str `
12-
18 | "bar boo" //~ ERROR mismatched types
13-
| ^^^^^^^^^^^ expected (), found reference
12+
18 | "bar boo" //~ ERROR mismatched types
13+
| ^^^^^^^^^^^^^^^^^^^^ expected (), found reference
1414
|
1515
= note: expected type `()`
1616
found type `&'static str`

src/test/ui/codemap_tests/tab_2.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error: unterminated double quote string
22
--> $DIR/tab_2.rs:14:7
33
|
4-
14 | """; //~ ERROR unterminated double quote
5-
| _______^
4+
14 | """; //~ ERROR unterminated double quote
5+
| ___________________^
66
15 | | }
77
| |__^
88

src/test/ui/codemap_tests/tab_3.stderr

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
error[E0382]: use of moved value: `some_vec`
22
--> $DIR/tab_3.rs:17:20
33
|
4-
15 | some_vec.into_iter();
5-
| -------- value moved here
6-
16 | {
7-
17 | println!("{:?}", some_vec); //~ ERROR use of moved
8-
| ^^^^^^^^ value used here after move
4+
15 | some_vec.into_iter();
5+
| -------- value moved here
6+
16 | {
7+
17 | println!("{:?}", some_vec); //~ ERROR use of moved
8+
| ^^^^^^^^ value used here after move
99
|
1010
= note: move occurs because `some_vec` has type `std::vec::Vec<&str>`, which does not implement the `Copy` trait
1111

0 commit comments

Comments
 (0)