2019-07-18 23:29:57 +03:00
|
|
|
//! This crate contains implementations of built-in macros and other code generating facilities
|
|
|
|
//! injecting code into the crate before it is lowered to HIR.
|
2015-12-10 23:23:14 +09:00
|
|
|
|
2020-09-23 21:51:56 +02:00
|
|
|
#![doc(html_root_url = "https://doc.rust-lang.org/nightly/nightly-rustc/")]
|
2021-01-29 08:31:08 +01:00
|
|
|
#![feature(box_patterns)]
|
|
|
|
#![feature(box_syntax)]
|
2019-10-08 01:14:42 +01:00
|
|
|
#![feature(bool_to_option)]
|
2019-06-20 11:52:31 +03:00
|
|
|
#![feature(crate_visibility_modifier)]
|
|
|
|
#![feature(decl_macro)]
|
2021-03-08 15:32:41 -08:00
|
|
|
#![feature(iter_zip)]
|
2019-06-20 11:52:31 +03:00
|
|
|
#![feature(nll)]
|
2019-08-20 02:26:40 +03:00
|
|
|
#![feature(proc_macro_internals)]
|
|
|
|
#![feature(proc_macro_quote)]
|
2021-03-14 14:12:04 -04:00
|
|
|
#![recursion_limit = "256"]
|
2019-02-04 21:49:54 +09:00
|
|
|
|
2019-08-20 02:26:40 +03:00
|
|
|
extern crate proc_macro;
|
|
|
|
|
2019-06-20 11:52:31 +03:00
|
|
|
use crate::deriving::*;
|
|
|
|
|
2021-01-10 14:36:30 +03:00
|
|
|
use rustc_expand::base::{MacroExpanderFn, ResolverExpand, SyntaxExtensionKind};
|
2019-12-29 17:23:55 +03:00
|
|
|
use rustc_expand::proc_macro::BangProcMacro;
|
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 19:52:16 -04:00
|
|
|
use rustc_span::def_id::LOCAL_CRATE;
|
2021-01-10 14:36:30 +03:00
|
|
|
use rustc_span::symbol::sym;
|
2019-06-20 11:52:31 +03:00
|
|
|
|
2020-02-12 15:47:43 +00:00
|
|
|
mod asm;
|
2018-05-17 21:17:53 +02:00
|
|
|
mod assert;
|
2015-12-10 23:23:14 +09:00
|
|
|
mod cfg;
|
2020-03-10 00:56:20 +03:00
|
|
|
mod cfg_accessible;
|
2021-03-06 21:33:02 +03:00
|
|
|
mod cfg_eval;
|
2017-05-06 23:26:45 -04:00
|
|
|
mod compile_error;
|
2015-12-10 23:23:14 +09:00
|
|
|
mod concat;
|
|
|
|
mod concat_idents;
|
2020-11-14 14:47:14 +03:00
|
|
|
mod derive;
|
2019-07-18 21:02:34 +03:00
|
|
|
mod deriving;
|
2015-12-10 23:23:14 +09:00
|
|
|
mod env;
|
|
|
|
mod format;
|
2016-11-11 15:23:15 +11:00
|
|
|
mod format_foreign;
|
2019-07-19 00:24:58 +03:00
|
|
|
mod global_allocator;
|
2020-01-14 13:40:42 +00:00
|
|
|
mod llvm_asm;
|
2015-12-10 23:23:14 +09:00
|
|
|
mod log_syntax;
|
2021-01-09 16:52:06 +01:00
|
|
|
mod panic;
|
2019-07-18 01:49:10 +03:00
|
|
|
mod source_util;
|
2018-07-20 18:04:02 -07:00
|
|
|
mod test;
|
2018-09-16 12:20:47 +03:00
|
|
|
mod trace_macros;
|
2019-10-08 14:15:26 +02:00
|
|
|
mod util;
|
2015-12-10 23:23:14 +09:00
|
|
|
|
2019-09-06 23:41:54 +03:00
|
|
|
pub mod cmdline_attrs;
|
2019-07-18 23:29:57 +03:00
|
|
|
pub mod proc_macro_harness;
|
2019-07-18 22:29:07 +03:00
|
|
|
pub mod standard_library_imports;
|
2019-07-18 21:29:15 +03:00
|
|
|
pub mod test_harness;
|
2017-01-09 01:31:14 -08:00
|
|
|
|
2021-01-10 14:36:30 +03:00
|
|
|
pub fn register_builtin_macros(resolver: &mut dyn ResolverExpand) {
|
|
|
|
let mut register = |name, kind| resolver.register_builtin_macro(name, kind);
|
2019-06-20 11:52:31 +03:00
|
|
|
macro register_bang($($name:ident: $f:expr,)*) {
|
|
|
|
$(register(sym::$name, SyntaxExtensionKind::LegacyBang(Box::new($f as MacroExpanderFn)));)*
|
|
|
|
}
|
|
|
|
macro register_attr($($name:ident: $f:expr,)*) {
|
|
|
|
$(register(sym::$name, SyntaxExtensionKind::LegacyAttr(Box::new($f)));)*
|
2019-06-16 18:58:39 +03:00
|
|
|
}
|
2019-06-20 11:52:31 +03:00
|
|
|
macro register_derive($($name:ident: $f:expr,)*) {
|
|
|
|
$(register(sym::$name, SyntaxExtensionKind::LegacyDerive(Box::new(BuiltinDerive($f))));)*
|
2015-12-10 23:23:14 +09:00
|
|
|
}
|
|
|
|
|
2019-06-20 11:52:31 +03:00
|
|
|
register_bang! {
|
2020-02-12 15:47:43 +00:00
|
|
|
asm: asm::expand_asm,
|
2019-06-20 11:52:31 +03:00
|
|
|
assert: assert::expand_assert,
|
2016-09-07 23:21:59 +00:00
|
|
|
cfg: cfg::expand_cfg,
|
2019-06-20 11:52:31 +03:00
|
|
|
column: source_util::expand_column,
|
|
|
|
compile_error: compile_error::expand_compile_error,
|
2019-09-03 17:43:25 +03:00
|
|
|
concat_idents: concat_idents::expand_concat_idents,
|
|
|
|
concat: concat::expand_concat,
|
2016-09-07 23:21:59 +00:00
|
|
|
env: env::expand_env,
|
2019-06-20 11:52:31 +03:00
|
|
|
file: source_util::expand_file,
|
|
|
|
format_args_nl: format::expand_format_args_nl,
|
|
|
|
format_args: format::expand_format_args,
|
2021-04-11 20:51:28 +01:00
|
|
|
global_asm: asm::expand_global_asm,
|
2019-06-20 11:52:31 +03:00
|
|
|
include_bytes: source_util::expand_include_bytes,
|
|
|
|
include_str: source_util::expand_include_str,
|
|
|
|
include: source_util::expand_include,
|
|
|
|
line: source_util::expand_line,
|
2020-01-14 13:40:42 +00:00
|
|
|
llvm_asm: llvm_asm::expand_llvm_asm,
|
2019-09-03 17:43:25 +03:00
|
|
|
log_syntax: log_syntax::expand_log_syntax,
|
2019-06-20 11:52:31 +03:00
|
|
|
module_path: source_util::expand_mod,
|
|
|
|
option_env: env::expand_option_env,
|
2021-01-09 16:52:06 +01:00
|
|
|
core_panic: panic::expand_panic,
|
|
|
|
std_panic: panic::expand_panic,
|
2019-06-20 11:52:31 +03:00
|
|
|
stringify: source_util::expand_stringify,
|
2019-06-22 16:18:05 +03:00
|
|
|
trace_macros: trace_macros::expand_trace_macros,
|
2019-06-16 18:58:39 +03:00
|
|
|
}
|
2018-07-20 18:04:02 -07:00
|
|
|
|
2019-06-20 11:52:31 +03:00
|
|
|
register_attr! {
|
|
|
|
bench: test::expand_bench,
|
2020-03-10 00:56:20 +03:00
|
|
|
cfg_accessible: cfg_accessible::Expander,
|
2021-03-06 21:33:02 +03:00
|
|
|
cfg_eval: cfg_eval::expand,
|
2020-11-14 14:47:14 +03:00
|
|
|
derive: derive::Expander,
|
2019-06-20 11:52:31 +03:00
|
|
|
global_allocator: global_allocator::expand,
|
|
|
|
test: test::expand_test,
|
2019-07-18 21:29:15 +03:00
|
|
|
test_case: test::expand_test_case,
|
2019-06-20 11:52:31 +03:00
|
|
|
}
|
2016-09-07 23:21:59 +00:00
|
|
|
|
2019-06-20 11:52:31 +03:00
|
|
|
register_derive! {
|
|
|
|
Clone: clone::expand_deriving_clone,
|
|
|
|
Copy: bounds::expand_deriving_copy,
|
|
|
|
Debug: debug::expand_deriving_debug,
|
|
|
|
Default: default::expand_deriving_default,
|
|
|
|
Eq: eq::expand_deriving_eq,
|
|
|
|
Hash: hash::expand_deriving_hash,
|
|
|
|
Ord: ord::expand_deriving_ord,
|
|
|
|
PartialEq: partial_eq::expand_deriving_partial_eq,
|
|
|
|
PartialOrd: partial_ord::expand_deriving_partial_ord,
|
|
|
|
RustcDecodable: decodable::expand_deriving_rustc_decodable,
|
|
|
|
RustcEncodable: encodable::expand_deriving_rustc_encodable,
|
2016-09-28 22:48:55 +00:00
|
|
|
}
|
2019-08-20 02:26:40 +03:00
|
|
|
|
|
|
|
let client = proc_macro::bridge::client::Client::expand1(proc_macro::quote);
|
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 19:52:16 -04:00
|
|
|
register(
|
|
|
|
sym::quote,
|
|
|
|
SyntaxExtensionKind::Bang(Box::new(BangProcMacro { client, krate: LOCAL_CRATE })),
|
|
|
|
);
|
2015-12-10 23:23:14 +09:00
|
|
|
}
|