Tokenstream -> syn -> ? for typed info

Input: rust crate

In memory "output": I want something higher level than tokenstream/syn. I want, for every variable, it's Type. What crates do I need to get this info ?


You can't reliably get it in something like a proc macro because type checking happens after that.[1] If you want it to be more than a guess, you need something like THIR, which happens after any type of macro system.[2]

There's no way a macro system can figure out the type of x in something like

let x = foreign_function(17);

(It can't even figure out which integer type the argument is.)

  1. Didn't this already come up before recently? ↩︎

  2. And if there's a good way to tap into THIR/MIR/whatever, I'm unaware of it (but that doesn't mean it doesn't exist) ↩︎


I'm not writing a macro. I am goofing around with indexing Rust code / custom tools for searching Rust code, where I really really really do want the type info of variables.

Is there something less "heavy" than adding the Rust compiler as a dependency ?

Use Rust Analyser as a library. It has the benefit of being the second kid on the block so it's easier to hook into, plus it's not tied to a specific version of the nightly compiler and the maintainers are very conscious of the project's build times.

Its also going to be well suited to these sorts of queries because they are the bread and butter of code assists (the little :bulb: thing).


This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.