rustviz-plugin 2.0.0

A tool that allows users to generate an interactive timeline depicting ownership and borrowing events for variables in a Rust code example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660

use std::{cmp::max, collections::{BTreeMap, HashMap, HashSet, VecDeque}, ops::Bound};
use log::warn;
use rustc_hir::Mutability;
use crate::svg_generator::data::{ExternalEvent, ResourceAccessPoint, ResourceTy};
use rustc_middle::ty::{Ty, TyCtxt};
use rustc_span::Span;
use rustc_hir::{Expr, ExprKind, Path, def::Res, Pat, PatKind, QPath, HirId, StmtKind, Stmt, Block, UnOp};

use crate::expr_visitor::{RapData, RefData};

// outdated function don't use this to find a name of a variable/hir_id
pub fn extract_var_name(input_string: &str ) -> Option<String> {
  let start_index = input_string.find('`')? + 1;
  let end_index = input_string.rfind('`')?;
  let rough_string=input_string[start_index..end_index].to_owned();
  if rough_string.contains("String::from"){
    Some(String::from("String::from"))
  }
  else{
    Some(rough_string)
  }
}

// Best-effort name extraction from a HIR id. Originally implemented by
// pretty-printing the node and grepping for backticked tokens; that pretty
// printer (`Map::node_to_string`) was removed in rustc 1.85, so we now walk
// the node directly.
pub fn hirid_to_var_name(id: HirId, tcx: &TyCtxt) -> Option<String> {
  fn name_of_path(p: &Path<'_>) -> String {
    let segs: Vec<String> = p.segments.iter().map(|s| s.ident.as_str().to_owned()).collect();
    segs.join("::")
  }
  // For `T::method`, recover the qself's type name so the returned string
  // covers the same source range that the path's span does (e.g. "String::from"
  // rather than just "from").
  fn name_of_ty(ty: &rustc_hir::Ty<'_>) -> Option<String> {
    match ty.kind {
      rustc_hir::TyKind::Path(QPath::Resolved(_, path)) => Some(name_of_path(path)),
      rustc_hir::TyKind::Path(QPath::TypeRelative(_, segment)) => Some(segment.ident.as_str().to_owned()),
      _ => None,
    }
  }
  let raw = match tcx.hir_node(id) {
    rustc_hir::Node::Expr(e) => match e.kind {
      ExprKind::Path(QPath::Resolved(_, path)) => name_of_path(path),
      ExprKind::Path(QPath::TypeRelative(qself, segment)) => {
        match name_of_ty(qself) {
          Some(qname) => format!("{}::{}", qname, segment.ident.as_str()),
          None => segment.ident.as_str().to_owned(),
        }
      }
      _ => return None,
    },
    n => n.ident().map(|i| i.as_str().to_owned())?,
  };
  // Preserve the historical normalization: any path containing String::from is
  // collapsed to the bare String::from so callers don't see e.g. <String as From<&str>>::from.
  if raw.contains("String::from") {
    Some(String::from("String::from"))
  } else {
    Some(raw)
  }
}

pub fn bool_of_mut (m: Mutability) -> bool {
  match m {
    Mutability::Not => {
      false
    }
    _ => { true }
  }
}


pub fn span_to_line(span:&Span, tcx: &TyCtxt) -> usize{
  tcx.sess.source_map().lookup_char_pos(span.lo()).line
}

pub fn expr_to_line(expr:&Expr, tcx: &TyCtxt) -> usize{
  span_to_line(&expr.span, tcx)
}

pub fn is_addr(expr: &Expr) -> bool { // todo, probably a better way to do this using the typechecker
  match expr.kind {
    ExprKind::AddrOf(..) => true,
    _ => false
  }
}

// The purpose of this function is to override the typechecking done on variables
// when we want to consider them owners rather than structs. This is because for 99.9% of cases
// users don't care about acessing String::len/buf members. 
// We should work towards elimanting all struct members that are not used in the program
// and that way we can just get rid of this hacky function.
pub fn ty_is_special_owner<'tcx> (tcx: &TyCtxt<'tcx>, t: &Ty<'tcx>) -> bool {
  match &*t.sort_string(*tcx) {
    "`std::string::String`" => { true }
    _ => {
      false
    }
  }
}

// Get a string representation of a Path - usually used as a name for a RAP
pub fn string_of_path(p: &Path, tcx: &TyCtxt) -> String {
  match p.res {
    Res::Def(rustc_hir::def::DefKind::Ctor(_, _), _) => {
      let mut name = String::new();
      for (i, segment) in p.segments.iter().enumerate() {
        name.push_str(tcx.hir_name(segment.hir_id).as_str());
        if i < p.segments.len() - 1 {
          name.push_str("::");
        }
      }
      name
    }
    _ => { // technically this is incomplete - there are more cases to cover
    
      tcx.hir_name(p.segments[0].hir_id).as_str().to_owned()
    }
  }
}

// Get the name of a pattern
// used for getting names of arms of Match expr
pub fn get_name_of_pat(pat: &Pat, tcx: &TyCtxt) -> String {
  match pat.kind {
    PatKind::Binding(_, _, ident, _) => ident.to_string(),
    PatKind::TupleStruct(QPath::Resolved(_, p), _, _) => {
      string_of_path(&p, tcx)
    }
    PatKind::Expr(rustc_hir::PatExpr { kind: rustc_hir::PatExprKind::Path(QPath::Resolved(_, p)), .. }) => {
      string_of_path(&p, tcx)
    }
    // Literal-pattern arms like `true =>` / `0 =>` show up in
    // macro-expanded code we have no source-text view of (chiefly
    // `assert!(cond)`, which expands to a match with `true` and `_`
    // arms). Render the literal's Debug form as the arm label rather
    // than panicking; the value is used purely as a label, not for
    // semantic analysis, so a "Bool(true)" / "Int(0, …)" label is
    // sufficient. Avoids needing to import rustc_ast for a typed
    // match on LitKind.
    PatKind::Expr(rustc_hir::PatExpr { kind: rustc_hir::PatExprKind::Lit { lit, .. }, .. }) => {
      format!("{:?}", lit.node)
    }
    PatKind::Wild => {
      String::from("Wild")
    }
    PatKind::Tuple(pat_list, _) => {
      let mut res = String::from("(");
      for (i, p) in pat_list.iter().enumerate() {
        res.push_str(&get_name_of_pat(p, tcx));
        if i < pat_list.len() - 1{
          res.push_str(", ");
        }
        else {
          res.push(')');
        }
      }
      res
    }
    _ => panic!("unexpected pat kind {:#?}", pat)
  }
}

pub fn num_derefs(expr: &Expr) -> usize{
  match expr.kind {
    ExprKind::Unary(UnOp::Deref, exp) => {
      1 + num_derefs(exp)
    }
    _ => 0
  }
}



// LIVENESS + DECLARATION FUNCTIONS
// Functions used for getting variables that are live inside of blocks
// As well as variables that are declared inside of blocks

pub fn get_decl_of_block(block: &Block, tcx: &TyCtxt, raps: &HashMap<String, RapData>) -> HashSet<ResourceAccessPoint>{
  let mut res:HashSet<ResourceAccessPoint> = HashSet::new();
  for stmt in block.stmts.iter() {
    res = res.union(&get_decl_of_stmt(&stmt, tcx, raps)).cloned().collect();
  }
  res
}
// we only care about fetching the declarations in the current block, which is why these functions are not mutually recursive
pub fn get_decl_of_expr(expr: &Expr, tcx: &TyCtxt, raps: &HashMap<String, RapData>) -> HashSet<ResourceAccessPoint> {
  match expr.kind {
    ExprKind::Block(b, _) => get_decl_of_block(b, tcx, raps),
    _ => HashSet::new() // maybe should handle match expressions as well? 
  }
}

pub fn get_decl_of_stmt(stmt: &Stmt, _tcx: &TyCtxt, raps: &HashMap<String, RapData>) -> HashSet<ResourceAccessPoint> {
  let mut res = HashSet::new();
  match stmt.kind {
    StmtKind::Let(ref local) => {
      let name = match local.pat.kind {
        PatKind::Binding(_, _, ident, _) => {
          ident.to_string()
        }
        _ => panic!("unexpected let binding pattern")
      };
      res.insert(raps.get(&name).unwrap().rap.to_owned());
    }
    _ => {}
  }
  res
}

pub fn get_live_of_stmt(stmt: &Stmt, tcx: &TyCtxt, raps: &HashMap<String, RapData>) -> HashSet<ResourceAccessPoint> {
  match stmt.kind {
    StmtKind::Let(ref local) => {
      match local.init {
        Some(expr) => {
          get_live_of_expr(&expr, tcx, raps)
        }
        None => HashSet::new()
      }
    }
    StmtKind::Item(_item) => panic!("not yet able to handle items inside of bodies"),
    StmtKind::Expr(ref expression) | StmtKind::Semi(ref expression) => {
        get_live_of_expr(expression, tcx, raps)
    }
  }
}

// Get all the live variables in the expression
// where live refers to the variables defined OUTSIDE of the expression
// and used inside it. This is distinct from those declared inside it
pub fn get_live_of_expr(expr: &Expr, tcx: &TyCtxt, raps: &HashMap<String, RapData>) -> HashSet<ResourceAccessPoint> {
  match expr.kind {
    ExprKind::Path(QPath::Resolved(_,p)) => {
      match p.res {
        Res::Def(rustc_hir::def::DefKind::Ctor(_, _), _) => {
          // function, so we don't care about it in regards to live vars
          HashSet::new()
        }
        _ => {
          let name = tcx.hir_name(p.segments[0].hir_id).as_str().to_owned();
          HashSet::from([raps.get(&name).unwrap().rap.to_owned()])
        }
      }
    }
    ExprKind::Field(expr, ident) => {
      match expr {
        Expr{kind: ExprKind::Path(QPath::Resolved(_,p)), ..} => {
          let name = tcx.hir_name(p.segments[0].hir_id).as_str().to_owned();
          let field_name: String = ident.as_str().to_owned();
          let total_name = format!("{}.{}", name, field_name);
          HashSet::from([raps.get(&total_name).unwrap().rap.to_owned()])
        }
        _ => { panic!("unexpected field expr") }
      } 
    }
    ExprKind::AddrOf(_, _, exp) | ExprKind::Unary(_, exp) 
    | ExprKind::DropTemps(exp) => {
      get_live_of_expr(exp, tcx, raps)
    }
    ExprKind::Binary(_, lhs_expr, rhs_expr) | ExprKind::Assign(lhs_expr, rhs_expr, _) | ExprKind::AssignOp(_, lhs_expr, rhs_expr) => {
      let lhs = get_live_of_expr(&lhs_expr, tcx, raps);
      let rhs = get_live_of_expr(&rhs_expr, tcx, raps);
      let res = lhs.union(&rhs).cloned().collect();
      res
    }
    ExprKind::Call(fn_expr, args) => {
      let mut res = HashSet::new();
      match fn_expr.kind {
        // Match onto println! macro
        ExprKind::Path(QPath::Resolved(_,rustc_hir::Path{res: rustc_hir::def::Res::Def(_, id), ..})) 
        if !id.is_local() => {
          match args {
            [Expr{kind: ExprKind::Call(_, a),..}] => {
              match a {
                [_, Expr{kind: ExprKind::AddrOf(_, _, 
                  Expr{kind: ExprKind::Array(x),..}),..}] => {
                    for exp in x.iter() {
                      match exp {
                        Expr{kind: ExprKind::Call(_, format_args), ..} => {
                          for a_expr in format_args.iter() {
                            res = res.union(&get_live_of_expr(&a_expr, tcx, raps)).cloned().collect();
                          }
                        }
                        _ => {
                          warn!("getting here to the println 1");
                        }
                      }
                    }
                  }
                _ => {
                  warn!("getting here to the println 2");
                }
              }
            }
            _ => {
              for a_expr in args.iter() {
                res = res.union(&get_live_of_expr(&a_expr, tcx, raps)).cloned().collect();
              }
            }
          }
        }
        _ => {
          for a_expr in args.iter() {
            res = res.union(&get_live_of_expr(&a_expr, tcx, raps)).cloned().collect();
          }
        }
      }
      res
    }
    ExprKind::MethodCall(_, rcvr, args, _) => {
      let rcvr_name = hirid_to_var_name(rcvr.hir_id, tcx).unwrap(); // TODO: will break for chained methodcalls
      let mut res = HashSet::from([raps.get(&rcvr_name).unwrap().rap.to_owned()]);
      for a_expr in args.iter() {
        res = res.union(&get_live_of_expr(&a_expr, tcx, raps)).cloned().collect();
      }
      res
    }
    // Branch expressions need to be handled a bit differently, 
    // We want the variables that are live in each block, but not the ones that were declared in 
    // the blocks (since their timelines should not be split)
    ExprKind::Block(b, _) => {
      let mut res: HashSet<ResourceAccessPoint> = HashSet::new();
      for stmt in b.stmts.iter() {
        res = res.union(&get_live_of_stmt(&stmt, tcx, raps)).cloned().collect();
      }
      match b.expr {
        Some(exp) => {
          res = res.union(&get_live_of_expr(exp, tcx, raps)).cloned().collect();
        }
        None => {}
      }
      res.difference(&get_decl_of_block(b, tcx, raps)).cloned().collect() // need to remove the variables that were declared in the current block 
    }
    ExprKind::If(guard_expr, if_expr, else_expr) => {
      let mut res: HashSet<ResourceAccessPoint> = HashSet::new();
      res = res.union(&get_live_of_expr(&guard_expr, tcx, raps)).cloned().collect();
      res = res.union(&get_live_of_expr(&if_expr, tcx, raps)).cloned().collect();
      match else_expr {
        Some(e) => {
          res = res.union(&get_live_of_expr(&e, tcx, raps)).cloned().collect();
        }
        None => {}
      }
      res
    }
    ExprKind::Tup(expr_list) => {
      let mut res: HashSet<ResourceAccessPoint> = HashSet::new();
      for e in expr_list.iter() {
        res = res.union(&get_live_of_expr(e, tcx, raps)).cloned().collect();
      }
      res
    }
    _ => {
      HashSet::new()
    }
  }
}

// Used for filtering events from the main event container
pub fn filter_ev((line_num, _ev): &(usize, ExternalEvent), split: usize, merge: usize) -> bool {
  if *line_num <= merge && *line_num >= split {
    true
  }
  else {
    false
  }
}

// Fetch the mutability of the borrow
pub fn fetch_mutability(expr: & Expr) -> Option<Mutability> {
  match expr.kind {
    ExprKind::Block(b, _) => {
      match b.expr {
        Some(expr) => { fetch_mutability(expr) }
        None => { panic!("invalid expr for fetching mutability") }
      }
    }
    ExprKind::AddrOf(_, mutability, expr) => {
      match fetch_mutability(&expr) {
        None => Some(mutability), 
        Some(m) => Some(m)
      }
    }
    _ => None
  }
}


// BORROWING LOGIC HELPERS

// Group loans for a certain lender into regions
// A region is defined as an area where multiple loans on the same local are active at the same time
pub fn get_regions(h: &HashSet<String>, borrow_map: &HashMap<String, RefData>) -> Vec<HashSet<String>> {
  let mut res: BTreeMap<usize, (usize, HashSet<String>)>  = BTreeMap::new();

  for borrower in h {
    let b_data = borrow_map.get(borrower).unwrap();
    let a_place = b_data.assigned_at;
    let k_place = b_data.lifetime;

    let mut c = res.upper_bound_mut(Bound::Included(&a_place));
    let mut to_replace: Option<(usize, (usize, HashSet<String>))> = None;
    match c.peek_prev() { // look to our left
      Some((_, (k, map))) => {
        if a_place < *k { // if current borrower was assigned in the same region
          *k = max(*k, k_place); // adjust region to encapsulate all lifetimes (extending the lifetime to the right)
          map.insert(borrower.clone());
        }
        else { // borrower belongs to a different region
          res.insert(a_place, (k_place, HashSet::from([borrower.clone()])));
        }
      }
      None => {
        match c.peek_next() { // look to our right
          Some((a, (k, map))) => {
            if *a < k_place { // need to do this because we can't mutate keys (would break BTreeMap invariants) 
            // although in our case it wouldn't matter because you still wouldn't be able to change the relative ordering of regions (try a proof by contradiction)
              map.insert(borrower.clone());
              to_replace = Some((*a, (max(*k, k_place), map.clone()))); // extending the lifetime to the left
            }
            else {
              res.insert(a_place, (k_place, HashSet::from([borrower.clone()])));
            }
          }
          None => {
            res.insert(a_place, (k_place, HashSet::from([borrower.clone()])));
          }
        }
      }
    }

    // if we need to replace a key
    match to_replace {
      Some((key, (k, map))) => {
        res.remove(&key);
        res.insert(a_place, (k, map));
      }
      None => {}
    }
  }

  let mut z: Vec<HashSet<String>> = Vec::new();
  for (_, (_, map)) in res.into_iter() {
    z.push(map);
  }
  z
}


// get the non anonymous lenders and their respective 'active' borrowers
pub fn get_non_anon_lenders(b_map: &HashMap<String, RefData>) -> HashMap<String, HashSet<String>> {
  let mut res: HashMap<String, HashSet<String>> = HashMap::new();
  for (k, v) in b_map.iter() {
    match v.lender {
      ResourceTy::Anonymous => {},
      _ => {
        let lender = v.lender.real_name();
        res.entry(lender.clone())
          .and_modify(|lendees| { lendees.insert(k.to_owned()); })
          .or_insert(HashSet::from([k.to_owned()]));
      }
    }
  }
  res
}

// get borrowers associated with a single lender
pub fn get_borrowers(borrower: &String, borrow_map: &HashMap<String, RefData>) -> HashSet<String> {
  let lender = borrow_map.get(borrower).unwrap().lender.clone();
  match lender {
    ResourceTy::Anonymous | ResourceTy::Caller => HashSet::from([borrower.to_owned()]),
    _ => {
      let mut res:HashSet<String> = HashSet::new();
      for (k, v) in borrow_map.iter() {
        if v.lender == lender {
          res.insert(k.to_string());
        }
      } 
      res
    }
  }
}

pub fn get_aliasing_data(r: &ResourceTy, borrow_map: &HashMap<String, RefData>) -> VecDeque<String> {
  match r {
    ResourceTy::Anonymous | ResourceTy::Caller => VecDeque::new(),
    ResourceTy::Value(x) | ResourceTy::Deref(x) => {
      match borrow_map.get(x.name()) {
        Some(r_data) => r_data.aliasing.to_owned(),
        None => VecDeque::new()
      }
    }
  }
}

// FETCHING RAP HELPERS
// Gonna be honest these functions are poorly named and are all very similar but subtly different

// Gets the RAP associated with an expr where we expect expr to resolve to a singular RAP
// For example, it will return the RAp/ResourceTy associated with a function/variable/etc
// depending on how its used
pub fn get_rap(expr: &Expr, tcx: &TyCtxt, raps: &HashMap<String, RapData>) -> ResourceTy {
  match expr.kind {
    ExprKind::Path(QPath::Resolved(_,p)) => {
      let name = tcx.hir_name(p.segments[0].hir_id).as_str().to_owned();
      // Fall back to Anonymous when the path resolves to a name we
      // didn't register as a RAP. The most common cause is a path
      // expression inside a macro expansion that references a
      // synthetic local (e.g. modern `println!` expands to refs to
      // an `args` binding that visit_local declines to register).
      // Anonymous is the existing return for "unknown resource";
      // downstream code already handles it.
      match raps.get(&name) {
        Some(rd) => ResourceTy::Value(rd.rap.to_owned()),
        None => ResourceTy::Anonymous,
      }
    }
    // In a deref expression 
    ExprKind::Unary(UnOp::Deref, expr) => {
      let rhs_rap = fetch_rap(&expr, tcx, raps);
      match rhs_rap {
        Some(x) => {
          ResourceTy::Deref(x)
        }
        None => ResourceTy::Anonymous
      }
    }
    ExprKind::AddrOf(_, _, expr) | ExprKind::Unary(_, expr) => get_rap(expr, tcx, raps),
    ExprKind::Call(fn_expr, _) => {
      let fn_name = hirid_to_var_name(fn_expr.hir_id, tcx).unwrap();
      ResourceTy::Value(raps.get(&fn_name).unwrap().rap.to_owned())
    }
    ExprKind::MethodCall(name_and_generic_args, ..) => {
      let fn_name = hirid_to_var_name(name_and_generic_args.hir_id, &tcx).unwrap();
      ResourceTy::Value(raps.get(&fn_name).unwrap().rap.to_owned())
    }
    ExprKind::Block(b, _) => {
      match b.expr {
        Some(expr) => { get_rap(expr, tcx, raps) }
        None => { panic!("invalid expr for getting rap") }
      }
    }
    ExprKind::Field(expr, ident) => {
      match expr {
        Expr{kind: ExprKind::Path(QPath::Resolved(_,p)), ..} => {
          let name = tcx.hir_name(p.segments[0].hir_id).as_str().to_owned();
          let field_name: String = ident.as_str().to_owned();
          let total_name = format!("{}.{}", name, field_name);
          ResourceTy::Value(raps.get(&total_name).unwrap().rap.to_owned())
        }
        _ => { panic!("unexpected field expr") }
      } 
    }
    _ => ResourceTy::Anonymous
  }
}

// Almost the same as get_rap but we don't care about any anonymous owners 
// which is why we return a RAP instead of a ResourceTy
pub fn fetch_rap(expr: &Expr, tcx: &TyCtxt, raps: &HashMap<String, RapData>) -> Option<ResourceAccessPoint> {
  match expr.kind {
    ExprKind::Call(..) | ExprKind::Binary(..) | ExprKind::Lit(_) | ExprKind::MethodCall(..) => None,
    ExprKind::Path(QPath::Resolved(_,p)) => {
      let name = tcx.hir_name(p.segments[0].hir_id).as_str().to_owned();
      Some(raps.get(&name).unwrap().rap.to_owned())
    }
    ExprKind::AddrOf(_, _, expr) | ExprKind::Unary(_, expr) => fetch_rap(expr, tcx, raps),
    ExprKind::Block(b, _) => {
      match b.expr {
        Some(expr) => { fetch_rap(expr, tcx, raps) }
        None => { panic!("invalid expr for fetching rap") }
      }
    }
    ExprKind::Field(expr, ident) => {
      match expr {
        Expr{kind: ExprKind::Path(QPath::Resolved(_,p)), ..} => {
          let name = tcx.hir_name(p.segments[0].hir_id).as_str().to_owned();
          let field_name: String = ident.as_str().to_owned();
          let total_name = format!("{}.{}", name, field_name);
          Some(raps.get(&total_name).unwrap().rap.to_owned())
        }
        _ => { panic!("unexpected field expr") }
      } 
    }
    _ => None
  }
}


// used to find the lender from the rhs of a let expr
// ex: let a = &y; (y is the lender)
// it gets a little interesting when rhs involves a * operator
pub fn find_lender(rhs: &Expr, tcx: &TyCtxt, raps: &HashMap<String, RapData>, borrow_map: &HashMap<String, RefData>) -> ResourceTy {
  match rhs.kind {
    ExprKind::Path(QPath::Resolved(_,p)) => {
      let name = tcx.hir_name(p.segments[0].hir_id).as_str().to_owned();
      if borrow_map.contains_key(&name) {
        borrow_map.get(&name).unwrap().to_owned().lender
      }
      else{
        ResourceTy::Value(raps.get(&name).unwrap().rap.to_owned())
      }
    }
    // For method-call chains that return a reference, the lender is
    // the chain's *base* receiver, not the outer call. Walk down
    // through `.method().method()` to the leftmost receiver and try
    // resolving that as the lender (it's commonly a Path or Field).
    // If we can't resolve it (e.g. the chain bottoms out in a
    // literal or another call), fall back to Anonymous as before.
    ExprKind::MethodCall(_, recv, _, _) => {
      find_lender(recv, tcx, raps, borrow_map)
    }
    ExprKind::Call(..) | ExprKind::Lit(_) => {
      ResourceTy::Anonymous
    }
    ExprKind::AddrOf(_, _, expr) => {
      find_lender(expr, tcx, raps, borrow_map)
    }
    ExprKind::Block(b, _) => {
      match b.expr {
        Some(expr) => { find_lender(expr, tcx, raps, borrow_map) }
        None => { panic!("invalid rhs lender block") }
      }
    }
    ExprKind::Unary(op, expr) => { 
      match op {
        rustc_hir::UnOp::Deref => {
          match find_lender(expr, tcx, raps, borrow_map) {
            ResourceTy::Deref(r) | ResourceTy::Value(r) => {
              borrow_map.get(r.name()).unwrap().to_owned().lender
            }
            _ => { ResourceTy::Anonymous }
          }
        },
        _ => {
          find_lender(expr, tcx, raps, borrow_map)
        }
      }
    }
    ExprKind::Field(expr, ident) => {
      match expr {
        Expr{kind: ExprKind::Path(QPath::Resolved(_,p)), ..} => {
          let mut name = tcx.hir_name(p.segments[0].hir_id).as_str().to_owned();
          name = format!("{}.{}", name, ident.as_str());
          if borrow_map.contains_key(&name) {
            borrow_map.get(&name).unwrap().to_owned().lender
          }
          else{
            ResourceTy::Value(raps.get(&name).unwrap().rap.to_owned())
          }
        }
        _ => { panic!("unexpected field expr") }
      } 
    }
  _ => { panic!("unexpected rhs lender expr {:#?}", rhs) }
  }
}