@@ -271,18 +271,24 @@ pub trait Emitter {
271
271
}
272
272
}
273
273
274
- fn render_multispans_macro_backtrace_and_fix_extern_macros (
274
+ fn fix_multispans_in_extern_macros_and_render_macro_backtrace (
275
275
& self ,
276
276
source_map : & Option < Lrc < SourceMap > > ,
277
277
span : & mut MultiSpan ,
278
278
children : & mut Vec < SubDiagnostic > ,
279
279
level : & Level ,
280
280
backtrace : bool ,
281
281
) {
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) ;
283
289
284
290
if !backtrace {
285
- if self . fix_multispans_in_extern_macros ( source_map , span , children ) {
291
+ if external_spans_updated {
286
292
let msg = format ! (
287
293
"this {} originates in a macro outside of the current crate \
288
294
(in Nightly builds, run with -Z macro-backtrace for more info)",
@@ -301,42 +307,33 @@ pub trait Emitter {
301
307
302
308
fn render_multispans_macro_backtrace (
303
309
& self ,
304
- source_map : & Option < Lrc < SourceMap > > ,
305
310
span : & mut MultiSpan ,
306
311
children : & mut Vec < SubDiagnostic > ,
307
312
backtrace : bool ,
308
313
) {
309
- self . render_multispan_macro_backtrace ( source_map , span, backtrace) ;
314
+ self . render_multispan_macro_backtrace ( span, backtrace) ;
310
315
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) ;
312
317
}
313
318
}
314
319
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 ) {
326
321
let mut new_labels: Vec < ( Span , String ) > = vec ! [ ] ;
327
322
328
- // First, find all the spans in <*macros> and point instead at their use site
329
323
for & sp in span. primary_spans ( ) {
330
324
if sp. is_dummy ( ) {
331
325
continue ;
332
326
}
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).
333
331
let macro_backtrace: Vec < _ > = sp. macro_backtrace ( ) . collect ( ) ;
334
332
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
337
333
if trace. def_site . is_dummy ( ) {
338
334
continue ;
339
335
}
336
+
340
337
if always_backtrace {
341
338
new_labels. push ( (
342
339
trace. def_site ,
@@ -353,9 +350,21 @@ pub trait Emitter {
353
350
) ,
354
351
) ) ;
355
352
}
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 , _) )
359
368
|| always_backtrace
360
369
{
361
370
new_labels. push ( (
@@ -371,9 +380,9 @@ pub trait Emitter {
371
380
} ,
372
381
) ,
373
382
) ) ;
374
- if !always_backtrace {
375
- break ;
376
- }
383
+ }
384
+ if !always_backtrace {
385
+ break ;
377
386
}
378
387
}
379
388
}
@@ -447,7 +456,7 @@ impl Emitter for EmitterWriter {
447
456
let mut children = diag. children . clone ( ) ;
448
457
let ( mut primary_span, suggestions) = self . primary_span_formatted ( & diag) ;
449
458
450
- self . render_multispans_macro_backtrace_and_fix_extern_macros (
459
+ self . fix_multispans_in_extern_macros_and_render_macro_backtrace (
451
460
& self . sm ,
452
461
& mut primary_span,
453
462
& mut children,
0 commit comments