Resolve an `QPath::TypeRelative`


Super new to rustc_* libraries, I want to traverse the hir to inspect if a function is called.

I'm using rustc_interface::run_compiler when I am in queries.global_ctxt() I get the hir and start traversing it from some predefined entrypoint that are functions. What I want to do is to check the function calls that I have inside the body of the function, so when I get an ExprKind(path) I follow the path and look at the called function.

Now when I have QPath::Resolved this is very easy to do. But when I have QPath::TypeRelative I'm lost. I get it when I try to call al method for example:

 struct A;
 impl A {
     pub fn new() -> Self {

When a function call A::new() I get QPath::TypeRelative .

Is possible to resolve the path? I tried various things like calling rustc_hir_analysis::check_crate or other functions that seem related to type checking, but the truth is that I'm just trying random things without a clear idea of what I'm doing :frowning:

going through the docs I found TypeckResults::qpath_res(&self, qpath: &QPath<'_>, id: HirId) .

I think that I want something like that. Not sure which qpath and id should I pass.

I tried dbg!(tcx.typeck(segment.hir_id.owner.def_id).qpath_res(qpath, segment.hir_id));


  • qpath = the TypeRelative qpath that I want to resolve.
  • hir_id = the hir A::new() (I guess that the issue is here and is the local_id (*))

But something is wrong cause I get an Err. Btw I feel like I'm in the right direction cause I can see that TypeckResults have the right path for A::new().

(*) seems that local_id in segment.hir_id is different from the one in the TypeRelative maps. But they both are local respect to the same Item cause TypeRelative.hir_owner is the function that call A::new() , and I'm pretty sure that segment.hir_id.local_id refer the the function that call A::new(). I'm saying that cause the only way to change segment.hir_id.local_id is changing that function.

Specifically I always get segment.hir_id.loca_id = local_id_in_the_type_relative_map + 3

Here POC statically check if paths are panic free - announcements - The Rust Programming Language Forum the final result.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.