about summary refs log tree commit diff
path: root/src/librustc_errors
diff options
context:
space:
mode:
authorEsteban Küber <esteban@kuber.com.ar>2017-07-05 16:39:06 -0700
committerEsteban Küber <esteban@kuber.com.ar>2017-07-06 14:36:49 -0700
commit697c85a4f15b8dd560fb1f78129f5bfdd4baf1f4 (patch)
treef96e6e3662433fed1b78afd8641f065ffb6e3e48 /src/librustc_errors
parenteb478e23813b89944edfa602b6927e17c4c62f86 (diff)
downloadrust-697c85a4f15b8dd560fb1f78129f5bfdd4baf1f4.tar.gz
rust-697c85a4f15b8dd560fb1f78129f5bfdd4baf1f4.zip
Only underline suggestion if it is not the only code being shown
Diffstat (limited to 'src/librustc_errors')
-rw-r--r--src/librustc_errors/emitter.rs23
-rw-r--r--src/librustc_errors/lib.rs23
2 files changed, 28 insertions, 18 deletions
diff --git a/src/librustc_errors/emitter.rs b/src/librustc_errors/emitter.rs
index 99342351d92..a6a45c8b502 100644
--- a/src/librustc_errors/emitter.rs
+++ b/src/librustc_errors/emitter.rs
@@ -1078,17 +1078,14 @@ impl EmitterWriter {
 
             let suggestions = suggestion.splice_lines(cm.borrow());
             let span_start_pos = cm.lookup_char_pos(primary_sub.span.lo);
-            let span_end_pos = cm.lookup_char_pos(primary_sub.span.hi);
             let line_start = span_start_pos.line;
             draw_col_separator_no_space(&mut buffer, 1, max_line_num_len + 1);
             let mut row_num = 2;
-            for complete in suggestions.iter().take(MAX_SUGGESTIONS) {
+            for (&(ref complete, show_underline), ref sub) in suggestions
+                    .iter().zip(primary_sub.substitutions.iter()).take(MAX_SUGGESTIONS)
+            {
                 let mut line_pos = 0;
                 // Only show underline if there's a single suggestion and it is a single line
-                let show_underline = complete.lines().count() == 1
-                    && span_start_pos.line == span_end_pos.line
-                    && primary_sub.substitutions.len() == 1;
-
                 let mut lines = complete.lines();
                 for line in lines.by_ref().take(MAX_HIGHLIGHT_LINES) {
                     // Print the span column to avoid confusion
@@ -1099,11 +1096,13 @@ impl EmitterWriter {
                     // print the suggestion
                     draw_col_separator(&mut buffer, row_num, max_line_num_len + 1);
                     buffer.append(row_num, line, Style::NoStyle);
+                    line_pos += 1;
                     row_num += 1;
+                    // Only show an underline in the suggestions if the suggestion is not the
+                    // entirety of the code being shown and the displayed code is not multiline.
                     if show_underline {
                         draw_col_separator(&mut buffer, row_num, max_line_num_len + 1);
-
-                        let sub_len = primary_sub.substitutions[0].trim_right().len();
+                        let sub_len = sub.trim_right().len();
                         let underline_start = span_start_pos.col.0;
                         let underline_end = span_start_pos.col.0 + sub_len;
                         for p in underline_start..underline_end {
@@ -1114,19 +1113,19 @@ impl EmitterWriter {
                         }
                         row_num += 1;
                     }
-                    line_pos += 1;
                 }
 
                 // if we elided some lines, add an ellipsis
                 if let Some(_) = lines.next() {
-                    buffer.append(row_num, "...", Style::NoStyle);
-                } else if !show_underline && suggestions.len() <= MAX_SUGGESTIONS {
+                    buffer.puts(row_num, max_line_num_len - 1, "...", Style::LineNumber);
+                } else if !show_underline {
                     draw_col_separator_no_space(&mut buffer, row_num, max_line_num_len + 1);
+                    row_num += 1;
                 }
             }
             if suggestions.len() > MAX_SUGGESTIONS {
                 let msg = format!("and {} other candidates", suggestions.len() - MAX_SUGGESTIONS);
-                buffer.append(row_num, &msg, Style::NoStyle);
+                buffer.puts(row_num, 0, &msg, Style::NoStyle);
             }
             emit_to_destination(&buffer.render(), level, &mut self.dst)?;
         }
diff --git a/src/librustc_errors/lib.rs b/src/librustc_errors/lib.rs
index dd25f969414..7fb75a263f9 100644
--- a/src/librustc_errors/lib.rs
+++ b/src/librustc_errors/lib.rs
@@ -114,8 +114,8 @@ impl CodeSuggestion {
         self.substitution_parts.iter().map(|sub| sub.span)
     }
 
-    /// Returns the assembled code suggestions.
-    pub fn splice_lines(&self, cm: &CodeMapper) -> Vec<String> {
+    /// Returns the assembled code suggestions and wether they should be shown with an underline.
+    pub fn splice_lines(&self, cm: &CodeMapper) -> Vec<(String, bool)> {
         use syntax_pos::{CharPos, Loc, Pos};
 
         fn push_trailing(buf: &mut String,
@@ -138,7 +138,7 @@ impl CodeSuggestion {
         }
 
         if self.substitution_parts.is_empty() {
-            return vec![String::new()];
+            return vec![(String::new(), false)];
         }
 
         let mut primary_spans: Vec<_> = self.substitution_parts
@@ -175,14 +175,25 @@ impl CodeSuggestion {
         prev_hi.col = CharPos::from_usize(0);
 
         let mut prev_line = fm.get_line(lines.lines[0].line_index);
-        let mut bufs = vec![String::new(); self.substitutions()];
+        let mut bufs = vec![(String::new(), false); self.substitutions()];
 
         for (sp, substitutes) in primary_spans {
             let cur_lo = cm.lookup_char_pos(sp.lo);
-            for (buf, substitute) in bufs.iter_mut().zip(substitutes) {
+            for (&mut (ref mut buf, ref mut underline), substitute) in bufs.iter_mut()
+                                                                           .zip(substitutes) {
                 if prev_hi.line == cur_lo.line {
                     push_trailing(buf, prev_line.as_ref(), &prev_hi, Some(&cur_lo));
+
+                    // Only show an underline in the suggestions if the suggestion is not the
+                    // entirety of the code being shown and the displayed code is not multiline.
+                    if prev_line.as_ref().unwrap().trim().len() > 0
+                        && !substitute.ends_with('\n')
+                        && substitute.lines().count() == 1
+                    {
+                        *underline = true;
+                    }
                 } else {
+                    *underline = false;
                     push_trailing(buf, prev_line.as_ref(), &prev_hi, None);
                     // push lines between the previous and current span (if any)
                     for idx in prev_hi.line..(cur_lo.line - 1) {
@@ -200,7 +211,7 @@ impl CodeSuggestion {
             prev_hi = cm.lookup_char_pos(sp.hi);
             prev_line = fm.get_line(prev_hi.line - 1);
         }
-        for buf in &mut bufs {
+        for &mut (ref mut buf, _) in &mut bufs {
             // if the replacement already ends with a newline, don't print the next line
             if !buf.ends_with('\n') {
                 push_trailing(buf, prev_line.as_ref(), &prev_hi, None);