2020-11-05 21:36:30 +00:00
|
|
|
PRINT-BANG INPUT (DISPLAY): struct S;
|
|
|
|
PRINT-BANG RE-COLLECTED (DISPLAY): struct S ;
|
|
|
|
PRINT-BANG INPUT (DEBUG): TokenStream [
|
|
|
|
Group {
|
|
|
|
delimiter: None,
|
|
|
|
stream: TokenStream [
|
|
|
|
Ident {
|
|
|
|
ident: "struct",
|
|
|
|
span: $DIR/nonterminal-token-hygiene.rs:30:5: 30:11 (#5),
|
|
|
|
},
|
|
|
|
Ident {
|
|
|
|
ident: "S",
|
|
|
|
span: $DIR/nonterminal-token-hygiene.rs:30:12: 30:13 (#5),
|
|
|
|
},
|
|
|
|
Punct {
|
|
|
|
ch: ';',
|
|
|
|
spacing: Alone,
|
|
|
|
span: $DIR/nonterminal-token-hygiene.rs:30:13: 30:14 (#5),
|
|
|
|
},
|
|
|
|
],
|
|
|
|
span: $DIR/nonterminal-token-hygiene.rs:20:27: 20:32 (#6),
|
|
|
|
},
|
|
|
|
]
|
|
|
|
#![feature /* 0#0 */(prelude_import)]
|
|
|
|
#![no_std /* 0#0 */]
|
|
|
|
// Make sure that marks from declarative macros are applied to tokens in nonterminal.
|
|
|
|
|
|
|
|
// check-pass
|
|
|
|
// compile-flags: -Z span-debug -Z macro-backtrace -Z unpretty=expanded,hygiene
|
|
|
|
// compile-flags: -Z trim-diagnostic-paths=no
|
|
|
|
// normalize-stdout-test "\d+#" -> "0#"
|
|
|
|
// aux-build:test-macros.rs
|
|
|
|
|
|
|
|
#![feature /* 0#0 */(decl_macro)]
|
|
|
|
|
|
|
|
#![no_std /* 0#0 */]
|
|
|
|
#[prelude_import /* 0#1 */]
|
2021-02-17 14:52:31 +00:00
|
|
|
use ::core /* 0#1 */::prelude /* 0#1 */::rust_2015 /* 0#1 */::*;
|
2020-11-05 21:36:30 +00:00
|
|
|
#[macro_use /* 0#1 */]
|
|
|
|
extern crate core /* 0#2 */;
|
|
|
|
#[macro_use /* 0#1 */]
|
|
|
|
extern crate compiler_builtins /* 0#2 */;
|
|
|
|
// Don't load unnecessary hygiene information from std
|
|
|
|
extern crate std /* 0#0 */;
|
|
|
|
|
|
|
|
#[macro_use /* 0#0 */]
|
|
|
|
extern crate test_macros /* 0#0 */;
|
|
|
|
|
|
|
|
macro_rules! outer
|
|
|
|
/*
|
|
|
|
0#0
|
|
|
|
*/ {
|
|
|
|
($ item : item) =>
|
|
|
|
{
|
2021-05-01 19:54:47 +00:00
|
|
|
macro inner() { print_bang! { $ item } } inner! () ;
|
2020-11-05 21:36:30 +00:00
|
|
|
|
|
|
|
} ;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct S /* 0#0 */;
|
2021-05-01 19:54:47 +00:00
|
|
|
macro inner /* 0#4 */ { () => { print_bang! { struct S; } } }
|
2020-11-05 21:36:30 +00:00
|
|
|
|
|
|
|
struct S /* 0#5 */;
|
|
|
|
// OK, not a duplicate definition of `S`
|
|
|
|
|
|
|
|
fn main /* 0#0 */() { }
|
|
|
|
|
|
|
|
/*
|
|
|
|
Expansions:
|
|
|
|
0: parent: ExpnId(0), call_site_ctxt: #0, def_site_ctxt: #0, kind: Root
|
|
|
|
1: parent: ExpnId(0), call_site_ctxt: #0, def_site_ctxt: #0, kind: AstPass(StdImports)
|
Implement span quoting for proc-macros
This PR implements span quoting, allowing proc-macros to produce spans
pointing *into their own crate*. This is used by the unstable
`proc_macro::quote!` macro, allowing us to get error messages like this:
```
error[E0412]: cannot find type `MissingType` in this scope
--> $DIR/auxiliary/span-from-proc-macro.rs:37:20
|
LL | pub fn error_from_attribute(_args: TokenStream, _input: TokenStream) -> TokenStream {
| ----------------------------------------------------------------------------------- in this expansion of procedural macro `#[error_from_attribute]`
...
LL | field: MissingType
| ^^^^^^^^^^^ not found in this scope
|
::: $DIR/span-from-proc-macro.rs:8:1
|
LL | #[error_from_attribute]
| ----------------------- in this macro invocation
```
Here, `MissingType` occurs inside the implementation of the proc-macro
`#[error_from_attribute]`. Previosuly, this would always result in a
span pointing at `#[error_from_attribute]`
This will make many proc-macro-related error message much more useful -
when a proc-macro generates code containing an error, users will get an
error message pointing directly at that code (within the macro
definition), instead of always getting a span pointing at the macro
invocation site.
This is implemented as follows:
* When a proc-macro crate is being *compiled*, it causes the `quote!`
macro to get run. This saves all of the sapns in the input to `quote!`
into the metadata of *the proc-macro-crate* (which we are currently
compiling). The `quote!` macro then expands to a call to
`proc_macro::Span::recover_proc_macro_span(id)`, where `id` is an
opaque identifier for the span in the crate metadata.
* When the same proc-macro crate is *run* (e.g. it is loaded from disk
and invoked by some consumer crate), the call to
`proc_macro::Span::recover_proc_macro_span` causes us to load the span
from the proc-macro crate's metadata. The proc-macro then produces a
`TokenStream` containing a `Span` pointing into the proc-macro crate
itself.
The recursive nature of 'quote!' can be difficult to understand at
first. The file `src/test/ui/proc-macro/quote-debug.stdout` shows
the output of the `quote!` macro, which should make this eaier to
understand.
This PR also supports custom quoting spans in custom quote macros (e.g.
the `quote` crate). All span quoting goes through the
`proc_macro::quote_span` method, which can be called by a custom quote
macro to perform span quoting. An example of this usage is provided in
`src/test/ui/proc-macro/auxiliary/custom-quote.rs`
Custom quoting currently has a few limitations:
In order to quote a span, we need to generate a call to
`proc_macro::Span::recover_proc_macro_span`. However, proc-macros
support renaming the `proc_macro` crate, so we can't simply hardcode
this path. Previously, the `quote_span` method used the path
`crate::Span` - however, this only works when it is called by the
builtin `quote!` macro in the same crate. To support being called from
arbitrary crates, we need access to the name of the `proc_macro` crate
to generate a path. This PR adds an additional argument to `quote_span`
to specify the name of the `proc_macro` crate. Howver, this feels kind
of hacky, and we may want to change this before stabilizing anything
quote-related.
Additionally, using `quote_span` currently requires enabling the
`proc_macro_internals` feature. The builtin `quote!` macro
has an `#[allow_internal_unstable]` attribute, but this won't work for
custom quote implementations. This will likely require some additional
tricks to apply `allow_internal_unstable` to the span of
`proc_macro::Span::recover_proc_macro_span`.
2020-08-02 23:52:16 +00:00
|
|
|
2: parent: ExpnId(0), call_site_ctxt: #0, def_site_ctxt: #0, kind: Macro { kind: Bang, name: "outer", proc_macro: false }
|
2020-11-05 21:36:30 +00:00
|
|
|
3: parent: ExpnId(0), call_site_ctxt: #0, def_site_ctxt: #0, kind: AstPass(StdImports)
|
Implement span quoting for proc-macros
This PR implements span quoting, allowing proc-macros to produce spans
pointing *into their own crate*. This is used by the unstable
`proc_macro::quote!` macro, allowing us to get error messages like this:
```
error[E0412]: cannot find type `MissingType` in this scope
--> $DIR/auxiliary/span-from-proc-macro.rs:37:20
|
LL | pub fn error_from_attribute(_args: TokenStream, _input: TokenStream) -> TokenStream {
| ----------------------------------------------------------------------------------- in this expansion of procedural macro `#[error_from_attribute]`
...
LL | field: MissingType
| ^^^^^^^^^^^ not found in this scope
|
::: $DIR/span-from-proc-macro.rs:8:1
|
LL | #[error_from_attribute]
| ----------------------- in this macro invocation
```
Here, `MissingType` occurs inside the implementation of the proc-macro
`#[error_from_attribute]`. Previosuly, this would always result in a
span pointing at `#[error_from_attribute]`
This will make many proc-macro-related error message much more useful -
when a proc-macro generates code containing an error, users will get an
error message pointing directly at that code (within the macro
definition), instead of always getting a span pointing at the macro
invocation site.
This is implemented as follows:
* When a proc-macro crate is being *compiled*, it causes the `quote!`
macro to get run. This saves all of the sapns in the input to `quote!`
into the metadata of *the proc-macro-crate* (which we are currently
compiling). The `quote!` macro then expands to a call to
`proc_macro::Span::recover_proc_macro_span(id)`, where `id` is an
opaque identifier for the span in the crate metadata.
* When the same proc-macro crate is *run* (e.g. it is loaded from disk
and invoked by some consumer crate), the call to
`proc_macro::Span::recover_proc_macro_span` causes us to load the span
from the proc-macro crate's metadata. The proc-macro then produces a
`TokenStream` containing a `Span` pointing into the proc-macro crate
itself.
The recursive nature of 'quote!' can be difficult to understand at
first. The file `src/test/ui/proc-macro/quote-debug.stdout` shows
the output of the `quote!` macro, which should make this eaier to
understand.
This PR also supports custom quoting spans in custom quote macros (e.g.
the `quote` crate). All span quoting goes through the
`proc_macro::quote_span` method, which can be called by a custom quote
macro to perform span quoting. An example of this usage is provided in
`src/test/ui/proc-macro/auxiliary/custom-quote.rs`
Custom quoting currently has a few limitations:
In order to quote a span, we need to generate a call to
`proc_macro::Span::recover_proc_macro_span`. However, proc-macros
support renaming the `proc_macro` crate, so we can't simply hardcode
this path. Previously, the `quote_span` method used the path
`crate::Span` - however, this only works when it is called by the
builtin `quote!` macro in the same crate. To support being called from
arbitrary crates, we need access to the name of the `proc_macro` crate
to generate a path. This PR adds an additional argument to `quote_span`
to specify the name of the `proc_macro` crate. Howver, this feels kind
of hacky, and we may want to change this before stabilizing anything
quote-related.
Additionally, using `quote_span` currently requires enabling the
`proc_macro_internals` feature. The builtin `quote!` macro
has an `#[allow_internal_unstable]` attribute, but this won't work for
custom quote implementations. This will likely require some additional
tricks to apply `allow_internal_unstable` to the span of
`proc_macro::Span::recover_proc_macro_span`.
2020-08-02 23:52:16 +00:00
|
|
|
4: parent: ExpnId(2), call_site_ctxt: #4, def_site_ctxt: #4, kind: Macro { kind: Bang, name: "inner", proc_macro: false }
|
|
|
|
5: parent: ExpnId(4), call_site_ctxt: #6, def_site_ctxt: #0, kind: Macro { kind: Bang, name: "print_bang", proc_macro: true }
|
2020-11-05 21:36:30 +00:00
|
|
|
|
|
|
|
SyntaxContexts:
|
|
|
|
#0: parent: #0, outer_mark: (ExpnId(0), Opaque)
|
|
|
|
#1: parent: #0, outer_mark: (ExpnId(1), Opaque)
|
|
|
|
#2: parent: #0, outer_mark: (ExpnId(1), Transparent)
|
|
|
|
#3: parent: #0, outer_mark: (ExpnId(3), Opaque)
|
|
|
|
#4: parent: #0, outer_mark: (ExpnId(2), SemiTransparent)
|
|
|
|
#5: parent: #0, outer_mark: (ExpnId(4), Opaque)
|
|
|
|
#6: parent: #4, outer_mark: (ExpnId(4), Opaque)
|
|
|
|
#7: parent: #0, outer_mark: (ExpnId(5), Opaque)
|
|
|
|
#8: parent: #6, outer_mark: (ExpnId(5), Transparent)
|
|
|
|
#9: parent: #5, outer_mark: (ExpnId(5), SemiTransparent)
|
|
|
|
*/
|