Skip to content

Commit

Permalink
rustc_macros: don't limit the -Zmacro-backtrace suggestion to extern …
Browse files Browse the repository at this point in the history
…macros.
  • Loading branch information
eddyb committed Feb 6, 2020
1 parent ab08097 commit 4c7eb59
Show file tree
Hide file tree
Showing 280 changed files with 888 additions and 119 deletions.
53 changes: 35 additions & 18 deletions src/librustc_errors/emitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ use std::borrow::Cow;
use std::cmp::{max, min, Reverse};
use std::io;
use std::io::prelude::*;
use std::iter;
use std::path::Path;
use termcolor::{Ansi, BufferWriter, ColorChoice, ColorSpec, StandardStream};
use termcolor::{Buffer, Color, WriteColor};
Expand Down Expand Up @@ -279,20 +280,41 @@ pub trait Emitter {
level: &Level,
backtrace: bool,
) {
let mut external_spans_updated = false;
// Check for spans in macros, before `fix_multispans_in_extern_macros`
// has a chance to replace them.
let has_macro_spans = iter::once(&*span)
.chain(children.iter().map(|child| &child.span))
.flat_map(|span| span.primary_spans())
.copied()
.flat_map(|sp| {
sp.macro_backtrace().filter_map(|expn_data| {
match expn_data.kind {
ExpnKind::Root => None,

// Skip past non-macro entries, just in case there
// are some which do actually involve macros.
ExpnKind::Desugaring(..) | ExpnKind::AstPass(..) => None,

ExpnKind::Macro(macro_kind, _) => Some(macro_kind),
}
})
})
.next();

if !backtrace {
external_spans_updated =
self.fix_multispans_in_extern_macros(source_map, span, children);
self.fix_multispans_in_extern_macros(source_map, span, children);
}

self.render_multispans_macro_backtrace(span, children, backtrace);

if !backtrace {
if external_spans_updated {
if let Some(macro_kind) = has_macro_spans {
let msg = format!(
"this {} originates in a macro outside of the current crate \
"this {} originates in {} {} \
(in Nightly builds, run with -Z macro-backtrace for more info)",
level,
macro_kind.article(),
macro_kind.descr(),
);

children.push(SubDiagnostic {
Expand All @@ -311,9 +333,8 @@ pub trait Emitter {
children: &mut Vec<SubDiagnostic>,
backtrace: bool,
) {
self.render_multispan_macro_backtrace(span, backtrace);
for child in children.iter_mut() {
self.render_multispan_macro_backtrace(&mut child.span, backtrace);
for span in iter::once(span).chain(children.iter_mut().map(|child| &mut child.span)) {
self.render_multispan_macro_backtrace(span, backtrace);
}
}

Expand Down Expand Up @@ -386,6 +407,7 @@ pub trait Emitter {
}
}
}

for (label_span, label_text) in new_labels {
span.push_span_label(label_span, label_text);
}
Expand All @@ -399,12 +421,10 @@ pub trait Emitter {
source_map: &Option<Lrc<SourceMap>>,
span: &mut MultiSpan,
children: &mut Vec<SubDiagnostic>,
) -> bool {
let mut spans_updated = self.fix_multispan_in_extern_macros(source_map, span);
for child in children.iter_mut() {
spans_updated |= self.fix_multispan_in_extern_macros(source_map, &mut child.span);
) {
for span in iter::once(span).chain(children.iter_mut().map(|child| &mut child.span)) {
self.fix_multispan_in_extern_macros(source_map, span);
}
spans_updated
}

// This "fixes" MultiSpans that contain Spans that are pointing to locations inside of
Expand All @@ -414,10 +434,10 @@ pub trait Emitter {
&self,
source_map: &Option<Lrc<SourceMap>>,
span: &mut MultiSpan,
) -> bool {
) {
let sm = match source_map {
Some(ref sm) => sm,
None => return false,
None => return,
};

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

// After we have them, make sure we replace these 'bad' def sites with their use sites
let spans_updated = !replacements.is_empty();
for (from, to) in replacements {
span.replace(from, to);
}

spans_updated
}
}

Expand Down
1 change: 1 addition & 0 deletions src/test/rustdoc-ui/intra-links-warning.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -175,4 +175,5 @@ LL | f!("Foo\nbar [BarF] bar\nbaz");
bar [BarF] bar
^^^^
= help: to escape `[` and `]` characters, just add '\' before them like `\[` or `\]`
= note: this warning originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)

1 change: 1 addition & 0 deletions src/test/ui-fulldeps/hash-stable-is-unstable.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ LL | #[derive(HashStable)]
|
= note: for more information, see https://github.com/rust-lang/rust/issues/27812
= help: add `#![feature(rustc_private)]` to the crate attributes to enable
= note: this error originates in a derive macro (in Nightly builds, run with -Z macro-backtrace for more info)

error: aborting due to 5 previous errors

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ LL | custom_lint_pass_macro!();
| -------------------------- in this macro invocation
|
= help: try using `declare_lint_pass!` or `impl_lint_pass!` instead
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)

error: aborting due to 2 previous errors

4 changes: 4 additions & 0 deletions src/test/ui/allocator/not-an-allocator.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ LL | static A: usize = 0;
| ^^^^^^^^^^^^^^^^^^^^ the trait `std::alloc::GlobalAlloc` is not implemented for `usize`
|
= note: required by `std::alloc::GlobalAlloc::alloc`
= note: this error originates in an attribute macro (in Nightly builds, run with -Z macro-backtrace for more info)

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

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

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

error: aborting due to 4 previous errors

Expand Down
2 changes: 2 additions & 0 deletions src/test/ui/allocator/two-allocators.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ LL | static A: System = System;
LL | #[global_allocator]
LL | static B: System = System;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot define a new global allocator
|
= note: this error originates in an attribute macro (in Nightly builds, run with -Z macro-backtrace for more info)

error: aborting due to previous error

Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ LL | x.x[0];
| ------ borrow later used here
|
= note: consider using a `let` binding to create a longer lived value
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z macro-backtrace for more info)
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)

error: aborting due to previous error

Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/borrowck/issue-64453.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ LL | static settings_dir: String = format!("");
|
= note: for more information, see https://github.com/rust-lang/rust/issues/49146
= help: add `#![feature(const_if_match)]` to the crate attributes to enable
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z macro-backtrace for more info)
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)

error: aborting due to previous error

Expand Down
2 changes: 2 additions & 0 deletions src/test/ui/borrowck/move-error-snippets.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ LL | aaa!(D);
...
LL | sss!();
| ------- in this macro invocation
|
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)

error: aborting due to previous error

Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/codemap_tests/bad-format-args.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ error: requires at least a format string argument
LL | format!();
| ^^^^^^^^^^
|
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z macro-backtrace for more info)
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)

error: expected token: `,`
--> $DIR/bad-format-args.rs:3:16
Expand Down
2 changes: 2 additions & 0 deletions src/test/ui/codemap_tests/issue-28308.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ error[E0600]: cannot apply unary operator `!` to type `&'static str`
|
LL | assert!("foo");
| ^^^^^^^^^^^^^^^ cannot apply unary operator `!`
|
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)

error: aborting due to previous error

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ LL | #[cfg(feature = $expr)]
...
LL | generate_s10!(concat!("nonexistent"));
| -------------------------------------- in this macro invocation
|
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)

error: aborting due to 10 previous errors

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ LL | #[cfg_attr(all(), unknown)]
...
LL | foo!();
| ------- in this macro invocation
|
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)

error: aborting due to previous error

Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ LL | println!("{:?}", [0_usize; 33]);
|
= note: required because of the requirements on the impl of `std::fmt::Debug` for `[usize; 33]`
= note: required by `std::fmt::Debug::fmt`
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0277]: arrays only have std trait implementations for lengths 0..=32
--> $DIR/core-traits-no-impls-length-33.rs:10:16
Expand Down
1 change: 1 addition & 0 deletions src/test/ui/const-generics/broken-mir-2.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ LL | struct S<T: Debug, const N: usize>([T; N]);
= note: required because of the requirements on the impl of `std::fmt::Debug` for `[T; _]`
= note: required because of the requirements on the impl of `std::fmt::Debug` for `&[T; _]`
= note: required for the cast to the object type `dyn std::fmt::Debug`
= note: this error originates in a derive macro (in Nightly builds, run with -Z macro-backtrace for more info)

error: aborting due to previous error

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ LL | a: [u32; N],
= note: required because of the requirements on the impl of `std::fmt::Debug` for `[u32; _]`
= note: required because of the requirements on the impl of `std::fmt::Debug` for `&[u32; _]`
= note: required for the cast to the object type `dyn std::fmt::Debug`
= note: this error originates in a derive macro (in Nightly builds, run with -Z macro-backtrace for more info)

error: aborting due to previous error

Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/consts/const-eval/const_fn_ptr_fail2.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ LL | assert_eq!(Y, 4);
| |
| referenced constant has errors
|
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z macro-backtrace for more info)
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0080]: evaluation of constant expression failed
--> $DIR/const_fn_ptr_fail2.rs:22:5
Expand All @@ -22,7 +22,7 @@ LL | assert_eq!(Z, 4);
| |
| referenced constant has errors
|
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z macro-backtrace for more info)
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)

error: aborting due to 2 previous errors

Expand Down
6 changes: 3 additions & 3 deletions src/test/ui/consts/const-eval/const_panic.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ LL | pub const Z: () = panic!("cheese");
| the evaluated program panicked at 'cheese', $DIR/const_panic.rs:4:19
|
= note: `#[deny(const_err)]` on by default
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z macro-backtrace for more info)
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)

error: any use of this value will cause an error
--> $DIR/const_panic.rs:7:19
Expand All @@ -17,7 +17,7 @@ LL | pub const Y: () = unreachable!();
| |
| the evaluated program panicked at 'internal error: entered unreachable code', $DIR/const_panic.rs:7:19
|
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z macro-backtrace for more info)
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)

error: any use of this value will cause an error
--> $DIR/const_panic.rs:10:19
Expand All @@ -27,7 +27,7 @@ LL | pub const X: () = unimplemented!();
| |
| the evaluated program panicked at 'not implemented', $DIR/const_panic.rs:10:19
|
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z macro-backtrace for more info)
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)

error: aborting due to 3 previous errors

6 changes: 3 additions & 3 deletions src/test/ui/consts/const-eval/const_panic_libcore.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ LL | const Z: () = panic!("cheese");
| the evaluated program panicked at 'cheese', $DIR/const_panic_libcore.rs:5:15
|
= note: `#[deny(const_err)]` on by default
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z macro-backtrace for more info)
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)

error: any use of this value will cause an error
--> $DIR/const_panic_libcore.rs:8:15
Expand All @@ -17,7 +17,7 @@ LL | const Y: () = unreachable!();
| |
| the evaluated program panicked at 'internal error: entered unreachable code', $DIR/const_panic_libcore.rs:8:15
|
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z macro-backtrace for more info)
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)

error: any use of this value will cause an error
--> $DIR/const_panic_libcore.rs:11:15
Expand All @@ -27,7 +27,7 @@ LL | const X: () = unimplemented!();
| |
| the evaluated program panicked at 'not implemented', $DIR/const_panic_libcore.rs:11:15
|
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z macro-backtrace for more info)
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)

error: aborting due to 3 previous errors

6 changes: 3 additions & 3 deletions src/test/ui/consts/const-eval/const_panic_libcore_main.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ LL | const Z: () = panic!("cheese");
| the evaluated program panicked at 'cheese', $DIR/const_panic_libcore_main.rs:9:15
|
= note: `#[deny(const_err)]` on by default
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z macro-backtrace for more info)
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)

error: any use of this value will cause an error
--> $DIR/const_panic_libcore_main.rs:12:15
Expand All @@ -17,7 +17,7 @@ LL | const Y: () = unreachable!();
| |
| the evaluated program panicked at 'internal error: entered unreachable code', $DIR/const_panic_libcore_main.rs:12:15
|
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z macro-backtrace for more info)
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)

error: any use of this value will cause an error
--> $DIR/const_panic_libcore_main.rs:15:15
Expand All @@ -27,7 +27,7 @@ LL | const X: () = unimplemented!();
| |
| the evaluated program panicked at 'not implemented', $DIR/const_panic_libcore_main.rs:15:15
|
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z macro-backtrace for more info)
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)

error: aborting due to 3 previous errors

6 changes: 3 additions & 3 deletions src/test/ui/consts/const-eval/feature-gate-const_panic.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ LL | const Z: () = panic!("cheese");
|
= note: for more information, see https://github.com/rust-lang/rust/issues/51999
= help: add `#![feature(const_panic)]` to the crate attributes to enable
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z macro-backtrace for more info)
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0658]: panicking in constants is unstable
--> $DIR/feature-gate-const_panic.rs:9:15
Expand All @@ -16,7 +16,7 @@ LL | const X: () = unimplemented!();
|
= note: for more information, see https://github.com/rust-lang/rust/issues/51999
= help: add `#![feature(const_panic)]` to the crate attributes to enable
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z macro-backtrace for more info)
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0658]: panicking in constants is unstable
--> $DIR/feature-gate-const_panic.rs:6:15
Expand All @@ -26,7 +26,7 @@ LL | const Y: () = unreachable!();
|
= note: for more information, see https://github.com/rust-lang/rust/issues/51999
= help: add `#![feature(const_panic)]` to the crate attributes to enable
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z macro-backtrace for more info)
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)

error: aborting due to 3 previous errors

Expand Down
Loading

0 comments on commit 4c7eb59

Please sign in to comment.