Auto merge of #45545 - durka:macro-backtrace, r=nrc

show macro backtrace with -Z flag

Fixes #39413 by adding a facility to restore the "old school" macro expansion backtraces (previously removed in 61865384b8).

The restored functionality is accessed through the flag `-Z external-macro-backtrace`. Errors showing the truncated backtraces will suggest this flag.

### Example

Code: <details>
`a/src/lib.rs`
```rust
#[macro_export]
macro_rules! a {
    () => { a!(@) };
    (@) => { a!(@@) };
    (@@) => {
        syntax error;
    }
}
```
`b/src/main.rs`
```rust
#[macro_use] extern crate a;

macro_rules! b {
    () => { b!(@) };
    (@) => { b!(@@) };
    (@@) => {
        syntax error;
    }
}

fn main() {
    a!();
    b!();
}
```
</details>

<br/><br/>
Running without env var (note: first error is from remote macro, second from local macro):

<details>

```
$ cargo +custom run
   Compiling b v0.1.0
error: expected one of `!`, `.`, `::`, `;`, `?`, `{`, `}`, or an operator, found `error`
  --> src/main.rs:12:5
   |
12 |     a!();
   |     ^^^^^
   |     |
   |     expected one of 8 possible tokens here
   |     unexpected token
   |
   = note: this error originates in a macro outside of the current crate (run with RUST_MACRO_BACKTRACE=1 for more info)

error: expected one of `!`, `.`, `::`, `;`, `?`, `{`, `}`, or an operator, found `error`
  --> src/main.rs:7:16
   |
7  |         syntax error;
   |               -^^^^^ unexpected token
   |               |
   |               expected one of 8 possible tokens here
...
13 |     b!();
   |     ----- in this macro invocation

error: aborting due to 2 previous errors

error: Could not compile `b`.

To learn more, run the command again with --verbose.
```
</details>
The output is the same as today, except for an addition to the note which aids discoverability of the new environment variable.

<br/><br/>
Running _with_ env var:
<details>

```
$ RUST_MACRO_BACKTRACE=1 cargo +custom run
   Compiling b v0.1.0
error: expected one of `!`, `.`, `::`, `;`, `?`, `{`, `}`, or an operator, found `error`
 --> <a macros>:1:72
  |
1 | (  ) => { a ! ( @ ) } ; ( @ ) => { a ! ( @ @ ) } ; ( @ @ ) => { syntax error ;
  |                                                                       -^^^^^ unexpected token
  |                                                                       |
  |                                                                       expected one of 8 possible tokens here
src/main.rs:12:5: 12:10 note: in this expansion of a! (defined in <a macros>)
<a macros>:1:11: 1:20 note: in this expansion of a! (defined in <a macros>)
<a macros>:1:36: 1:47 note: in this expansion of a! (defined in <a macros>)

error: expected one of `!`, `.`, `::`, `;`, `?`, `{`, `}`, or an operator, found `error`
 --> src/main.rs:7:16
  |
7 |         syntax error;
  |               -^^^^^ unexpected token
  |               |
  |               expected one of 8 possible tokens here
src/main.rs:12:5: 12:10 note: in this expansion of a! (defined in <a macros>)
<a macros>:1:11: 1:20 note: in this expansion of a! (defined in <a macros>)
<a macros>:1:36: 1:47 note: in this expansion of a! (defined in <a macros>)

error: expected one of `!`, `.`, `::`, `;`, `?`, `{`, `}`, or an operator, found `error`
 --> src/main.rs:7:16
  |
7 |         syntax error;
  |               -^^^^^ unexpected token
  |               |
  |               expected one of 8 possible tokens here
src/main.rs:13:5: 13:10 note: in this expansion of b! (defined in src/main.rs)
src/main.rs:4:13: 4:18 note: in this expansion of b! (defined in src/main.rs)
src/main.rs:5:14: 5:20 note: in this expansion of b! (defined in src/main.rs)

error: aborting due to 2 previous errors

error: Could not compile `b`.

To learn more, run the command again with --verbose.
```
</details>

The output is hard to read, but better than nothing (and it's exactly what we used to have before the infamous `fix_multispans_in_std_macros`).

<br/><br/>
Wishlist:

- Save the actual source of macros in crate metadata, not just AST, so the output can be improved
    - Hopefully this would allow line numbers in the trace as well
- Show the actual macro invocations in the traces

r? @nrc
This commit is contained in:
bors 2017-11-21 06:42:14 +00:00
commit bac7c53bc3
30 changed files with 211 additions and 58 deletions

View file

@ -4,7 +4,7 @@ error: requires at least a format string argument
12 | format!();
| ^^^^^^^^^^
|
= note: this error originates in a macro outside of the current crate
= note: this error originates in a macro outside of the current crate (run with -Z external-macro-backtrace for more info)
error: expected token: `,`
--> $DIR/bad-format-args.rs:13:5
@ -12,7 +12,7 @@ error: expected token: `,`
13 | format!("" 1);
| ^^^^^^^^^^^^^^
|
= note: this error originates in a macro outside of the current crate
= note: this error originates in a macro outside of the current crate (run with -Z external-macro-backtrace for more info)
error: expected token: `,`
--> $DIR/bad-format-args.rs:14:5
@ -20,7 +20,7 @@ error: expected token: `,`
14 | format!("", 1 1);
| ^^^^^^^^^^^^^^^^^
|
= note: this error originates in a macro outside of the current crate
= note: this error originates in a macro outside of the current crate (run with -Z external-macro-backtrace for more info)
error: aborting due to 3 previous errors

View file

@ -4,7 +4,7 @@ error[E0600]: cannot apply unary operator `!` to type `&'static str`
12 | assert!("foo");
| ^^^^^^^^^^^^^^^
|
= note: this error originates in a macro outside of the current crate
= note: this error originates in a macro outside of the current crate (run with -Z external-macro-backtrace for more info)
error: aborting due to previous error

View file

@ -4,7 +4,7 @@ error: 1 positional argument in format string, but no arguments were given
16 | myprintln!("{}"); //~ ERROR in this macro
| ^^^^^^^^^^^^^^^^^
|
= note: this error originates in a macro outside of the current crate
= note: this error originates in a macro outside of the current crate (run with -Z external-macro-backtrace for more info)
error: aborting due to previous error

View file

@ -5,7 +5,7 @@ error: invalid format string: expected `'}'` but string was terminated
| ^^^^^^^^^^^^^^
|
= note: if you intended to print `{`, you can escape it using `{{`
= note: this error originates in a macro outside of the current crate
= note: this error originates in a macro outside of the current crate (run with -Z external-macro-backtrace for more info)
error: invalid format string: unmatched `}` found
--> $DIR/format-string-error.rs:14:5
@ -14,7 +14,7 @@ error: invalid format string: unmatched `}` found
| ^^^^^^^^^^^^^^
|
= note: if you intended to print `}`, you can escape it using `}}`
= note: this error originates in a macro outside of the current crate
= note: this error originates in a macro outside of the current crate (run with -Z external-macro-backtrace for more info)
error: aborting due to 2 previous errors

View file

@ -9,7 +9,7 @@ error[E0597]: borrowed value does not live long enough
| - temporary value needs to live until here
|
= note: consider using a `let` binding to increase its lifetime
= note: this error originates in a macro outside of the current crate
= note: this error originates in a macro outside of the current crate (run with -Z external-macro-backtrace for more info)
error: aborting due to previous error

View file

@ -0,0 +1,20 @@
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
// Test that the macro backtrace facility works (supporting file)
// a non-local macro
#[macro_export]
macro_rules! ping {
() => {
pong!();
}
}

View file

@ -0,0 +1,25 @@
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
// Test that the macro backtrace facility works
// aux-build:ping.rs
// compile-flags: -Z external-macro-backtrace
#[macro_use] extern crate ping;
// a local macro
macro_rules! pong {
() => { syntax error };
}
fn main() {
pong!();
ping!();
}

View file

@ -0,0 +1,21 @@
error: expected one of `!`, `.`, `::`, `;`, `?`, `{`, `}`, or an operator, found `error`
--> $DIR/main.rs:19:20
|
19 | () => { syntax error };
| -^^^^^ unexpected token
| |
| expected one of 8 possible tokens here
$DIR/main.rs:23:5: 23:13 note: in this expansion of pong! (defined in $DIR/main.rs)
error: expected one of `!`, `.`, `::`, `;`, `?`, `{`, `}`, or an operator, found `error`
--> $DIR/main.rs:19:20
|
19 | () => { syntax error };
| -^^^^^ unexpected token
| |
| expected one of 8 possible tokens here
$DIR/main.rs:24:5: 24:13 note: in this expansion of ping! (defined in <ping macros>)
<ping macros>:1:11: 1:24 note: in this expansion of pong! (defined in $DIR/main.rs)
error: aborting due to 2 previous errors

View file

@ -11,7 +11,7 @@ error: multiple unused formatting arguments
= help: `%.*3$s` should be written as `{:.2$}`
= help: `%s` should be written as `{}`
= note: printf formatting not supported; see the documentation for `std::fmt`
= note: this error originates in a macro outside of the current crate
= note: this error originates in a macro outside of the current crate (run with -Z external-macro-backtrace for more info)
error: argument never used
--> $DIR/format-foreign.rs:13:29

View file

@ -8,7 +8,7 @@ error: multiple unused formatting arguments
| | unused
| unused
|
= note: this error originates in a macro outside of the current crate
= note: this error originates in a macro outside of the current crate (run with -Z external-macro-backtrace for more info)
error: multiple unused formatting arguments
--> $DIR/format-unused-lables.rs:14:5
@ -23,7 +23,7 @@ error: multiple unused formatting arguments
18 | | );
| |______^
|
= note: this error originates in a macro outside of the current crate
= note: this error originates in a macro outside of the current crate (run with -Z external-macro-backtrace for more info)
error: named argument never used
--> $DIR/format-unused-lables.rs:20:35
@ -47,7 +47,7 @@ error: multiple unused formatting arguments
|
= help: `$STUFF` should be written as `{STUFF}`
= note: shell formatting not supported; see the documentation for `std::fmt`
= note: this error originates in a macro outside of the current crate
= note: this error originates in a macro outside of the current crate (run with -Z external-macro-backtrace for more info)
error: aborting due to 4 previous errors

View file

@ -9,7 +9,7 @@ note: lint level defined here
|
13 | #![deny(unreachable_code)]
| ^^^^^^^^^^^^^^^^
= note: this error originates in a macro outside of the current crate
= note: this error originates in a macro outside of the current crate (run with -Z external-macro-backtrace for more info)
error: aborting due to previous error

View file

@ -16,7 +16,7 @@ error: unreachable statement
36 | println!("foo");
| ^^^^^^^^^^^^^^^^
|
= note: this error originates in a macro outside of the current crate
= note: this error originates in a macro outside of the current crate (run with -Z external-macro-backtrace for more info)
error: aborting due to 2 previous errors

View file

@ -9,7 +9,7 @@ note: lint level defined here
|
14 | #![deny(unreachable_code)]
| ^^^^^^^^^^^^^^^^
= note: this error originates in a macro outside of the current crate
= note: this error originates in a macro outside of the current crate (run with -Z external-macro-backtrace for more info)
error: aborting due to previous error

View file

@ -9,7 +9,7 @@ note: lint level defined here
|
14 | #![deny(unreachable_code)]
| ^^^^^^^^^^^^^^^^
= note: this error originates in a macro outside of the current crate
= note: this error originates in a macro outside of the current crate (run with -Z external-macro-backtrace for more info)
error: unreachable statement
--> $DIR/expr_loop.rs:31:5
@ -17,7 +17,7 @@ error: unreachable statement
31 | println!("I am dead.");
| ^^^^^^^^^^^^^^^^^^^^^^^
|
= note: this error originates in a macro outside of the current crate
= note: this error originates in a macro outside of the current crate (run with -Z external-macro-backtrace for more info)
error: unreachable statement
--> $DIR/expr_loop.rs:41:5
@ -25,7 +25,7 @@ error: unreachable statement
41 | println!("I am dead.");
| ^^^^^^^^^^^^^^^^^^^^^^^
|
= note: this error originates in a macro outside of the current crate
= note: this error originates in a macro outside of the current crate (run with -Z external-macro-backtrace for more info)
error: aborting due to 3 previous errors

View file

@ -16,7 +16,7 @@ error: unreachable statement
25 | println!("I am dead");
| ^^^^^^^^^^^^^^^^^^^^^^
|
= note: this error originates in a macro outside of the current crate
= note: this error originates in a macro outside of the current crate (run with -Z external-macro-backtrace for more info)
error: unreachable statement
--> $DIR/expr_match.rs:35:5
@ -24,7 +24,7 @@ error: unreachable statement
35 | println!("I am dead");
| ^^^^^^^^^^^^^^^^^^^^^^
|
= note: this error originates in a macro outside of the current crate
= note: this error originates in a macro outside of the current crate (run with -Z external-macro-backtrace for more info)
error: aborting due to 3 previous errors

View file

@ -9,7 +9,7 @@ note: lint level defined here
|
14 | #![deny(unreachable_code)]
| ^^^^^^^^^^^^^^^^
= note: this error originates in a macro outside of the current crate
= note: this error originates in a macro outside of the current crate (run with -Z external-macro-backtrace for more info)
error: unreachable statement
--> $DIR/expr_while.rs:33:9
@ -17,7 +17,7 @@ error: unreachable statement
33 | println!("I am dead.");
| ^^^^^^^^^^^^^^^^^^^^^^^
|
= note: this error originates in a macro outside of the current crate
= note: this error originates in a macro outside of the current crate (run with -Z external-macro-backtrace for more info)
error: unreachable statement
--> $DIR/expr_while.rs:35:5
@ -25,7 +25,7 @@ error: unreachable statement
35 | println!("I am, too.");
| ^^^^^^^^^^^^^^^^^^^^^^^
|
= note: this error originates in a macro outside of the current crate
= note: this error originates in a macro outside of the current crate (run with -Z external-macro-backtrace for more info)
error: aborting due to 3 previous errors

View file

@ -56,7 +56,7 @@ error[E0308]: mismatched types
= note: expected type `&mut std::string::String`
found type `std::string::String`
= help: try with `&mut format!("foo")`
= note: this error originates in a macro outside of the current crate
= note: this error originates in a macro outside of the current crate (run with -Z external-macro-backtrace for more info)
error: aborting due to 6 previous errors

View file

@ -6,7 +6,7 @@ error[E0308]: mismatched types
|
= note: expected type `std::fmt::Arguments<'_>`
found type `std::string::String`
= note: this error originates in a macro outside of the current crate
= note: this error originates in a macro outside of the current crate (run with -Z external-macro-backtrace for more info)
error: aborting due to previous error

View file

@ -8,7 +8,7 @@ error[E0597]: `foo` does not live long enough
| | borrow occurs here
| borrowed value needs to live until here
|
= note: this error originates in a macro outside of the current crate
= note: this error originates in a macro outside of the current crate (run with -Z external-macro-backtrace for more info)
error: aborting due to previous error

View file

@ -9,7 +9,7 @@ error[E0597]: borrowed value does not live long enough
19 | }
| - temporary value needs to live until here
|
= note: this error originates in a macro outside of the current crate
= note: this error originates in a macro outside of the current crate (run with -Z external-macro-backtrace for more info)
error: aborting due to previous error

View file

@ -6,7 +6,7 @@ error[E0282]: type annotations needed
| |
| consider giving `x` a type
|
= note: this error originates in a macro outside of the current crate
= note: this error originates in a macro outside of the current crate (run with -Z external-macro-backtrace for more info)
error: aborting due to previous error

View file

@ -6,7 +6,7 @@ error[E0282]: type annotations needed
| |
| consider giving the pattern a type
|
= note: this error originates in a macro outside of the current crate
= note: this error originates in a macro outside of the current crate (run with -Z external-macro-backtrace for more info)
error: aborting due to previous error