How can I implement my own tool with the code in rustc?

Hi, I am interested in rustc, I am trying to use the data structure defined in rustc and then implement my own tool development, how can I do it? For example, I want to use the code related to AST in rustc, how should I introduce the related dependencies in cargo.toml? Or do I need to clone the rust project and then recompile and modify it?

If you just want to use the compiler internals, you can just write extern crate to load whichever crates you want:

extern crate rustc_ast;

// stuff with rustc_ast.

Documentation for the individual crates is at

This only works with the nightly toolchain.

If you want something that actually drives the entire compilation process, you can look at the Clippy source for a real-world example. I thought there was a small example in the docs somewhere, but I'm unable to find it right now.

If you aren't aware, there is also the which gives an overview of the compiler internals.

Thanks, I've got it figured out!

One more question, when I use #! [feature(rustc_private)], does it have an effect on my code if the version of rustc is updated? For example, I am using rustc 1.67.0-nightly, am I always using the internals corresponding to rustc 1.67.0 when I use extern crate rustc_ast and other crates?