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
Proposal: Use rustdoc
JSON instead of parsing with syn
#906
Comments
The infrastructure we use is all open-source and based on the Trustfall query engine and the If you're interested in reusing some of that work, I'd be happy to point you in the right direction and help you get started. |
Is this true? We also need to know If rustdoc provides all that information, that might be an interesting approach. I'd be happy to take a patch that prototypes it assuming no regressions... cc #287 for things that would allow us to handle macros. |
We generate rustdoc JSON with the |
Cool! Curious, do you know how fast / slow rustdoc happens to be compared to syn? Also, thinking a bit about what cbindgen features we use in Firefox I came up with:
|
Rustdoc is a component of rustc, so to my knowledge it behaves very similarly: it runs build scripts, resolves dependencies, requires the code to type-check, handles incremental builds, etc. I've never benchmarked it versus syn but I'd expect it's often slower because syn isn't doing nearly as much work. |
Parsing Rust code with
syn
is useful for macro authors, but has its limitations for whole-crate tools such ascbindgen
, as is apparent in that it does not properly support namespacing since it has no access to type information and such (and the support for macro expansion is also a bit spotty).In an ideal world, I think
cbindgen
should be implemented usingrustc_driver
, and distributed alongside the Rust compiler inrustup
(perhaps with acargo cbindgen
subcommand included); this would give it all the power ofrustc
, and allow it to do full semantic analysis of the code; weird quirks begone!But I recognize that that's a huge ask from several teams, and that it may not be desired for the Rust project to take ownership over this project (which is effectively a blocker for using
rustc_driver
, as doing that out-of-tree / without support from upstream is a huge hazzle).Luckily, there exists something else we can use! Enter
rustdoc
's JSON output (documentation for the format here, can be access conveniently via. therustdoc-types
crate). This fully describes a crate's public API, which is more than enough forcbindgen
's needs (it only needs to know allpub extern "C" fn
, and their dependent types).While the format itself is yet unstable, it is already depended on in the ecosystem, prominently
cargo-semver-checks
, so I'd say there is a high likelihood that it will not outright go away without some sort of replacement1.So: I propose that
cbindgen
transitions to using this instead ofsyn
, which would solve all of the aforementioned problems with namespacing (and likely make the implementation ofcbindgen
smaller, and capable of more advanced semantic type analysis in the future (example of something: knowledge of auto traits)).What do you think?
1: If
cbindgen
ends up going this route, we should of course state so on the tracking issue.The text was updated successfully, but these errors were encountered: