How to add rustc_parse, rustc_middle as cargo dependency?

How do I add things like rustc_parse or rustc_middle as a dependency ? shows very little.

Yes, I do in fact want to add the Rust compiler as a dependency. I'm goofing around with HIR / THIR, and as far as I can tell, this is the only way to parse crates to HIR / THIR.

1 Like

I'm also okay with solutions that

  1. depends on the system installation of cargo / rustc

  2. calls out shell cmd to dump hir / thir via cargo build

  3. somehow parses that back to HIR / THIR -- but it is not clear to me that is possible (i.e. the HIR / THIR types are deriving debug, but I see no way to parse them back from string)


extern crate rustc_middle;
extern crate rustc_parse;

Note that IDE (or at least IDEA) doesn't know about sysroot crates, so they will not be indexed, and IDE features generally won't work. For this reason I prefer to make a local checkout of rustc with the same version as the toolchain, and to add patches to Cargo.toml which point to those locally checked out crates.

Also note that rustc crates are very unstable. You'll spend significant amount of time catching up with the changes, which can occasionally upend your entire architecture, or you'll get stuck on an old nightly toolchain.

1 Like

Using rustc_private also needs rustup component add rustc-dev (and also llvm-tools if rustup doesn't automatically install it) And you have to do extern crate rustc_driver; even if you don't use it. contains the object code for all rustc libraries and the rest of the libraries are shipped as rmeta only to save space, so you need to link against to avoid link errors.

1 Like

LOL. Yeah. Even after installing nightly and using cargo +nightly ... I can't get anything that depends on rustc_parse (local checkout from github) to build.

Recalibrating to how much pain this is going to be.

I don't understand a sentence you said.

I did a local checkout of GitHub - rust-lang/rust: Empowering everyone to build reliable and efficient software. , checkouted a branch matching local rust version.

Then I tried to build something that depends on rustc_parse using cargo +nightly -- and getting errors.

Are your statements above relevant if I have a local checkout of the entire rustc src ?

You shouldn't directly depend on the rust source repo. That version can only be compiled by the exact same rustc version as the rust source is from (and the bootstrap compiler if you use the bootstrap trick to enable nightly features on stable and beta). Instead you should depend on the precompiled libraries in the rustc-dev component that you can install using rustup.

Okay, I think I'm starting to see clarity now.

There are two approaches to getting rustc_parse:

  1. Clone GitHub - rust-lang/rust: Empowering everyone to build reliable and efficient software. , try to get right tag, etc ...

  2. Use the "builtin" version of rustc_parse -- for some definition of "builtin" -- i.e. whatever version cargo itself is using. And this is the approach your previous comment addresses ?

Is this correct ?

Note that in my comment I don't suggest building your project with rustc_parse from a git checkout. I use the patch only for local code editing, don't commit it, and disable it before building the project. It's a pain, but not as much pain as losing all intellisense for rustc crates.

Didn't know about the rustc_driver caveat. Guess I didn't get any link errors since the other workspace crates used rustc_driver anyway.

@afetisov @bjorn3 : I got something to (1) depend on rustc_parse and (2) compile now. LOL. Thanks.

Next up: get intelli sense working / try to write some code.

I definitely misunderstood you. Are you suggesting the following:

  1. git clone rust-lang/rust

  2. in local project, add "rustc_parse = { path = ... }"

  3. get intelli sense, do editing

  4. comment out #2; compile

  5. goto #2

The goal here is to get intellisense during editing (not working for me yet) and use the existing rustc_parse during compile ?

Yes. If there is a better way to do it, I'd love to know it. I don't know whether rust-analyzer can deal with sysroot crates directly.

If you set rust-analyzer.rustc.source to "discover" or the path to a local checkout of rustc and add the following to the Cargo.toml of every crate that uses the rustc_* crates:


rust-analyzer should provide all the regular features for usage of the rustc internal crates.


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.