]> git.lizzy.rs Git - rust.git/commitdiff
Use fold instead of collect/join and add comments
authorEsteban Küber <esteban@kuber.com.ar>
Sun, 8 Jan 2017 20:56:34 +0000 (12:56 -0800)
committerEsteban Küber <esteban@kuber.com.ar>
Sun, 8 Jan 2017 21:12:13 +0000 (13:12 -0800)
src/librustc_errors/emitter.rs

index 015997211bb3f07d03a617a8a782b9b962d32662..88373bf988da790369fa2af7b0fbfe48e12cfaf9 100644 (file)
@@ -924,19 +924,48 @@ fn emit_messages_default(&mut self,
                             }
                         },
                         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,