Rust AST with types?

I have a couple of features I'd like to implement that depend on having type information for Rust source code:

  1. Syntax highlighting with added links to documentation for types and methods of a certain crate. When I display code fragments from a README, doc comments, examples' source code I'd like to know which spans/tokens are which types and methods, so I can wrap them in exact docs.rs links.

  2. Statistics of most popular functions and types of a Cargo crate, based on usage in all other crates (I can do crater-like run). For this I don't need to know exact source code positions, just fully* expanded names of types and functions (* but knowing when a type comes from a type alias would be great to count all the custom Result aliases).

What are my options?

For #1 I suppose I could try to use RLS. For #2 I'm thinking about dumping MIR and searching for UFCS, but MIR syntax is not supposed to be parseable.

Is there a place in rustc where such information could be dumped relatively easily? Are there other tools or compiler output modes that would be useful?

I think both could be achieved using rls-save analysis: GitHub - rust-dev-tools/rls-analysis: Core functionality for handling rustc's save-analysis data. You can take a look at how cargo-src is implemented, it does similar things.

Rust analyzer in theory should also provide a convenient API for doing this sort of things, but, while the basics of type inference already work, it has a long way to go until it can analyze all the code precisely.

2 Likes