about summary refs log tree commit diff
path: root/compiler/rustc_errors
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2024-08-06 07:06:24 +0000
committerbors <bors@rust-lang.org>2024-08-06 07:06:24 +0000
commit93ea767e2928589b74296ba85b57d80e108db712 (patch)
treea6e97311e65b0307c2ffd5846ff977f5a15bea36 /compiler/rustc_errors
parent8c7e0e160831866bc1a40691a39455aac21271c0 (diff)
parent1bbaf6eb2f08a4f3d5c442560909f7109b66bf95 (diff)
downloadrust-93ea767e2928589b74296ba85b57d80e108db712.tar.gz
rust-93ea767e2928589b74296ba85b57d80e108db712.zip
Auto merge of #126804 - estebank:short-error-primary-label, r=davidtwco
On short error format, append primary span label to message

The `error-format=short` output only displays the path, error code and main error message all in the same line. We now add the primary span label as well after the error message, to provide more context.
Diffstat (limited to 'compiler/rustc_errors')
-rw-r--r--compiler/rustc_errors/src/emitter.rs22
1 files changed, 21 insertions, 1 deletions
diff --git a/compiler/rustc_errors/src/emitter.rs b/compiler/rustc_errors/src/emitter.rs
index d673646ace4..483b757f20c 100644
--- a/compiler/rustc_errors/src/emitter.rs
+++ b/compiler/rustc_errors/src/emitter.rs
@@ -1346,10 +1346,11 @@ impl HumanEmitter {
                 buffer.append(0, ": ", header_style);
                 label_width += 2;
             }
+            let mut line = 0;
             for (text, _) in msgs.iter() {
                 let text = self.translate_message(text, args).map_err(Report::new).unwrap();
                 // Account for newlines to align output to its label.
-                for (line, text) in normalize_whitespace(&text).lines().enumerate() {
+                for text in normalize_whitespace(&text).lines() {
                     buffer.append(
                         line,
                         &format!(
@@ -1359,6 +1360,25 @@ impl HumanEmitter {
                         ),
                         header_style,
                     );
+                    line += 1;
+                }
+            }
+            if self.short_message {
+                let labels = msp
+                    .span_labels()
+                    .into_iter()
+                    .filter_map(|label| match label.label {
+                        Some(msg) if label.is_primary => {
+                            let text = self.translate_message(&msg, args).ok()?;
+                            if !text.trim().is_empty() { Some(text.to_string()) } else { None }
+                        }
+                        _ => None,
+                    })
+                    .collect::<Vec<_>>()
+                    .join(", ");
+                if !labels.is_empty() {
+                    buffer.append(line, ": ", Style::NoStyle);
+                    buffer.append(line, &labels, Style::NoStyle);
                 }
             }
         }