Hello!
What are you tring to do?
I want to run a static analysis over rust codes. Instead of working with raw user source, I decided to dump intermeidate represention of rust code in the form of HIR/THIR/MIR. The rustc command that emits the representation looks like this:
rustc -Zunpretty=hir-tree target_file.rs
rustc -Zunpretty=thir-tree target_file.rs
rustc -Zdump-mir target_file.rs
As you see, dumping is a unstable feature, and the rustup channel needs to be set on the nightly. Therefore, a whole command is to be:
rustup run nightly rustc -Zunpretty=hir-tree target_file.rs
There is a caveat: Even though a unit of compilation on rust is crate, dumping code is generated per file, which impiles it is even possible to dump only a part of certain crate or module.
When it comes to mod, everything works fine as long as a relative module path can be inferred. Notice that the dumped file is not a entry of lib/main/crate.
// foo.rs
pub struct fooStruct(i32);
// target.rs
mod foo;
struct Bar(foo::fooStruct);
And
rustup run nightly rustc -Zunpretty=hir-tree target.rs
But it is not the case when the dumped file has dependencies. rustc has a option that add a library path to resolve a symbol in the use(or extern crate)
The Problem
Let's say that I want to dump a file 'target.rs', which is a part of the crate, and the crate has a dependency of 'foo'. The file 'target.rs' has a code of 'use foo'.
Then it would look like this:
// target.rs
use foo;
sturct Bar(foo::something_in_foo);
to dump I tried:
rustup run nightly rustc -L crate=/relative/path/to/root/of/foo -Zunpretty=hir-tree target.rs
But it results in the error[E0432], which means "unresolved import Foo"
I guess there is a HIR representation of extern crate/use, because Enum rustc_hir::ItemKind has variants of External Crate and Use. link
What should I do?
What is not desired
The static analysis could be run on the environment that doesn't have build.rs or isn't built with the nightly compiler. So it is hard to make use of features of rustc-env/build.rs/cargo.