@@ -265,7 +265,7 @@ impl<'a, 'tcx> Visitor<'tcx> for BorrowDependencies<'a, 'tcx> {
265
265
src_node_idx,
266
266
self . dep_graph. node( src_node_idx) . data,
267
267
node_idx,
268
- borrowed_local
268
+ place . local
269
269
) ;
270
270
271
271
self . dep_graph . add_edge ( src_node_idx, node_idx, ( ) ) ;
@@ -359,42 +359,8 @@ impl<'mir, 'tcx> BorrowedLocalsResults<'mir, 'tcx>
359
359
where
360
360
' tcx : ' mir ,
361
361
{
362
- fn new (
363
- tcx : TyCtxt < ' tcx > ,
364
- body : & ' mir Body < ' tcx > ,
365
- borrows_analysis_results : Results < ' tcx , LiveBorrows < ' mir , ' tcx > > ,
366
- ) -> Self {
367
- let mut borrow_deps = BorrowDependencies :: new ( body. local_decls ( ) , tcx) ;
368
- borrow_deps. visit_body ( body) ;
369
-
370
- if cfg ! ( debug_assertions) {
371
- let dep_graph = & borrow_deps. dep_graph ;
372
-
373
- debug ! (
374
- "nodes: {:#?}" ,
375
- dep_graph
376
- . all_nodes( )
377
- . clone( )
378
- . into_iter( )
379
- . enumerate( )
380
- . map( |( i, node) | ( i, node. data) )
381
- . collect:: <Vec <_>>( )
382
- ) ;
383
-
384
- debug ! ( "edges:" ) ;
385
- for edge in dep_graph. all_edges ( ) {
386
- let src_node_idx = edge. source ( ) ;
387
- let src_node = dep_graph. node ( src_node_idx) ;
388
- let target_node_idx = edge. target ( ) ;
389
- let target_node = dep_graph. node ( target_node_idx) ;
390
- debug ! (
391
- "{:?}({:?}) -> {:?}({:?}) ({:?})" ,
392
- src_node_idx, src_node. data, target_node_idx, target_node. data, edge. data
393
- )
394
- }
395
- }
396
-
397
- let dep_graph = & borrow_deps. dep_graph ;
362
+ fn new ( borrows_analysis_results : Results < ' tcx , LiveBorrows < ' mir , ' tcx > > ) -> Self {
363
+ let dep_graph = & borrows_analysis_results. analysis . borrow_deps . dep_graph ;
398
364
let borrowed_local_to_locals_to_keep_alive = Self :: get_locals_to_keep_alive_map ( dep_graph) ;
399
365
Self { borrows_analysis_results, borrowed_local_to_locals_to_keep_alive }
400
366
}
@@ -467,11 +433,41 @@ pub fn get_borrowed_locals_results<'mir, 'tcx>(
467
433
tcx : TyCtxt < ' tcx > ,
468
434
) -> BorrowedLocalsResults < ' mir , ' tcx > {
469
435
debug ! ( "body: {:#?}" , body) ;
470
- let live_borrows = LiveBorrows :: new ( body, tcx) ;
436
+
437
+ let mut borrow_deps = BorrowDependencies :: new ( body. local_decls ( ) , tcx) ;
438
+ borrow_deps. visit_body ( body) ;
439
+
440
+ if cfg ! ( debug_assertions) {
441
+ let dep_graph = & borrow_deps. dep_graph ;
442
+
443
+ debug ! (
444
+ "nodes: {:#?}" ,
445
+ dep_graph
446
+ . all_nodes( )
447
+ . clone( )
448
+ . into_iter( )
449
+ . enumerate( )
450
+ . map( |( i, node) | ( i, node. data) )
451
+ . collect:: <Vec <_>>( )
452
+ ) ;
453
+
454
+ debug ! ( "edges:" ) ;
455
+ for edge in dep_graph. all_edges ( ) {
456
+ let src_node_idx = edge. source ( ) ;
457
+ let src_node = dep_graph. node ( src_node_idx) ;
458
+ let target_node_idx = edge. target ( ) ;
459
+ let target_node = dep_graph. node ( target_node_idx) ;
460
+ debug ! (
461
+ "{:?}({:?}) -> {:?}({:?}) ({:?})" ,
462
+ src_node_idx, src_node. data, target_node_idx, target_node. data, edge. data
463
+ )
464
+ }
465
+ }
466
+ let live_borrows = LiveBorrows :: new ( body, tcx, borrow_deps) ;
471
467
let results =
472
468
live_borrows. into_engine ( tcx, body) . pass_name ( "borrowed_locals" ) . iterate_to_fixpoint ( ) ;
473
469
474
- BorrowedLocalsResults :: new ( tcx , body , results)
470
+ BorrowedLocalsResults :: new ( results)
475
471
}
476
472
477
473
pub struct BorrowedLocalsResultsCursor < ' a , ' mir , ' tcx > {
@@ -544,18 +540,31 @@ impl<'a, 'mir, 'tcx> BorrowedLocalsResultsCursor<'a, 'mir, 'tcx> {
544
540
}
545
541
546
542
/// Performs a liveness analysis for borrows and raw pointers.
547
- pub struct LiveBorrows < ' a , ' tcx > {
548
- body : & ' a Body < ' tcx > ,
543
+ pub struct LiveBorrows < ' mir , ' tcx > {
544
+ body : & ' mir Body < ' tcx > ,
549
545
tcx : TyCtxt < ' tcx > ,
546
+ borrow_deps : BorrowDependencies < ' mir , ' tcx > ,
550
547
}
551
548
552
- impl < ' a , ' tcx > LiveBorrows < ' a , ' tcx > {
553
- fn new ( body : & ' a Body < ' tcx > , tcx : TyCtxt < ' tcx > ) -> Self {
554
- LiveBorrows { body, tcx }
549
+ impl < ' mir , ' tcx > LiveBorrows < ' mir , ' tcx > {
550
+ fn new (
551
+ body : & ' mir Body < ' tcx > ,
552
+ tcx : TyCtxt < ' tcx > ,
553
+ borrow_deps : BorrowDependencies < ' mir , ' tcx > ,
554
+ ) -> Self {
555
+ LiveBorrows { body, tcx, borrow_deps }
555
556
}
556
557
557
- fn transfer_function < ' b , T > ( & self , trans : & ' b mut T ) -> TransferFunction < ' a , ' b , ' tcx , T > {
558
- TransferFunction { body : self . body , tcx : self . tcx , _trans : trans }
558
+ fn transfer_function < ' b , T > (
559
+ & self ,
560
+ trans : & ' b mut T ,
561
+ ) -> TransferFunction < ' mir , ' b , ' _ , ' tcx , T > {
562
+ TransferFunction {
563
+ body : self . body ,
564
+ tcx : self . tcx ,
565
+ _trans : trans,
566
+ borrow_deps : & self . borrow_deps ,
567
+ }
559
568
}
560
569
}
561
570
@@ -607,13 +616,14 @@ impl<'a, 'tcx> GenKillAnalysis<'tcx> for LiveBorrows<'a, 'tcx> {
607
616
}
608
617
609
618
/// A `Visitor` that defines the transfer function for `MaybeBorrowedLocals`.
610
- struct TransferFunction < ' a , ' b , ' tcx , T > {
619
+ struct TransferFunction < ' a , ' b , ' c , ' tcx , T > {
611
620
body : & ' a Body < ' tcx > ,
612
621
tcx : TyCtxt < ' tcx > ,
613
622
_trans : & ' b mut T ,
623
+ borrow_deps : & ' c BorrowDependencies < ' a , ' tcx > ,
614
624
}
615
625
616
- impl < ' a , ' tcx , T > Visitor < ' tcx > for TransferFunction < ' a , ' _ , ' tcx , T >
626
+ impl < ' a , ' tcx , T > Visitor < ' tcx > for TransferFunction < ' a , ' _ , ' _ , ' tcx , T >
617
627
where
618
628
T : GenKill < Local > ,
619
629
{
@@ -670,9 +680,22 @@ where
670
680
match local_ty. kind ( ) {
671
681
ty:: Ref ( ..) | ty:: RawPtr ( ..) => {
672
682
debug ! ( "gen {:?}" , local) ;
673
- self . _trans . gen ( place. local ) ;
683
+ self . _trans . gen ( local) ;
684
+ }
685
+ _ => {
686
+ if let Some ( node_idx) = self . borrow_deps . locals_to_node_indexes . get ( & local) {
687
+ let node = self . borrow_deps . dep_graph . node ( * node_idx) ;
688
+
689
+ // these are `Local`s that contain references/pointers or are raw pointers
690
+ // that were assigned to raw pointers, which were cast to usize. Hence we
691
+ // need to treat them as uses of the references/pointers that they
692
+ // refer/correspond to.
693
+ if let NodeKind :: LocalWithRefs ( _) = node. data {
694
+ debug ! ( "gen {:?}" , local) ;
695
+ self . _trans . gen ( local) ;
696
+ }
697
+ }
674
698
}
675
- _ => { }
676
699
}
677
700
678
701
self . super_place ( place, context, location) ;
0 commit comments