Getting and using ast/hir/mir of a rust source code

I would like to use rustc to generate ast/hir/mir for a rust source code and I would like to walk the ast and/or analyze the other IRs as well. I don't particularly have an exact goal with this, I would just like to get to know rust and the IRs a little bit better and would like to play with them.

I have read about the syntex_syntax crate, but it is over 2 years old, so I guess it's not my best option.
Next option I've seen is using CompileController, through which one can provide callbacks for the compiler and they will be called at the appropriate stages. I've found a brief tutorial about it here.
Last option I've found so far is the --unpretty flag with the nightly compiler. I checked out some options for this flag, I would like something like this, but avoiding dumping an IR into a json and reading it back would be nice. I guess I can follow this flag in the source code (session -> driver -> ...), but haven't tried it yet.

Can you please provide me some pros/cons about these options and which one is easier?

Your best bet if you want to play with these directly is to write a rustc plugin. If you go this route you may want to tie your project to a specific nightly with a rust-toolchain file.

I've never written one myself but there are a number of things that I can suggest looking at:

  • rocket_codegen pre-4.0 is a decent example of how to define a special attribute you can place on items that is capable of inspecting their HIR.

  • Examples of how to inspect mir can be found in rust-clippy

  • If you're curious about really low level stuff, to my (limited) knowledge, the textual form of LLVM IR is normative. Perhaps there's a decent parser for it somewhere. (maybe)

  • I know there's another intermediate representation called the HAIR that sits between the HIR and MIR:

    The rustc guide:

    The HAIR's most important feature is that the various adjustments (which happen without explicit syntax) like coercions, autoderef, autoref and overloaded method calls have become explicit casts, deref operations, reference expressions or concrete function calls.

    I wouldn't know of any examples of tools that actually look at this (I think it's just an implementation detail of the lowering and so it's simply not useful?), but if you're just curious how the compiler works then if there's a will there's probably a way.

Note: I've heard claims on here that plugins are being removed, but am not sure what these claims are based on. Without a doubt, the goal is that eventually all libraries intended for wide consumption should be powered by stable proc_macros rather than hideously unstable rustc plugins; but when the goal is precisely to peek at rustc's current infrastructure, what could be better? (and besides, clippy needs the infrastructure...)


I have read about the syntex_syntax crate, but it is over 2 years old, so I guess it's not my best option.

This crate was superceded by syn. syn is great for parsing the rust AST and is definitely a tool you'll want when writing proc_macros. However, it won't give you access to the deeper IRs you are interested in looking at.

1 Like