about summary refs log tree commit diff
path: root/src/librustc_errors
diff options
context:
space:
mode:
authorAlex Burka <alex@alexburka.com>2017-10-26 04:39:37 +0000
committerAlex Burka <alex@alexburka.com>2017-11-19 22:22:22 +0000
commitc73bcf043e94b32e2d53ecfd3e53a0535b320456 (patch)
tree2224466e5c781a49e49490898441ee892f586a41 /src/librustc_errors
parent5041b3bb3d953a14f32b15d1e41341c629acae12 (diff)
downloadrust-c73bcf043e94b32e2d53ecfd3e53a0535b320456.tar.gz
rust-c73bcf043e94b32e2d53ecfd3e53a0535b320456.zip
show macro backtrace with env var
Diffstat (limited to 'src/librustc_errors')
-rw-r--r--src/librustc_errors/emitter.rs59
-rw-r--r--src/librustc_errors/snippet.rs1
2 files changed, 47 insertions, 13 deletions
diff --git a/src/librustc_errors/emitter.rs b/src/librustc_errors/emitter.rs
index 57523d2c058..7ce902d0a6f 100644
--- a/src/librustc_errors/emitter.rs
+++ b/src/librustc_errors/emitter.rs
@@ -23,6 +23,7 @@ use std::rc::Rc;
 use term;
 use std::collections::HashMap;
 use std::cmp::min;
+use std::env;
 
 /// Emitter trait for emitting errors.
 pub trait Emitter {
@@ -786,18 +787,20 @@ impl EmitterWriter {
     fn fix_multispans_in_std_macros(&mut self,
                                     span: &mut MultiSpan,
                                     children: &mut Vec<SubDiagnostic>) {
-        let mut spans_updated = self.fix_multispan_in_std_macros(span);
-        for child in children.iter_mut() {
-            spans_updated |= self.fix_multispan_in_std_macros(&mut child.span);
-        }
-        if spans_updated {
-            children.push(SubDiagnostic {
-                level: Level::Note,
-                message: vec![("this error originates in a macro outside of the current crate"
-                    .to_string(), Style::NoStyle)],
-                span: MultiSpan::new(),
-                render_span: None,
-            });
+        if env::var_os("RUST_MACRO_BACKTRACE").is_none() {
+            let mut spans_updated = self.fix_multispan_in_std_macros(span);
+            for child in children.iter_mut() {
+                spans_updated |= self.fix_multispan_in_std_macros(&mut child.span);
+            }
+            if spans_updated {
+                children.push(SubDiagnostic {
+                    level: Level::Note,
+                    message: vec![("this error originates in a macro outside of the current crate (run with RUST_MACRO_BACKTRACE=1 for more info)"
+                        .to_string(), Style::NoStyle)],
+                    span: MultiSpan::new(),
+                    render_span: None,
+                });
+            }
         }
     }
 
@@ -1079,6 +1082,12 @@ impl EmitterWriter {
             }
         }
 
+        if env::var_os("RUST_MACRO_BACKTRACE").is_some() {
+            if let Some(ref primary_span) = msp.primary_span().as_ref() {
+                self.render_macro_backtrace_old_school(primary_span, &mut buffer)?;
+            }
+        }
+
         // final step: take our styled buffer, render it, then output it
         emit_to_destination(&buffer.render(), level, &mut self.dst, self.short_message)?;
 
@@ -1226,6 +1235,30 @@ impl EmitterWriter {
             }
         }
     }
+
+    fn render_macro_backtrace_old_school(&self,
+                                         sp: &Span,
+                                         buffer: &mut StyledBuffer) -> io::Result<()> {
+        if let Some(ref cm) = self.cm {
+            for trace in sp.macro_backtrace().iter().rev() {
+                let line_offset = buffer.num_lines();
+ 
+                let mut diag_string =
+                    format!("in this expansion of {}", trace.macro_decl_name);
+                if let Some(def_site_span) = trace.def_site_span {
+                    diag_string.push_str(
+                        &format!(" (defined in {})",
+                            cm.span_to_filename(def_site_span)));
+                }
+                let snippet = cm.span_to_string(trace.call_site);
+                buffer.append(line_offset, &format!("{} ", snippet), Style::NoStyle);
+                buffer.append(line_offset, "note", Style::Level(Level::Note));
+                buffer.append(line_offset, ": ", Style::NoStyle);
+                buffer.append(line_offset, &diag_string, Style::OldSchoolNoteText);
+            }
+        }
+        Ok(())
+    }
 }
 
 fn draw_col_separator(buffer: &mut StyledBuffer, line: usize, col: usize) {
@@ -1415,7 +1448,7 @@ impl Destination {
                 }
             }
             Style::Quotation => {}
-            Style::HeaderMsg => {
+            Style::OldSchoolNoteText | Style::HeaderMsg => {
                 self.start_attr(term::Attr::Bold)?;
                 if cfg!(windows) {
                     self.start_attr(term::Attr::ForegroundColor(term::color::BRIGHT_WHITE))?;
diff --git a/src/librustc_errors/snippet.rs b/src/librustc_errors/snippet.rs
index 2e8deeee5a5..b76036b22df 100644
--- a/src/librustc_errors/snippet.rs
+++ b/src/librustc_errors/snippet.rs
@@ -213,6 +213,7 @@ pub enum Style {
     UnderlineSecondary,
     LabelPrimary,
     LabelSecondary,
+    OldSchoolNoteText,
     NoStyle,
     Level(Level),
     Highlight,