about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorest31 <MTest31@outlook.com>2017-09-07 08:47:13 +0200
committerest31 <MTest31@outlook.com>2017-09-07 10:26:27 +0200
commitd58281097a3bf14fe1b199c8ba6aee00793e111e (patch)
tree71d6084a9f3e870110a6d5da0359c46927767b52 /src
parent05e3c96d36b5281e969b5e79b92d671c7872bf83 (diff)
downloadrust-d58281097a3bf14fe1b199c8ba6aee00793e111e.tar.gz
rust-d58281097a3bf14fe1b199c8ba6aee00793e111e.zip
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]: https://github.com/rust-lang/rust/issues/38384#issuecomment-326813710
Diffstat (limited to 'src')
-rw-r--r--src/librustc_errors/styled_buffer.rs17
-rw-r--r--src/test/ui/codemap_tests/tab.rs4
-rw-r--r--src/test/ui/codemap_tests/tab.stderr17
-rw-r--r--src/test/ui/codemap_tests/tab_2.rs15
-rw-r--r--src/test/ui/codemap_tests/tab_2.stderr10
-rw-r--r--src/test/ui/codemap_tests/tab_3.rs19
-rw-r--r--src/test/ui/codemap_tests/tab_3.stderr13
7 files changed, 82 insertions, 13 deletions
diff --git a/src/librustc_errors/styled_buffer.rs b/src/librustc_errors/styled_buffer.rs
index f1f2e6c55e9..ceb94f27dc3 100644
--- a/src/librustc_errors/styled_buffer.rs
+++ b/src/librustc_errors/styled_buffer.rs
@@ -26,14 +26,11 @@ impl StyledBuffer {
         }
     }
 
-    pub fn copy_tabs(&mut self, row: usize) {
-        if row < self.text.len() {
-            for i in row + 1..self.text.len() {
-                for j in 0..self.text[i].len() {
-                    if self.text[row].len() > j && self.text[row][j] == '\t' &&
-                       self.text[i][j] == ' ' {
-                        self.text[i][j] = '\t';
-                    }
+    fn replace_tabs(&mut self) {
+        for line in self.text.iter_mut() {
+            for c in line.iter_mut() {
+                if *c == '\t' {
+                    *c = ' ';
                 }
             }
         }
@@ -43,8 +40,8 @@ impl StyledBuffer {
         let mut output: Vec<Vec<StyledString>> = vec![];
         let mut styled_vec: Vec<StyledString> = vec![];
 
-        // before we render, do a little patch-up work to support tabs
-        self.copy_tabs(3);
+        // before we render, replace tabs with spaces
+        self.replace_tabs();
 
         for (row, row_style) in self.text.iter().zip(&self.styles) {
             let mut current_style = Style::NoStyle;
diff --git a/src/test/ui/codemap_tests/tab.rs b/src/test/ui/codemap_tests/tab.rs
index 0672b5508b6..146ad2283c2 100644
--- a/src/test/ui/codemap_tests/tab.rs
+++ b/src/test/ui/codemap_tests/tab.rs
@@ -13,3 +13,7 @@
 fn main() {
 	bar;
 }
+
+fn foo() {
+	"bar			boo"
+}
diff --git a/src/test/ui/codemap_tests/tab.stderr b/src/test/ui/codemap_tests/tab.stderr
index 657deca4e6d..b3fa9b128c5 100644
--- a/src/test/ui/codemap_tests/tab.stderr
+++ b/src/test/ui/codemap_tests/tab.stderr
@@ -1,8 +1,19 @@
 error[E0425]: cannot find value `bar` in this scope
   --> $DIR/tab.rs:14:2
    |
-14 | \tbar;
-   | \t^^^ not found in this scope
+14 |  bar;
+   |  ^^^ not found in this scope
 
-error: aborting due to previous error
+error[E0308]: mismatched types
+  --> $DIR/tab.rs:18:2
+   |
+17 | fn foo() {
+   |          - help: try adding a return type: `-> &'static str `
+18 |  "bar   boo"
+   |  ^^^^^^^^^^^ expected (), found reference
+   |
+   = note: expected type `()`
+              found type `&'static str`
+
+error: aborting due to 2 previous errors
 
diff --git a/src/test/ui/codemap_tests/tab_2.rs b/src/test/ui/codemap_tests/tab_2.rs
new file mode 100644
index 00000000000..d26d7974d85
--- /dev/null
+++ b/src/test/ui/codemap_tests/tab_2.rs
@@ -0,0 +1,15 @@
+// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+// ignore-tidy-tab
+
+fn main() {
+				""";
+}
diff --git a/src/test/ui/codemap_tests/tab_2.stderr b/src/test/ui/codemap_tests/tab_2.stderr
new file mode 100644
index 00000000000..a2b3ca7e4d4
--- /dev/null
+++ b/src/test/ui/codemap_tests/tab_2.stderr
@@ -0,0 +1,10 @@
+error: unterminated double quote string
+  --> $DIR/tab_2.rs:14:7
+   |
+14 |       """;
+   |  _______^
+15 | | }
+   | |__^
+
+error: aborting due to previous error
+
diff --git a/src/test/ui/codemap_tests/tab_3.rs b/src/test/ui/codemap_tests/tab_3.rs
new file mode 100644
index 00000000000..9b3513d5116
--- /dev/null
+++ b/src/test/ui/codemap_tests/tab_3.rs
@@ -0,0 +1,19 @@
+// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+// ignore-tidy-tab
+
+fn main() {
+	let some_vec = vec!["hi"];
+	some_vec.into_iter();
+	{
+		println!("{:?}", some_vec);
+	}
+}
diff --git a/src/test/ui/codemap_tests/tab_3.stderr b/src/test/ui/codemap_tests/tab_3.stderr
new file mode 100644
index 00000000000..f19f5f20d23
--- /dev/null
+++ b/src/test/ui/codemap_tests/tab_3.stderr
@@ -0,0 +1,13 @@
+error[E0382]: use of moved value: `some_vec`
+  --> $DIR/tab_3.rs:17:20
+   |
+15 |  some_vec.into_iter();
+   |  -------- value moved here
+16 |  {
+17 |   println!("{:?}", some_vec);
+   |                    ^^^^^^^^ value used here after move
+   |
+   = note: move occurs because `some_vec` has type `std::vec::Vec<&str>`, which does not implement the `Copy` trait
+
+error: aborting due to previous error
+