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

"nightly" feature of proc-macro2 is not working #88

Closed
ngg opened this issue Mar 7, 2018 · 2 comments · Fixed by #89
Closed

"nightly" feature of proc-macro2 is not working #88

ngg opened this issue Mar 7, 2018 · 2 comments · Fixed by #89

Comments

@ngg
Copy link
Contributor

ngg commented Mar 7, 2018

I have a project that tries to use prost and I have another dependency that uses the "nightly" feature of proc-macro2 which makes it a wrapper around the compiler's inner proc-macro crate.

The biggest difference is in handling spans.
While in the current version Span::def_site() and Span::call_site() is the same, it's really different in the nightly version.
I think at most of the places prost cannot use the quote! macro and Ident::from because that makes them "hygienic" so they cannot use anything from outside the macros and they cannot export anything outside of the macro.
If you run cargo build inside the tests dir, you get thousands of errors because of this.

To fix this prost should use quote_spanned!(Span::call_site()=> ...) and Ident::new(&..., Span::call_site()) at most places.

Please take a look at the discussion here about this difference: rust-lang/rust#45934

I found a quick and dirty workaround to try to use call_site() in all quote!s by overriding the macro like this:

// prost-derive/src/lib.rs
use proc_macro2::Span;
macro_rules! quote {
    ($($tt:tt)*) => (quote_spanned!(Span::call_site()=> $($tt)*));
}

The thousands of errors disappear after this, but there are a few new ones:

   Compiling tests v0.0.0 (file:///home/ngg/git/prost/tests)
error: int literal is too large
 --> /home/ngg/git/prost/target/debug/build/tests-b935ebcf576482e1/out/protobuf_unittest.rs:3:28
  |
3 | #[derive(Clone, PartialEq, Message)]
  |                            ^^^^^^^

error: proc-macro derive produced unparseable tokens
 --> /home/ngg/git/prost/target/debug/build/tests-b935ebcf576482e1/out/protobuf_unittest.rs:3:28
  |
3 | #[derive(Clone, PartialEq, Message)]
  |                            ^^^^^^^

error: Could not compile `tests`.

I've tried to change the few explicit Span::def_site() references into Span::call_site() and all Ident::from(...) into Ident::new(&..., Span::call_site()) but unfortunately these didn't fix the problem.

I have not yet debugged it further, I'd like to ask for your opinion before.
I don't think this is really urgent, but it will need to be handled somehow.

My current changes can be found here: danburkert/prost@master...ngg:nightly-proc-macro2

@ngg
Copy link
Contributor Author

ngg commented Mar 9, 2018

The remaining errors turned out to be a bug (either in the rust compiler or in quote depending on the viewpoint):
rust-lang/rust#48889

@ngg
Copy link
Contributor Author

ngg commented Apr 2, 2018

rust-lang/rust#48889 has been fixed in latest nightly, and with the new proc-macro2 0.3.x it is really easy to be compatible with both the stable and nightly macros, I've simplified my pull request #89. Please consider merging it!

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

Successfully merging a pull request may close this issue.

1 participant