Skip to content

Commit c73bcf0

Browse files
committed
show macro backtrace with env var
1 parent 5041b3b commit c73bcf0

File tree

2 files changed

+47
-13
lines changed

2 files changed

+47
-13
lines changed

src/librustc_errors/emitter.rs

Lines changed: 46 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ use std::rc::Rc;
2323
use term;
2424
use std::collections::HashMap;
2525
use std::cmp::min;
26+
use std::env;
2627

2728
/// Emitter trait for emitting errors.
2829
pub trait Emitter {
@@ -786,18 +787,20 @@ impl EmitterWriter {
786787
fn fix_multispans_in_std_macros(&mut self,
787788
span: &mut MultiSpan,
788789
children: &mut Vec<SubDiagnostic>) {
789-
let mut spans_updated = self.fix_multispan_in_std_macros(span);
790-
for child in children.iter_mut() {
791-
spans_updated |= self.fix_multispan_in_std_macros(&mut child.span);
792-
}
793-
if spans_updated {
794-
children.push(SubDiagnostic {
795-
level: Level::Note,
796-
message: vec![("this error originates in a macro outside of the current crate"
797-
.to_string(), Style::NoStyle)],
798-
span: MultiSpan::new(),
799-
render_span: None,
800-
});
790+
if env::var_os("RUST_MACRO_BACKTRACE").is_none() {
791+
let mut spans_updated = self.fix_multispan_in_std_macros(span);
792+
for child in children.iter_mut() {
793+
spans_updated |= self.fix_multispan_in_std_macros(&mut child.span);
794+
}
795+
if spans_updated {
796+
children.push(SubDiagnostic {
797+
level: Level::Note,
798+
message: vec![("this error originates in a macro outside of the current crate (run with RUST_MACRO_BACKTRACE=1 for more info)"
799+
.to_string(), Style::NoStyle)],
800+
span: MultiSpan::new(),
801+
render_span: None,
802+
});
803+
}
801804
}
802805
}
803806

@@ -1079,6 +1082,12 @@ impl EmitterWriter {
10791082
}
10801083
}
10811084

1085+
if env::var_os("RUST_MACRO_BACKTRACE").is_some() {
1086+
if let Some(ref primary_span) = msp.primary_span().as_ref() {
1087+
self.render_macro_backtrace_old_school(primary_span, &mut buffer)?;
1088+
}
1089+
}
1090+
10821091
// final step: take our styled buffer, render it, then output it
10831092
emit_to_destination(&buffer.render(), level, &mut self.dst, self.short_message)?;
10841093

@@ -1226,6 +1235,30 @@ impl EmitterWriter {
12261235
}
12271236
}
12281237
}
1238+
1239+
fn render_macro_backtrace_old_school(&self,
1240+
sp: &Span,
1241+
buffer: &mut StyledBuffer) -> io::Result<()> {
1242+
if let Some(ref cm) = self.cm {
1243+
for trace in sp.macro_backtrace().iter().rev() {
1244+
let line_offset = buffer.num_lines();
1245+
1246+
let mut diag_string =
1247+
format!("in this expansion of {}", trace.macro_decl_name);
1248+
if let Some(def_site_span) = trace.def_site_span {
1249+
diag_string.push_str(
1250+
&format!(" (defined in {})",
1251+
cm.span_to_filename(def_site_span)));
1252+
}
1253+
let snippet = cm.span_to_string(trace.call_site);
1254+
buffer.append(line_offset, &format!("{} ", snippet), Style::NoStyle);
1255+
buffer.append(line_offset, "note", Style::Level(Level::Note));
1256+
buffer.append(line_offset, ": ", Style::NoStyle);
1257+
buffer.append(line_offset, &diag_string, Style::OldSchoolNoteText);
1258+
}
1259+
}
1260+
Ok(())
1261+
}
12291262
}
12301263

12311264
fn draw_col_separator(buffer: &mut StyledBuffer, line: usize, col: usize) {
@@ -1415,7 +1448,7 @@ impl Destination {
14151448
}
14161449
}
14171450
Style::Quotation => {}
1418-
Style::HeaderMsg => {
1451+
Style::OldSchoolNoteText | Style::HeaderMsg => {
14191452
self.start_attr(term::Attr::Bold)?;
14201453
if cfg!(windows) {
14211454
self.start_attr(term::Attr::ForegroundColor(term::color::BRIGHT_WHITE))?;

src/librustc_errors/snippet.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@ pub enum Style {
213213
UnderlineSecondary,
214214
LabelPrimary,
215215
LabelSecondary,
216+
OldSchoolNoteText,
216217
NoStyle,
217218
Level(Level),
218219
Highlight,

0 commit comments

Comments
 (0)