The rustc_interface allows you to interact with Rust code at various stages of compilation.
To get the type of an expression, use the global_ctxt() query to get a TyCtxt.
The following was tested with nightly-2024-01-19:
{{#include ../examples/rustc-driver-interacting-with-the-ast.rs}}