-
Notifications
You must be signed in to change notification settings - Fork 12.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to resolve modules for THIR visitor? #129646
Comments
Update: I found out that there is let (resolver, _) = &*tcx.resolver_for_lowering().borrow(); This resolver appears to correctly infer that my submodule is a module. But the error that the module cannot be resolved is triggered before I even invoke this function. So the following is the code in question: rustc_interface::run_compiler(config, |compiler| {
compiler.enter(|queries| {
queries.global_ctxt().unwrap().enter(|tcx| {
let hir_krate = tcx.hir();
// No error thrown
println!("After HIR query");
for id in hir_krate.items() {
// Errors thrown that all items that are not defined in `src/main.rs`
//(the main file of the program that I want to analyze) could not have been found.
// The error messages appear BEFORE the FIRST print
println!("Hello item!");
let item = hir_krate.item(id);
// do stuff with the item
}
});
});
}); The error is thrown precisely at the |
match item.kind {
rustc_hir::ItemKind::Fn(_signature, _generics, body_id) => {
let func_hir = &tcx.hir().body(body_id);
let body_expr = func_hir.value;
match body_expr.kind {
rustc_hir::ExprKind::Block(_, _) => {
match &tcx.thir_body(body_expr.hir_id.owner) {
Ok((thir_ro, expr_id)) => {
// this is where I want to do my analysis
},
Err(x) => {
// But I always get an `ErrorGuaranteed` here
dbg!(x);
}
};
}
_ => {
dbg!("Function body, that is no block..?");
}
}
}
_ => (),
} |
Could you move this discussion to zulip? https://rust-lang.zulipchat.com/ |
Alright, thanks |
Problem statement
I want to do static analysis on rust programs. For AST-only, syn is excellent. But I ran into issues and want to implement a visitor to visit the THIR. There is some docs on how to run an analysis over a typed program. Sadly, it doesn't resolve modules or externs. My question: how can I use the resolver of the compiler to study the functions used in other modules?
What I did so far
cargo build -vv
to see all the flags that cargo sets and set them accordingly in the config of my invocationLog
Lesson from output
As you can see, the analysis successfully runs and outputs the function names. But it fails to resolve both
thir_analysis
andwalkdir::WalkDir
, thoughcargo run
clearly found them.To reproduce
cargo run -- src/main.rs
Question
How do I correctly invoke the compiler to run a static analysis that also resolves modules?
The text was updated successfully, but these errors were encountered: