Syn 0.12 -- complete redesign for all your Macros 1.1 and 2.0 needs


Syn is a parsing library for parsing a stream of Rust tokens into a syntax tree of Rust source code.

This release features a redesigned AST that is more consistent and easier to understand than the previous one, and supports more comfortable idioms for traversing and building the syntax tree.

In addition, unlike all past Syn releases which focused on parsing strings of source code as input, this release is built around parsing the Rust compiler’s token representation used by 2.0 procedural macros. We provide a polyfill so that code written against the token-based API of tomorrow can work just as well on Rust compilers all the way back to the first stable support for procedural macros in Rust 1.15.0.

Whether you have used Syn extensively already or are looking at it for the first time, I encourage you to browse through our brand new examples directory featuring runnable working procedural macros of all sorts. Check out heapsize2 for a 2.0-style custom derive with high quality error messages, lazy-static for a function-like procedural macro that demonstrates the custom diagnostic API, and trace-var for a procedural macro attribute that mutates a function body.

Error mesage from a 2.0-style custom derive

error[E0277]: the trait bound `std::thread::Thread: HeapSize` is not satisfied
 --> src/
7 |     bad: std::thread::Thread,
  |     ^^^^^^^^^^^^^^^^^^^^^^^^ the trait `HeapSize` is not implemented for `std::thread::Thread`

Custom warning from a procedural macro

warning: come on, pick a more creative name
  --> src/
10 |     static ref FOO: String = "lazy_static".to_owned();
   |                ^^^

The 0.12 release represents many person-months of design iteration and documentation. Thanks in particular to @mystor for being a force multiplier and shouldering a lot of both the tricky and the tedious implementation work, @alexcrichton for the key idea of polyfilling Macros 2.0 support atop a Macros 1.1 shim and for outstanding rapid prototyping work in the futures-await library, and @jseyfried and @DroidLogician both for driving all of this procedural macro stuff forward on the compiler side with implementation, insightful discussion, and responsiveness.


I want to be sure to highlight the underlying work done here by @dtolnay and @mystor, they’ve done an insane amount of work in preparation for this release! The entire syn crate has basically been rewritten from the ground up, which is no small feat as it’s a parser for all of Rust!

@dtolnay and @mystor have also done tireless work to make the syn crate’s API actually pleasant to use and have put a huge amount of thought towards making it as ergonomic as possible to use. I’ve personally been quite impressed with the result and I’m looking forward to updating a whole slew of projects to these released versions!

I’d encourage any macro authors to kick the tires on these crates, I’ve found them excellent for working with procedural macros (unfortunately still an unstable feature) but what’s possible with them is so much more powerful!

Congrats and great job @dtolnay, @mystor, and of course everyone else helping out with proc macros 2.0!