@@ -271,18 +271,24 @@ pub trait Emitter {
271271 }
272272 }
273273
274- fn render_multispans_macro_backtrace_and_fix_extern_macros (
274+ fn fix_multispans_in_extern_macros_and_render_macro_backtrace (
275275 & self ,
276276 source_map : & Option < Lrc < SourceMap > > ,
277277 span : & mut MultiSpan ,
278278 children : & mut Vec < SubDiagnostic > ,
279279 level : & Level ,
280280 backtrace : bool ,
281281 ) {
282- self . render_multispans_macro_backtrace ( source_map, span, children, backtrace) ;
282+ let mut external_spans_updated = false ;
283+ if !backtrace {
284+ external_spans_updated =
285+ self . fix_multispans_in_extern_macros ( source_map, span, children) ;
286+ }
287+
288+ self . render_multispans_macro_backtrace ( span, children, backtrace) ;
283289
284290 if !backtrace {
285- if self . fix_multispans_in_extern_macros ( source_map , span , children ) {
291+ if external_spans_updated {
286292 let msg = format ! (
287293 "this {} originates in a macro outside of the current crate \
288294 (in Nightly builds, run with -Z macro-backtrace for more info)",
@@ -301,42 +307,33 @@ pub trait Emitter {
301307
302308 fn render_multispans_macro_backtrace (
303309 & self ,
304- source_map : & Option < Lrc < SourceMap > > ,
305310 span : & mut MultiSpan ,
306311 children : & mut Vec < SubDiagnostic > ,
307312 backtrace : bool ,
308313 ) {
309- self . render_multispan_macro_backtrace ( source_map , span, backtrace) ;
314+ self . render_multispan_macro_backtrace ( span, backtrace) ;
310315 for child in children. iter_mut ( ) {
311- self . render_multispan_macro_backtrace ( source_map , & mut child. span , backtrace) ;
316+ self . render_multispan_macro_backtrace ( & mut child. span , backtrace) ;
312317 }
313318 }
314319
315- fn render_multispan_macro_backtrace (
316- & self ,
317- source_map : & Option < Lrc < SourceMap > > ,
318- span : & mut MultiSpan ,
319- always_backtrace : bool ,
320- ) {
321- let sm = match source_map {
322- Some ( ref sm) => sm,
323- None => return ,
324- } ;
325-
320+ fn render_multispan_macro_backtrace ( & self , span : & mut MultiSpan , always_backtrace : bool ) {
326321 let mut new_labels: Vec < ( Span , String ) > = vec ! [ ] ;
327322
328- // First, find all the spans in <*macros> and point instead at their use site
329323 for & sp in span. primary_spans ( ) {
330324 if sp. is_dummy ( ) {
331325 continue ;
332326 }
327+
328+ // FIXME(eddyb) use `retain` on `macro_backtrace` to remove all the
329+ // entries we don't want to print, to make sure the indices being
330+ // printed are contiguous (or omitted if there's only one entry).
333331 let macro_backtrace: Vec < _ > = sp. macro_backtrace ( ) . collect ( ) ;
334332 for ( i, trace) in macro_backtrace. iter ( ) . rev ( ) . enumerate ( ) {
335- // Only show macro locations that are local
336- // and display them like a span_note
337333 if trace. def_site . is_dummy ( ) {
338334 continue ;
339335 }
336+
340337 if always_backtrace {
341338 new_labels. push ( (
342339 trace. def_site ,
@@ -353,9 +350,21 @@ pub trait Emitter {
353350 ) ,
354351 ) ) ;
355352 }
356- // Check to make sure we're not in any <*macros>
357- if !sm. span_to_filename ( trace. def_site ) . is_macros ( )
358- && matches ! ( trace. kind, ExpnKind :: Macro ( MacroKind :: Bang , _) )
353+
354+ // Don't add a label on the call site if the diagnostic itself
355+ // already points to (a part of) that call site, as the label
356+ // is meant for showing the relevant invocation when the actual
357+ // diagnostic is pointing to some part of macro definition.
358+ //
359+ // This also handles the case where an external span got replaced
360+ // with the call site span by `fix_multispans_in_extern_macros`.
361+ //
362+ // NB: `-Zmacro-backtrace` overrides this, for uniformity, as the
363+ // "in this expansion of" label above is always added in that mode,
364+ // and it needs an "in this macro invocation" label to match that.
365+ let redundant_span = trace. call_site . contains ( sp) ;
366+
367+ if !redundant_span && matches ! ( trace. kind, ExpnKind :: Macro ( MacroKind :: Bang , _) )
359368 || always_backtrace
360369 {
361370 new_labels. push ( (
@@ -371,9 +380,9 @@ pub trait Emitter {
371380 } ,
372381 ) ,
373382 ) ) ;
374- if !always_backtrace {
375- break ;
376- }
383+ }
384+ if !always_backtrace {
385+ break ;
377386 }
378387 }
379388 }
@@ -447,7 +456,7 @@ impl Emitter for EmitterWriter {
447456 let mut children = diag. children . clone ( ) ;
448457 let ( mut primary_span, suggestions) = self . primary_span_formatted ( & diag) ;
449458
450- self . render_multispans_macro_backtrace_and_fix_extern_macros (
459+ self . fix_multispans_in_extern_macros_and_render_macro_backtrace (
451460 & self . sm ,
452461 & mut primary_span,
453462 & mut children,
0 commit comments