-
-
Notifications
You must be signed in to change notification settings - Fork 313
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
How to parse keeping spans #178
Comments
You need to opt in to using the new unstable TokenStream API that preserves span info. Otherwise syn is using a compatibility layer around the old TokenStream which does not have span info. proc-macro2 = { version = "0.1", features = ["unstable"] } Although it seems to not work right now... @alexcrichton. Without
With
Without
|
Looks like it. serde-rs/serde#980 does effectively the same thing: proc_macro2::TokenStream::from(expanded).into() I filed #179 to follow up. |
Yeah :(. This is rust-lang/rust#43081 |
Is rust-lang/rust#43081 really a cause of this?
So.. proc_macro::TokenStream has correct span.
So it also has correct spans. I found that syn and quote does not expose or enable it's "unstable" feature. If it is the cause, what do you think about detecting nightly and automatically enabling "unstable" from proc_macro2 build script? Edit: Clarify meaning of pm2 is a shim. |
And I'm not absolutely sure but current incorrect behavior seems like a that of pm2 without unstable feature (or just proc macros 1.1). |
Enabling "unstable" feature does not work. For experiment, I tried below and I think it's a parsing bug or quasi quoting bug. // Span is correct
return proc_macro2::TokenStream::from(input).into();
// All span points `fn`
return proc_macro2::TokenStream::from(parse::<Item>(function).unwrap().into_tokens()).into(); I will investigate more as I need this to make error reporting accurate. Current behavior (futures-await) is error[E0308]: mismatched types
--> tests/lifetime.rs:12:12
|
12 | #[async]
| ^ expected enum `std::result::Result`, found ()
|
= note: expected type `std::result::Result<u64, std::num::ParseIntError>`
found type `()`
|
When I change span of fn_token to default, error points annotation instead of fn. So ToTokens seems innocent. So I concluded that parser does not advance span. I'll try to fix it. |
Oh I was wrong. Sorry. I noticed that TokenStream was NtItem and has only one valid span, and other spans are just (0..0). I think currently there's no way to bypass this limitation. |
I think this must have been fixed upstream as of rustc 1.24.0-nightly (77e189cd7 2017-12-28). I tried the following macro: #![feature(proc_macro)]
#[macro_use]
extern crate quote;
extern crate syn;
extern crate proc_macro;
use proc_macro::TokenStream;
#[proc_macro]
pub fn roundtrip_item(tokens: TokenStream) -> TokenStream {
let item = syn::parse::<syn::Item>(tokens).unwrap();
quote!(#item).into()
} #![feature(proc_macro)]
extern crate mac;
mac::roundtrip_item! {
struct Antoyo(Missing);
}
fn main() {} Building with
Without
|
Hi.
I tried using the git version (of both
syn
andquote
) to benefit fromproc-macro2
but I'm unable to keep the span information.Here is the code I use:
Is it how I'm suppose to use
syn
andquote
to have span info?Also, is it normal that I need to call
into()
twice?I use rust nightly:
Thanks for your help.
The text was updated successfully, but these errors were encountered: