Skip to content

Commit 29c0404

Browse files
committed
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`.
1 parent 83cdb9d commit 29c0404

File tree

3 files changed

+24
-5
lines changed

3 files changed

+24
-5
lines changed

proc_macro/src/bridge/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,8 @@ macro_rules! with_api {
162162
fn join($self: $S::Span, other: $S::Span) -> Option<$S::Span>;
163163
fn resolved_at($self: $S::Span, at: $S::Span) -> $S::Span;
164164
fn source_text($self: $S::Span) -> Option<String>;
165+
fn save_span($self: $S::Span) -> usize;
166+
fn recover_proc_macro_span(id: usize) -> $S::Span;
165167
},
166168
}
167169
};
@@ -338,6 +340,7 @@ mark_noop! {
338340
&'a [u8],
339341
&'a str,
340342
String,
343+
usize,
341344
Delimiter,
342345
Level,
343346
LineColumn,

proc_macro/src/lib.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ pub mod token_stream {
265265
/// Unquoting is done with `$`, and works by taking the single next ident as the unquoted term.
266266
/// To quote `$` itself, use `$$`.
267267
#[unstable(feature = "proc_macro_quote", issue = "54722")]
268-
#[allow_internal_unstable(proc_macro_def_site)]
268+
#[allow_internal_unstable(proc_macro_def_site, proc_macro_internals)]
269269
#[rustc_builtin_macro]
270270
pub macro quote($($t:tt)*) {
271271
/* compiler built-in */
@@ -394,6 +394,20 @@ impl Span {
394394
self.0.source_text()
395395
}
396396

397+
// Used by the implementation of `Span::quote`
398+
#[doc(hidden)]
399+
#[unstable(feature = "proc_macro_internals", issue = "27812")]
400+
pub fn save_span(&self) -> usize {
401+
self.0.save_span()
402+
}
403+
404+
// Used by the implementation of `Span::quote`
405+
#[doc(hidden)]
406+
#[unstable(feature = "proc_macro_internals", issue = "27812")]
407+
pub fn recover_proc_macro_span(id: usize) -> Span {
408+
Span(bridge::client::Span::recover_proc_macro_span(id))
409+
}
410+
397411
diagnostic_method!(error, Level::Error);
398412
diagnostic_method!(warning, Level::Warning);
399413
diagnostic_method!(note, Level::Note);

proc_macro/src/quote.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ pub fn quote(stream: TokenStream) -> TokenStream {
6565
if stream.is_empty() {
6666
return quote!(crate::TokenStream::new());
6767
}
68+
let proc_macro_crate = quote!(crate);
6869
let mut after_dollar = false;
6970
let tokens = stream
7071
.into_iter()
@@ -105,7 +106,7 @@ pub fn quote(stream: TokenStream) -> TokenStream {
105106
))),
106107
TokenTree::Ident(tt) => quote!(crate::TokenTree::Ident(crate::Ident::new(
107108
(@ TokenTree::from(Literal::string(&tt.to_string()))),
108-
(@ quote_span(tt.span())),
109+
(@ quote_span(proc_macro_crate.clone(), tt.span())),
109110
))),
110111
TokenTree::Literal(tt) => quote!(crate::TokenTree::Literal({
111112
let mut iter = (@ TokenTree::from(Literal::string(&tt.to_string())))
@@ -115,7 +116,7 @@ pub fn quote(stream: TokenStream) -> TokenStream {
115116
if let (Some(crate::TokenTree::Literal(mut lit)), None) =
116117
(iter.next(), iter.next())
117118
{
118-
lit.set_span((@ quote_span(tt.span())));
119+
lit.set_span((@ quote_span(proc_macro_crate.clone(), tt.span())));
119120
lit
120121
} else {
121122
unreachable!()
@@ -135,6 +136,7 @@ pub fn quote(stream: TokenStream) -> TokenStream {
135136
/// Quote a `Span` into a `TokenStream`.
136137
/// This is needed to implement a custom quoter.
137138
#[unstable(feature = "proc_macro_quote", issue = "54722")]
138-
pub fn quote_span(_: Span) -> TokenStream {
139-
quote!(crate::Span::def_site())
139+
pub fn quote_span(proc_macro_crate: TokenStream, span: Span) -> TokenStream {
140+
let id = span.save_span();
141+
quote!((@ proc_macro_crate ) ::Span::recover_proc_macro_span((@ TokenTree::from(Literal::usize_unsuffixed(id)))))
140142
}

0 commit comments

Comments
 (0)