Skip to content

Commit 4c7eb59

Browse files
committed
rustc_macros: don't limit the -Zmacro-backtrace suggestion to extern macros.
1 parent ab08097 commit 4c7eb59

File tree

280 files changed

+888
-119
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

280 files changed

+888
-119
lines changed

src/librustc_errors/emitter.rs

Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ use std::borrow::Cow;
2626
use std::cmp::{max, min, Reverse};
2727
use std::io;
2828
use std::io::prelude::*;
29+
use std::iter;
2930
use std::path::Path;
3031
use termcolor::{Ansi, BufferWriter, ColorChoice, ColorSpec, StandardStream};
3132
use termcolor::{Buffer, Color, WriteColor};
@@ -279,20 +280,41 @@ pub trait Emitter {
279280
level: &Level,
280281
backtrace: bool,
281282
) {
282-
let mut external_spans_updated = false;
283+
// Check for spans in macros, before `fix_multispans_in_extern_macros`
284+
// has a chance to replace them.
285+
let has_macro_spans = iter::once(&*span)
286+
.chain(children.iter().map(|child| &child.span))
287+
.flat_map(|span| span.primary_spans())
288+
.copied()
289+
.flat_map(|sp| {
290+
sp.macro_backtrace().filter_map(|expn_data| {
291+
match expn_data.kind {
292+
ExpnKind::Root => None,
293+
294+
// Skip past non-macro entries, just in case there
295+
// are some which do actually involve macros.
296+
ExpnKind::Desugaring(..) | ExpnKind::AstPass(..) => None,
297+
298+
ExpnKind::Macro(macro_kind, _) => Some(macro_kind),
299+
}
300+
})
301+
})
302+
.next();
303+
283304
if !backtrace {
284-
external_spans_updated =
285-
self.fix_multispans_in_extern_macros(source_map, span, children);
305+
self.fix_multispans_in_extern_macros(source_map, span, children);
286306
}
287307

288308
self.render_multispans_macro_backtrace(span, children, backtrace);
289309

290310
if !backtrace {
291-
if external_spans_updated {
311+
if let Some(macro_kind) = has_macro_spans {
292312
let msg = format!(
293-
"this {} originates in a macro outside of the current crate \
313+
"this {} originates in {} {} \
294314
(in Nightly builds, run with -Z macro-backtrace for more info)",
295315
level,
316+
macro_kind.article(),
317+
macro_kind.descr(),
296318
);
297319

298320
children.push(SubDiagnostic {
@@ -311,9 +333,8 @@ pub trait Emitter {
311333
children: &mut Vec<SubDiagnostic>,
312334
backtrace: bool,
313335
) {
314-
self.render_multispan_macro_backtrace(span, backtrace);
315-
for child in children.iter_mut() {
316-
self.render_multispan_macro_backtrace(&mut child.span, backtrace);
336+
for span in iter::once(span).chain(children.iter_mut().map(|child| &mut child.span)) {
337+
self.render_multispan_macro_backtrace(span, backtrace);
317338
}
318339
}
319340

@@ -386,6 +407,7 @@ pub trait Emitter {
386407
}
387408
}
388409
}
410+
389411
for (label_span, label_text) in new_labels {
390412
span.push_span_label(label_span, label_text);
391413
}
@@ -399,12 +421,10 @@ pub trait Emitter {
399421
source_map: &Option<Lrc<SourceMap>>,
400422
span: &mut MultiSpan,
401423
children: &mut Vec<SubDiagnostic>,
402-
) -> bool {
403-
let mut spans_updated = self.fix_multispan_in_extern_macros(source_map, span);
404-
for child in children.iter_mut() {
405-
spans_updated |= self.fix_multispan_in_extern_macros(source_map, &mut child.span);
424+
) {
425+
for span in iter::once(span).chain(children.iter_mut().map(|child| &mut child.span)) {
426+
self.fix_multispan_in_extern_macros(source_map, span);
406427
}
407-
spans_updated
408428
}
409429

410430
// This "fixes" MultiSpans that contain Spans that are pointing to locations inside of
@@ -414,10 +434,10 @@ pub trait Emitter {
414434
&self,
415435
source_map: &Option<Lrc<SourceMap>>,
416436
span: &mut MultiSpan,
417-
) -> bool {
437+
) {
418438
let sm = match source_map {
419439
Some(ref sm) => sm,
420-
None => return false,
440+
None => return,
421441
};
422442

423443
// First, find all the spans in <*macros> and point instead at their use site
@@ -438,12 +458,9 @@ pub trait Emitter {
438458
.collect();
439459

440460
// After we have them, make sure we replace these 'bad' def sites with their use sites
441-
let spans_updated = !replacements.is_empty();
442461
for (from, to) in replacements {
443462
span.replace(from, to);
444463
}
445-
446-
spans_updated
447464
}
448465
}
449466

src/test/rustdoc-ui/intra-links-warning.stderr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,4 +175,5 @@ LL | f!("Foo\nbar [BarF] bar\nbaz");
175175
bar [BarF] bar
176176
^^^^
177177
= help: to escape `[` and `]` characters, just add '\' before them like `\[` or `\]`
178+
= note: this warning originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
178179

src/test/ui-fulldeps/hash-stable-is-unstable.stderr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ LL | #[derive(HashStable)]
4242
|
4343
= note: for more information, see https://github.com/rust-lang/rust/issues/27812
4444
= help: add `#![feature(rustc_private)]` to the crate attributes to enable
45+
= note: this error originates in a derive macro (in Nightly builds, run with -Z macro-backtrace for more info)
4546

4647
error: aborting due to 5 previous errors
4748

src/test/ui-fulldeps/internal-lints/lint_pass_impl_without_macro.stderr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ LL | custom_lint_pass_macro!();
2121
| -------------------------- in this macro invocation
2222
|
2323
= help: try using `declare_lint_pass!` or `impl_lint_pass!` instead
24+
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
2425

2526
error: aborting due to 2 previous errors
2627

src/test/ui/allocator/not-an-allocator.stderr

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ LL | static A: usize = 0;
55
| ^^^^^^^^^^^^^^^^^^^^ the trait `std::alloc::GlobalAlloc` is not implemented for `usize`
66
|
77
= note: required by `std::alloc::GlobalAlloc::alloc`
8+
= note: this error originates in an attribute macro (in Nightly builds, run with -Z macro-backtrace for more info)
89

910
error[E0277]: the trait bound `usize: std::alloc::GlobalAlloc` is not satisfied
1011
--> $DIR/not-an-allocator.rs:2:1
@@ -13,6 +14,7 @@ LL | static A: usize = 0;
1314
| ^^^^^^^^^^^^^^^^^^^^ the trait `std::alloc::GlobalAlloc` is not implemented for `usize`
1415
|
1516
= note: required by `std::alloc::GlobalAlloc::dealloc`
17+
= note: this error originates in an attribute macro (in Nightly builds, run with -Z macro-backtrace for more info)
1618

1719
error[E0277]: the trait bound `usize: std::alloc::GlobalAlloc` is not satisfied
1820
--> $DIR/not-an-allocator.rs:2:1
@@ -21,6 +23,7 @@ LL | static A: usize = 0;
2123
| ^^^^^^^^^^^^^^^^^^^^ the trait `std::alloc::GlobalAlloc` is not implemented for `usize`
2224
|
2325
= note: required by `std::alloc::GlobalAlloc::realloc`
26+
= note: this error originates in an attribute macro (in Nightly builds, run with -Z macro-backtrace for more info)
2427

2528
error[E0277]: the trait bound `usize: std::alloc::GlobalAlloc` is not satisfied
2629
--> $DIR/not-an-allocator.rs:2:1
@@ -29,6 +32,7 @@ LL | static A: usize = 0;
2932
| ^^^^^^^^^^^^^^^^^^^^ the trait `std::alloc::GlobalAlloc` is not implemented for `usize`
3033
|
3134
= note: required by `std::alloc::GlobalAlloc::alloc_zeroed`
35+
= note: this error originates in an attribute macro (in Nightly builds, run with -Z macro-backtrace for more info)
3236

3337
error: aborting due to 4 previous errors
3438

src/test/ui/allocator/two-allocators.stderr

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ LL | static A: System = System;
66
LL | #[global_allocator]
77
LL | static B: System = System;
88
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot define a new global allocator
9+
|
10+
= note: this error originates in an attribute macro (in Nightly builds, run with -Z macro-backtrace for more info)
911

1012
error: aborting due to previous error
1113

src/test/ui/borrowck/borrowck-borrowed-uniq-rvalue-2.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ LL | x.x[0];
99
| ------ borrow later used here
1010
|
1111
= note: consider using a `let` binding to create a longer lived value
12-
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z macro-backtrace for more info)
12+
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
1313

1414
error: aborting due to previous error
1515

src/test/ui/borrowck/issue-64453.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ LL | static settings_dir: String = format!("");
66
|
77
= note: for more information, see https://github.com/rust-lang/rust/issues/49146
88
= help: add `#![feature(const_if_match)]` to the crate attributes to enable
9-
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z macro-backtrace for more info)
9+
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
1010

1111
error: aborting due to previous error
1212

src/test/ui/borrowck/move-error-snippets.stderr

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ LL | aaa!(D);
99
...
1010
LL | sss!();
1111
| ------- in this macro invocation
12+
|
13+
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
1214

1315
error: aborting due to previous error
1416

src/test/ui/codemap_tests/bad-format-args.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error: requires at least a format string argument
44
LL | format!();
55
| ^^^^^^^^^^
66
|
7-
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z macro-backtrace for more info)
7+
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
88

99
error: expected token: `,`
1010
--> $DIR/bad-format-args.rs:3:16

0 commit comments

Comments
 (0)