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

Is there a way to use syn with stable Rust? #38

Closed
emk opened this issue Oct 12, 2016 · 5 comments
Closed

Is there a way to use syn with stable Rust? #38

emk opened this issue Oct 12, 2016 · 5 comments

Comments

@emk
Copy link

emk commented Oct 12, 2016

This is a great library!

With syntex, it was possible to support stable Rust via build.rs and source-code processing. Is there a way to do that with syn?

@dtolnay
Copy link
Owner

dtolnay commented Oct 12, 2016

Syn itself compiles fine on stable, in fact serde_codegen depends on it on stable. Here is the relevant code.

  • Use syntex to register a custom derive
  • Receive a syntex AST in your registered function
  • Use pprust to print it to a string
  • Parse it to a syn AST
  • Expand it using the same codepath that proc_macro uses
  • Print the expanded syn AST to a string
  • Create a syntex parse session and parse it
  • Return the expanded syntex AST

There are a few missing pieces before it can be used without syntex but I have been making steady progress on those and expect to have it ready in the next week or two. The biggest blocker was being able to parse the full Rust syntax other than just structs and enums. That is largely done, for example this 6000 line file copied from the rustc source code can be parsed and printed back such that the original and the printed one are parsed identically by syntex.

If you have time to help out, the remaining missing pieces in order of importance are:

The pretty-printer is because debugging will be much easier if your build.rs produces formatted code rather than a single massive line. I don't want to be in the business of maintaining a pretty printer myself, so we need to use a dependency on an existing one that can be disabled once things work.

@dtolnay
Copy link
Owner

dtolnay commented Oct 24, 2016

I implemented the biggest missing piece in #55. I will try to make it through the remaining items quickly.

let mut registry = Registry::new();
registry.add_derive("Serialize", expand_serialize);
registry.add_derive("Deserialize", expand_deserialize);
registry.expand_file(src, dst).unwrap();

fn expand_serialize(input: MacroInput) -> Result<Expanded, String> { /* ... */ }
fn expand_deserialize(input: MacroInput) -> Result<Expanded, String> { /* ... */ }

@dtolnay
Copy link
Owner

dtolnay commented Oct 24, 2016

All the pieces have been implemented. I need to write documentation and example code and cut a release.

@emk
Copy link
Author

emk commented Oct 24, 2016

This is just fantastic! I have a couple of crates that will benefit from this.

@dtolnay
Copy link
Owner

dtolnay commented Oct 31, 2016

I released 0.10.0 with a custom-derive registry that is able to expand custom derives on stable Rust.

@dtolnay dtolnay closed this as completed Oct 31, 2016
Repository owner locked and limited conversation to collaborators Jun 12, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants