Mention first and last macro in backtrace

This commit is contained in:
Michael Goulet 2022-06-20 19:25:52 -07:00
parent 475aec1188
commit 01b2379e49
78 changed files with 157 additions and 144 deletions

View file

@ -399,11 +399,11 @@ pub trait Emitter {
) {
// Check for spans in macros, before `fix_multispans_in_extern_macros`
// has a chance to replace them.
let has_macro_spans = iter::once(&*span)
let has_macro_spans: Vec<_> = iter::once(&*span)
.chain(children.iter().map(|child| &child.span))
.flat_map(|span| span.primary_spans())
.flat_map(|sp| sp.macro_backtrace())
.find_map(|expn_data| {
.filter_map(|expn_data| {
match expn_data.kind {
ExpnKind::Root => None,
@ -413,7 +413,8 @@ pub trait Emitter {
ExpnKind::Macro(macro_kind, name) => Some((macro_kind, name)),
}
});
})
.collect();
if !backtrace {
self.fix_multispans_in_extern_macros(source_map, span, children);
@ -422,11 +423,22 @@ pub trait Emitter {
self.render_multispans_macro_backtrace(span, children, backtrace);
if !backtrace {
if let Some((macro_kind, name)) = has_macro_spans {
let descr = macro_kind.descr();
if let Some((macro_kind, name)) = has_macro_spans.first() {
// Mark the actual macro this originates from
let and_then = if let Some((macro_kind, last_name)) = has_macro_spans.last()
&& last_name != name
{
let descr = macro_kind.descr();
format!(
" which comes from the expansion of the {descr} `{last_name}`",
)
} else {
"".to_string()
};
let descr = macro_kind.descr();
let msg = format!(
"this {level} originates in the {descr} `{name}` \
"this {level} originates in the {descr} `{name}`{and_then} \
(in Nightly builds, run with -Z macro-backtrace for more info)",
);