about summary refs log tree commit diff
path: root/src/librustc_errors
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2017-01-11 05:27:11 +0000
committerbors <bors@rust-lang.org>2017-01-11 05:27:11 +0000
commite57f061be20666eb0506f6f41551c798bbb38b60 (patch)
treecd8e01c09388b85eb7315fc9cde960cac96ddb51 /src/librustc_errors
parente4fee525e04838dabc82beed5ae1a06051be53fd (diff)
parent04e4a60b45ec4debd20be2327cb6859271502c05 (diff)
downloadrust-e57f061be20666eb0506f6f41551c798bbb38b60.tar.gz
rust-e57f061be20666eb0506f6f41551c798bbb38b60.zip
Auto merge of #38916 - estebank:pad-suggestion-list, r=nikomatsakis
Teach diagnostics to correct margin of multiline messages

Make the suggestion list have a correct padding:

```
error[E0308]: mismatched types
 --> file.rs:3:20
  |
3 |     let x: usize = "";
  |                    ^^ expected usize, found reference
  |
  = note: expected type `usize`
  = note:    found type `&'static str`
  = help: here are some functions which might fulfill your needs:
          - .len()
          - .foo()
          - .bar()
```
Diffstat (limited to 'src/librustc_errors')
-rw-r--r--src/librustc_errors/emitter.rs44
1 files changed, 41 insertions, 3 deletions
diff --git a/src/librustc_errors/emitter.rs b/src/librustc_errors/emitter.rs
index 808fe504b95..77c6c368364 100644
--- a/src/librustc_errors/emitter.rs
+++ b/src/librustc_errors/emitter.rs
@@ -703,6 +703,40 @@ impl EmitterWriter {
         }
     }
 
+    /// Add a left margin to every line but the first, given a padding length and the label being
+    /// displayed.
+    fn msg_with_padding(&self, msg: &str, padding: usize, label: &str) -> String {
+        // The extra 5 ` ` is padding that's always needed to align to the `note: `:
+        //
+        //   error: message
+        //     --> file.rs:13:20
+        //      |
+        //   13 |     <CODE>
+        //      |      ^^^^
+        //      |
+        //      = note: multiline
+        //              message
+        //   ++^^^----xx
+        //    |  |   | |
+        //    |  |   | magic `2`
+        //    |  |   length of label
+        //    |  magic `3`
+        //    `max_line_num_len`
+        let padding = (0..padding + label.len() + 5)
+            .map(|_| " ")
+            .collect::<String>();
+
+        msg.split('\n').enumerate().fold("".to_owned(), |mut acc, x| {
+            if x.0 != 0 {
+                acc.push_str("\n");
+                // Align every line with first one.
+                acc.push_str(&padding);
+            }
+            acc.push_str(&x.1);
+            acc
+        })
+    }
+
     fn emit_message_default(&mut self,
                             msp: &MultiSpan,
                             msg: &str,
@@ -721,7 +755,9 @@ impl EmitterWriter {
             draw_note_separator(&mut buffer, 0, max_line_num_len + 1);
             buffer.append(0, &level.to_string(), Style::HeaderMsg);
             buffer.append(0, ": ", Style::NoStyle);
-            buffer.append(0, msg, Style::NoStyle);
+
+            let message = self.msg_with_padding(msg, max_line_num_len, "note");
+            buffer.append(0, &message, Style::NoStyle);
         } else {
             buffer.append(0, &level.to_string(), Style::Level(level.clone()));
             match code {
@@ -854,7 +890,9 @@ impl EmitterWriter {
 
             buffer.append(0, &level.to_string(), Style::Level(level.clone()));
             buffer.append(0, ": ", Style::HeaderMsg);
-            buffer.append(0, msg, Style::HeaderMsg);
+
+            let message = self.msg_with_padding(msg, max_line_num_len, "suggestion");
+            buffer.append(0, &message, Style::HeaderMsg);
 
             let lines = cm.span_to_lines(primary_span).unwrap();
 
@@ -930,7 +968,7 @@ impl EmitterWriter {
                                                             max_line_num_len,
                                                             true) {
                                 Err(e) => panic!("failed to emit error: {}", e),
-                                _ => ()
+                                _ => (),
                             }
                         }
                     }