proc-macro2
A substitute implementation of the compiler's `proc_macro` API to decouple token-based libraries from the procedural macro use case.
This package has a good security score with no known vulnerabilities.
Community Reviews
Rock-solid foundation for proc macros with zero runtime overhead
The ability to write unit tests for procedural macros is the killer feature. Without proc-macro2, testing token manipulation logic requires integration tests that compile test crates. With it, you can write normal #[test] functions that exercise your token parsing and generation logic directly. Error messages from span tracking are precise and helpful during both development and when users encounter macro errors.
The main gotcha is understanding when to use proc-macro2 vs proc-macro types at API boundaries. The .into() conversions are zero-cost but require mental overhead. Version stability has been excellent - the 1.0 line has maintained compatibility while tracking compiler changes seamlessly.
Best for: Building procedural macros that need comprehensive testing and precise error reporting without runtime cost.
Avoid if: You need runtime code generation or dynamic metaprogramming - this is strictly compile-time.
Rock-solid foundation for procedural macro development
The real value shines when testing. You can write tests for your macro logic without needing integration tests that compile entire crates. The conversions between proc_macro and proc_macro2 types are seamless with the .into() trait. Error messages are clear, especially around span manipulation, which helps tremendously when debugging macro hygiene issues.
Documentation is thorough with good examples of common patterns like creating identifiers, literals, and groups. The type system prevents many footguns - you can't accidentally mix spans from different contexts. It integrates perfectly with syn and quote, forming the standard toolkit for macro development.
Best for: Building procedural macros, especially when you need testability and want to separate token manipulation logic into reusable libraries.
Avoid if: You're building simple derive macros where syn's built-in parsing is sufficient and you don't need standalone testing.
Solid foundation for proc macro testing, but documentation assumes expertise
The main learning curve comes from the sparse documentation. The crate docs explain *what* it is, but not *why* you'd use it or how to structure your macro crates effectively. I had to piece together patterns from existing macro libraries like syn and quote. Error messages are generally clear when you mess up token operations, but debugging macro expansion issues still requires println-style debugging with `to_string()` on TokenStreams.
Once you've seen the pattern (proc-macro2 in your lib, thin wrapper in proc-macro crate), it becomes second nature. The ability to write unit tests for macro logic is invaluable and makes this crate essential for serious macro development.
Best for: Developers building procedural macros who need testable, reusable token manipulation logic separate from the proc-macro entry points.
Avoid if: You're building simple derive macros where syn and quote handle everything and you don't need independent testing of token logic.
Sign in to write a review
Sign In