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-01-08 12:56:34 -0800
committerEsteban Küber <esteban@kuber.com.ar>2017-01-08 13:12:13 -0800
commitf65a907ef9e21a350118d2efa323ebf49c3ec1cf (patch)
tree5b8d7b8048bb07aeacce50264b6035b4c4efa332 /src/librustc_errors
parent43b10fa8ed3dd96684ec89812dc7810431cd0d01 (diff)
downloadrust-f65a907ef9e21a350118d2efa323ebf49c3ec1cf.tar.gz
rust-f65a907ef9e21a350118d2efa323ebf49c3ec1cf.zip
Use fold instead of collect/join and add comments
Diffstat (limited to 'src/librustc_errors')
-rw-r--r--src/librustc_errors/emitter.rs49
1 files changed, 39 insertions, 10 deletions
diff --git a/src/librustc_errors/emitter.rs b/src/librustc_errors/emitter.rs
index 015997211bb..88373bf988d 100644
--- a/src/librustc_errors/emitter.rs
+++ b/src/librustc_errors/emitter.rs
@@ -924,19 +924,48 @@ impl EmitterWriter {
                             }
                         },
                         None => {
+                            // Diagnostic with lists need to render the list items at the
+                            // appropriate depth and composed into the body of the message.
                             let msg = if child.list.len() == 0 {
+                                // Diagnostics without lists just need the original message
                                 child.message.to_owned()
                             } else {
-                                format!("{}\n{}",
-                                        &child.message,
-                                        &child.list.iter().map(|item| {
-                                            format!("{}         - {}",
-                                                    (0..max_line_num_len)
-                                                          .map(|_| " ")
-                                                          .collect::<String>(),
-                                                    item)
-                                        }).collect::<Vec<String>>()
-                                        .join("\n"))
+                                // Diagnostic with a list of items needs to be rendered with the
+                                // appropriate padding at the left to have a consistent margin with
+                                // the `note: ` text.
+
+                                // Add as many ` ` chars at the beggining to align the `- item`
+                                // text to the beggining of the `note: ` text. The extra 9 ` ` is
+                                // the padding that's always needed to align to the `note: `.
+                                let padding = (0..max_line_num_len + 9)
+                                    .map(|_| " ")
+                                    .collect::<String>();
+
+                                // Concatenate the message and all the list items, properly aligned
+                                child.list.iter().fold(child.message.to_owned(), |mut acc, x| {
+                                    acc.push_str("\n");
+                                    acc.push_str(&padding);
+                                    acc.push_str("- ");
+                                    acc.push_str(x);
+                                    acc
+                                })
+                                // msg will now be:
+                                //
+                                //     child.message's content
+                                //              - item 1
+                                //              - item 2
+                                //
+                                // and the diagnostic will look like
+                                //
+                                //     error: message
+                                //      --> file.rs:3:20
+                                //       |
+                                //     3 |     <Code>
+                                //       |      ^^^^ highlight
+                                //       |
+                                //       = help: child.message's content
+                                //               - item 1
+                                //               - item 2
                             };
                             match self.emit_message_default(&child.span,
                                                             &msg,