Getting the correct BodyId for an Expr/Local


I need to access type information for locals and expr, it seems that I need the BodyId for this.

I am not quite sure how to find the correct bodyid for a given expr/local and that is why I am currently iterating over every body in the crate.

fn visit_expr(&mut self, ex: &'v Expr) {
    let ty =
        .filter_map(|(&id, _)| self.ty_ctx.body_tables(id).expr_ty_opt(ex))
    println!("ty = {:?}", ty);
    walk_expr(self, ex);

fn visit_local(&mut self, l: &'v Local) {
    walk_local(self, l);
    let ty: Vec<_> =
        .filter_map(|(&id, _)| self.ty_ctx.body_tables(id).node_id_to_type_opt(l.hir_id))
    println!("ty = {:?}", ty);

But this sometimes results in more than one type and the first type is probably not always correct.

So I need to find the correct body for a given expr/local, how would I do that? Do I need to do this manually while traversing over the ast or is there something that maps from nodeid to bodyid? is a hir::map::Map

I have also seen but a Node doesn’t contain a body.


You can add an impl for visit_body, and store the body id (or even the body_tables) in Self

fn visit_body(...) {
    // take the old body_tables
    // insert new body_tables
    // reinsert the old body_tables


Thanks, I wasn’t quite sure if I needed to do that.