Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Investigate proc_macro::quote_span integration #184

Closed
Aaron1011 opened this issue May 15, 2021 · 5 comments
Closed

Investigate proc_macro::quote_span integration #184

Aaron1011 opened this issue May 15, 2021 · 5 comments

Comments

@Aaron1011
Copy link

With PR rust-lang/rust#84278, Rust now has the ability to quote a Span to a TokenStream. This allows us to obtain spans pointing into a proc-macro crate:

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

Currently, this is integrated with the unstable proc_macro::quote! macro. However, as virtually everyone uses the quote crate instead, it would be nice to add span-quoting support here (behind a nightly feature).

The span-quoting API is used via proc_macro::quote_span, and produces a TokenStream. I've provided an example of using it here.

I'm not sure how to best way to integrate this API into the quote crate, since it looks like a Span parameter isn't passed around in many places (presumably for performance reasons). However, I'd be happy to work on a PR if you support the overall idea, and know the general shape of the modifications that you'd like.

@Aaron1011
Copy link
Author

cc @dtolnay - does the overall idea of supporting this in quote sound reasonable?

@dtolnay
Copy link
Owner

dtolnay commented Aug 3, 2021

If I understand correctly, you are asking to reimplement quote as a procedural macro? If so, I am not interested in that. I think the benefits of this change would be minor or nonexistent for typical users of this crate and the compile time cost would greatly outweigh it in comparison.

@Aaron1011
Copy link
Author

It should be possible to get most of the benefit of quote_span without re-implementing the quote crate. Instead, a helper proc-macro crate would be used to implement push_ident:

quote/src/lib.rs

Lines 1047 to 1049 in cbe5ded

($tokens:ident $ident:ident) => {
$crate::__private::push_ident(&mut $tokens, stringify!($ident));
};

We would pass the captured $ident metavariable to the proc-macro:

quote_helper::quote_with_span!($ident)

The macro implementation would call proc_macro::quote_span with the Span taken from its input TokenStream. As a result, quote_with_span!($ident) would expand to something like Ident::new(<literal_ident_contents>, ::Span::recover_proc_macro_span(...))

This would allow the rest of quote to remain unchanged. We wouldn't be able to quote spans for the various operators special-cased by quote_token, but it should be enough to get many error messages to point to the proper place within a proc-macro crate.

@dtolnay
Copy link
Owner

dtolnay commented Aug 7, 2021

Nice, that sounds promising -- but I would like this to be done as a different crate for now.

@dtolnay
Copy link
Owner

dtolnay commented Dec 20, 2021

I'll close since I am not planning on pursuing this in quote for now. I am interested to hear how it goes in a different crate though.

@dtolnay dtolnay closed this as completed Dec 20, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants