Skip to content

Commit ab08097

Browse files
committed
rustc_errors: hide "in this macro invocation" when redundant, more explicitly.
1 parent 5eaa9a1 commit ab08097

9 files changed

+53
-68
lines changed

src/librustc_errors/annotate_snippet_emitter_writer.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ impl Emitter for AnnotateSnippetEmitterWriter {
3232
let mut children = diag.children.clone();
3333
let (mut primary_span, suggestions) = self.primary_span_formatted(&diag);
3434

35-
self.render_multispans_macro_backtrace_and_fix_extern_macros(
35+
self.fix_multispans_in_extern_macros_and_render_macro_backtrace(
3636
&self.source_map,
3737
&mut primary_span,
3838
&mut children,

src/librustc_errors/emitter.rs

Lines changed: 36 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -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,

src/test/ui/macros/same-sequence-span.stderr

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,7 @@ error: `$x:expr` may be followed by `=`, which is not allowed for `expr` fragmen
3333
--> $DIR/same-sequence-span.rs:19:1
3434
|
3535
LL | proc_macro_sequence::make_foo!();
36-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
37-
| |
38-
| not allowed after `expr` fragments
39-
| in this macro invocation
36+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ not allowed after `expr` fragments
4037
|
4138
= note: allowed there are: `=>`, `,` or `;`
4239

src/test/ui/proc-macro/generate-mod.stderr

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@ error[E0412]: cannot find type `FromOutside` in this scope
22
--> $DIR/generate-mod.rs:9:1
33
|
44
LL | generate_mod::check!();
5-
| ^^^^^^^^^^^^^^^^^^^^^^^
6-
| |
7-
| not found in this scope
8-
| in this macro invocation
5+
| ^^^^^^^^^^^^^^^^^^^^^^^ not found in this scope
96
|
107
= note: possible candidate is found in another module, you can import it into scope:
118
FromOutside
@@ -14,10 +11,7 @@ error[E0412]: cannot find type `Outer` in this scope
1411
--> $DIR/generate-mod.rs:9:1
1512
|
1613
LL | generate_mod::check!();
17-
| ^^^^^^^^^^^^^^^^^^^^^^^
18-
| |
19-
| not found in this scope
20-
| in this macro invocation
14+
| ^^^^^^^^^^^^^^^^^^^^^^^ not found in this scope
2115
|
2216
= note: possible candidate is found in another module, you can import it into scope:
2317
Outer

src/test/ui/proc-macro/invalid-punct-ident-4.stderr

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@ error: unexpected closing delimiter: `)`
22
--> $DIR/invalid-punct-ident-4.rs:6:1
33
|
44
LL | lexer_failure!();
5-
| ^^^^^^^^^^^^^^^^^
6-
| |
7-
| unexpected closing delimiter
8-
| in this macro invocation
5+
| ^^^^^^^^^^^^^^^^^ unexpected closing delimiter
96

107
error: proc macro panicked
118
--> $DIR/invalid-punct-ident-4.rs:6:1

src/test/ui/proc-macro/lints_in_proc_macros.stderr

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@ error[E0425]: cannot find value `foobar2` in this scope
22
--> $DIR/lints_in_proc_macros.rs:12:5
33
|
44
LL | bang_proc_macro2!();
5-
| ^^^^^^^^^^^^^^^^^^^^
6-
| |
7-
| help: a local variable with a similar name exists: `foobar`
8-
| in this macro invocation
5+
| ^^^^^^^^^^^^^^^^^^^^ help: a local variable with a similar name exists: `foobar`
96

107
error: aborting due to previous error
118

src/test/ui/proc-macro/mixed-site-span.stderr

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,13 @@ error[E0426]: use of undeclared label `'label_use`
22
--> $DIR/mixed-site-span.rs:15:9
33
|
44
LL | proc_macro_rules!();
5-
| ^^^^^^^^^^^^^^^^^^^^
6-
| |
7-
| undeclared label `'label_use`
8-
| in this macro invocation
5+
| ^^^^^^^^^^^^^^^^^^^^ undeclared label `'label_use`
96

107
error[E0425]: cannot find value `local_use` in this scope
118
--> $DIR/mixed-site-span.rs:15:9
129
|
1310
LL | proc_macro_rules!();
14-
| ^^^^^^^^^^^^^^^^^^^^
15-
| |
16-
| not found in this scope
17-
| in this macro invocation
11+
| ^^^^^^^^^^^^^^^^^^^^ not found in this scope
1812

1913
error[E0425]: cannot find value `local_def` in this scope
2014
--> $DIR/mixed-site-span.rs:19:9

src/test/ui/proc-macro/subspan.stderr

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error: found 'hi's
22
--> $DIR/subspan.rs:11:1
33
|
44
LL | subspan!("hi");
5-
| ^^^^^^^^^^^^^^^ in this macro invocation
5+
| ^^^^^^^^^^^^^^^
66
|
77
note: here
88
--> $DIR/subspan.rs:11:11
@@ -14,7 +14,7 @@ error: found 'hi's
1414
--> $DIR/subspan.rs:14:1
1515
|
1616
LL | subspan!("hihi");
17-
| ^^^^^^^^^^^^^^^^^ in this macro invocation
17+
| ^^^^^^^^^^^^^^^^^
1818
|
1919
note: here
2020
--> $DIR/subspan.rs:14:11
@@ -26,7 +26,7 @@ error: found 'hi's
2626
--> $DIR/subspan.rs:17:1
2727
|
2828
LL | subspan!("hihihi");
29-
| ^^^^^^^^^^^^^^^^^^^ in this macro invocation
29+
| ^^^^^^^^^^^^^^^^^^^
3030
|
3131
note: here
3232
--> $DIR/subspan.rs:17:11
@@ -38,7 +38,7 @@ error: found 'hi's
3838
--> $DIR/subspan.rs:20:1
3939
|
4040
LL | subspan!("why I hide? hi!");
41-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ in this macro invocation
41+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4242
|
4343
note: here
4444
--> $DIR/subspan.rs:20:17
@@ -50,7 +50,7 @@ error: found 'hi's
5050
--> $DIR/subspan.rs:21:1
5151
|
5252
LL | subspan!("hey, hi, hidy, hidy, hi hi");
53-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ in this macro invocation
53+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
5454
|
5555
note: here
5656
--> $DIR/subspan.rs:21:16
@@ -62,7 +62,7 @@ error: found 'hi's
6262
--> $DIR/subspan.rs:22:1
6363
|
6464
LL | subspan!("this is a hi, and this is another hi");
65-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ in this macro invocation
65+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6666
|
6767
note: here
6868
--> $DIR/subspan.rs:22:12
@@ -74,7 +74,7 @@ error: found 'hi's
7474
--> $DIR/subspan.rs:23:1
7575
|
7676
LL | subspan!("how are you this evening");
77-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ in this macro invocation
77+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
7878
|
7979
note: here
8080
--> $DIR/subspan.rs:23:24
@@ -86,7 +86,7 @@ error: found 'hi's
8686
--> $DIR/subspan.rs:24:1
8787
|
8888
LL | subspan!("this is highly eradic");
89-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ in this macro invocation
89+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
9090
|
9191
note: here
9292
--> $DIR/subspan.rs:24:12

src/test/ui/suggestions/dont-suggest-deref-inside-macro-issue-58298.stderr

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,7 @@ error[E0308]: mismatched types
44
LL | / intrinsic_match! {
55
LL | | "abc"
66
LL | | };
7-
| | ^
8-
| | |
9-
| |______expected `&str`, found struct `std::string::String`
10-
| in this macro invocation
7+
| |______^ expected `&str`, found struct `std::string::String`
118
|
129
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z macro-backtrace for more info)
1310

0 commit comments

Comments
 (0)