8
8
// option. This file may not be copied, modified, or distributed
9
9
// except according to those terms.
10
10
11
+ use borrow_check:: borrow_set:: { BorrowSet , BorrowData } ;
12
+
11
13
use rustc;
12
14
use rustc:: hir;
13
15
use rustc:: hir:: def_id:: DefId ;
@@ -32,7 +34,6 @@ use borrow_check::nll::ToRegionVid;
32
34
33
35
use syntax_pos:: Span ;
34
36
35
- use std:: fmt;
36
37
use std:: hash:: Hash ;
37
38
use std:: rc:: Rc ;
38
39
@@ -49,69 +50,12 @@ pub struct Borrows<'a, 'gcx: 'tcx, 'tcx: 'a> {
49
50
scope_tree : Lrc < region:: ScopeTree > ,
50
51
root_scope : Option < region:: Scope > ,
51
52
52
- /// The fundamental map relating bitvector indexes to the borrows
53
- /// in the MIR.
54
- borrows : IndexVec < BorrowIndex , BorrowData < ' tcx > > ,
55
-
56
- /// Each borrow is also uniquely identified in the MIR by the
57
- /// `Location` of the assignment statement in which it appears on
58
- /// the right hand side; we map each such location to the
59
- /// corresponding `BorrowIndex`.
60
- location_map : FxHashMap < Location , BorrowIndex > ,
61
-
62
- /// Locations which activate borrows.
63
- activation_map : FxHashMap < Location , FxHashSet < BorrowIndex > > ,
64
-
65
- /// Every borrow has a region; this maps each such regions back to
66
- /// its borrow-indexes.
67
- region_map : FxHashMap < Region < ' tcx > , FxHashSet < BorrowIndex > > ,
68
-
69
- /// Map from local to all the borrows on that local
70
- local_map : FxHashMap < mir:: Local , FxHashSet < BorrowIndex > > ,
71
-
72
- /// Maps regions to their corresponding source spans
73
- /// Only contains ReScope()s as keys
74
- region_span_map : FxHashMap < RegionKind , Span > ,
53
+ borrow_set : BorrowSet < ' tcx > ,
75
54
76
55
/// NLL region inference context with which NLL queries should be resolved
77
56
nonlexical_regioncx : Option < Rc < RegionInferenceContext < ' tcx > > > ,
78
57
}
79
58
80
- // temporarily allow some dead fields: `kind` and `region` will be
81
- // needed by borrowck; `borrowed_place` will probably be a MovePathIndex when
82
- // that is extended to include borrowed data paths.
83
- #[ allow( dead_code) ]
84
- #[ derive( Debug ) ]
85
- pub struct BorrowData < ' tcx > {
86
- /// Location where the borrow reservation starts.
87
- /// In many cases, this will be equal to the activation location but not always.
88
- pub ( crate ) reserve_location : Location ,
89
- /// Location where the borrow is activated. None if this is not a
90
- /// 2-phase borrow.
91
- pub ( crate ) activation_location : Option < Location > ,
92
- /// What kind of borrow this is
93
- pub ( crate ) kind : mir:: BorrowKind ,
94
- /// The region for which this borrow is live
95
- pub ( crate ) region : Region < ' tcx > ,
96
- /// Place from which we are borrowing
97
- pub ( crate ) borrowed_place : mir:: Place < ' tcx > ,
98
- /// Place to which the borrow was stored
99
- pub ( crate ) assigned_place : mir:: Place < ' tcx > ,
100
- }
101
-
102
- impl < ' tcx > fmt:: Display for BorrowData < ' tcx > {
103
- fn fmt ( & self , w : & mut fmt:: Formatter ) -> fmt:: Result {
104
- let kind = match self . kind {
105
- mir:: BorrowKind :: Shared => "" ,
106
- mir:: BorrowKind :: Unique => "uniq " ,
107
- mir:: BorrowKind :: Mut { .. } => "mut " ,
108
- } ;
109
- let region = format ! ( "{}" , self . region) ;
110
- let region = if region. len ( ) > 0 { format ! ( "{} " , region) } else { region } ;
111
- write ! ( w, "&{}{}{:?}" , region, kind, self . borrowed_place)
112
- }
113
- }
114
-
115
59
impl ReserveOrActivateIndex {
116
60
fn reserved ( i : BorrowIndex ) -> Self { ReserveOrActivateIndex :: new ( i. index ( ) * 2 ) }
117
61
fn active ( i : BorrowIndex ) -> Self { ReserveOrActivateIndex :: new ( ( i. index ( ) * 2 ) + 1 ) }
@@ -169,14 +113,16 @@ impl<'a, 'gcx, 'tcx> Borrows<'a, 'gcx, 'tcx> {
169
113
170
114
return Borrows { tcx : tcx,
171
115
mir : mir,
172
- borrows : visitor. idx_vec ,
116
+ borrow_set : BorrowSet {
117
+ borrows : visitor. idx_vec ,
118
+ location_map : visitor. location_map ,
119
+ activation_map : visitor. activation_map ,
120
+ region_map : visitor. region_map ,
121
+ local_map : visitor. local_map ,
122
+ region_span_map : visitor. region_span_map ,
123
+ } ,
173
124
scope_tree,
174
125
root_scope,
175
- location_map : visitor. location_map ,
176
- activation_map : visitor. activation_map ,
177
- region_map : visitor. region_map ,
178
- local_map : visitor. local_map ,
179
- region_span_map : visitor. region_span_map ,
180
126
nonlexical_regioncx } ;
181
127
182
128
struct GatherBorrows < ' a , ' gcx : ' tcx , ' tcx : ' a > {
@@ -514,20 +460,20 @@ impl<'a, 'gcx, 'tcx> Borrows<'a, 'gcx, 'tcx> {
514
460
match self . nonlexical_regioncx {
515
461
Some ( _) => None ,
516
462
None => {
517
- match self . region_span_map . get ( region) {
463
+ match self . borrow_set . region_span_map . get ( region) {
518
464
Some ( span) => Some ( self . tcx . sess . codemap ( ) . end_point ( * span) ) ,
519
465
None => Some ( self . tcx . sess . codemap ( ) . end_point ( self . mir . span ) )
520
466
}
521
467
}
522
468
}
523
469
}
524
470
525
- pub fn borrows ( & self ) -> & IndexVec < BorrowIndex , BorrowData < ' tcx > > { & self . borrows }
471
+ crate fn borrows ( & self ) -> & IndexVec < BorrowIndex , BorrowData < ' tcx > > { & self . borrow_set . borrows }
526
472
527
473
pub fn scope_tree ( & self ) -> & Lrc < region:: ScopeTree > { & self . scope_tree }
528
474
529
475
pub fn location ( & self , idx : BorrowIndex ) -> & Location {
530
- & self . borrows [ idx] . reserve_location
476
+ & self . borrow_set . borrows [ idx] . reserve_location
531
477
}
532
478
533
479
/// Add all borrows to the kill set, if those borrows are out of scope at `location`.
@@ -548,7 +494,7 @@ impl<'a, 'gcx, 'tcx> Borrows<'a, 'gcx, 'tcx> {
548
494
// terminator *does* introduce a new loan of the same
549
495
// region, then setting that gen-bit will override any
550
496
// potential kill introduced here.
551
- for ( borrow_index, borrow_data) in self . borrows . iter_enumerated ( ) {
497
+ for ( borrow_index, borrow_data) in self . borrow_set . borrows . iter_enumerated ( ) {
552
498
let borrow_region = borrow_data. region . to_region_vid ( ) ;
553
499
if !regioncx. region_contains_point ( borrow_region, location) {
554
500
sets. kill ( & ReserveOrActivateIndex :: reserved ( borrow_index) ) ;
@@ -562,7 +508,7 @@ impl<'a, 'gcx, 'tcx> Borrows<'a, 'gcx, 'tcx> {
562
508
sets : & mut BlockSets < ReserveOrActivateIndex > ,
563
509
local : & rustc:: mir:: Local )
564
510
{
565
- if let Some ( borrow_indexes) = self . local_map . get ( local) {
511
+ if let Some ( borrow_indexes) = self . borrow_set . local_map . get ( local) {
566
512
sets. kill_all ( borrow_indexes. iter ( )
567
513
. map ( |b| ReserveOrActivateIndex :: reserved ( * b) ) ) ;
568
514
sets. kill_all ( borrow_indexes. iter ( )
@@ -575,7 +521,7 @@ impl<'a, 'gcx, 'tcx> Borrows<'a, 'gcx, 'tcx> {
575
521
sets : & mut BlockSets < ReserveOrActivateIndex > ,
576
522
location : Location ) {
577
523
// Handle activations
578
- match self . activation_map . get ( & location) {
524
+ match self . borrow_set . activation_map . get ( & location) {
579
525
Some ( activations) => {
580
526
for activated in activations {
581
527
debug ! ( "activating borrow {:?}" , activated) ;
@@ -591,7 +537,7 @@ impl<'a, 'gcx, 'tcx> BitDenotation for Borrows<'a, 'gcx, 'tcx> {
591
537
type Idx = ReserveOrActivateIndex ;
592
538
fn name ( ) -> & ' static str { "borrows" }
593
539
fn bits_per_block ( & self ) -> usize {
594
- self . borrows . len ( ) * 2
540
+ self . borrow_set . borrows . len ( ) * 2
595
541
}
596
542
597
543
fn start_block_effect ( & self , _entry_set : & mut IdxSet < ReserveOrActivateIndex > ) {
@@ -623,7 +569,9 @@ impl<'a, 'gcx, 'tcx> BitDenotation for Borrows<'a, 'gcx, 'tcx> {
623
569
match stmt. kind {
624
570
// EndRegion kills any borrows (reservations and active borrows both)
625
571
mir:: StatementKind :: EndRegion ( region_scope) => {
626
- if let Some ( borrow_indexes) = self . region_map . get ( & ReScope ( region_scope) ) {
572
+ if let Some ( borrow_indexes) =
573
+ self . borrow_set . region_map . get ( & ReScope ( region_scope) )
574
+ {
627
575
assert ! ( self . nonlexical_regioncx. is_none( ) ) ;
628
576
for idx in borrow_indexes {
629
577
sets. kill ( & ReserveOrActivateIndex :: reserved ( * idx) ) ;
@@ -650,7 +598,7 @@ impl<'a, 'gcx, 'tcx> BitDenotation for Borrows<'a, 'gcx, 'tcx> {
650
598
651
599
if let mir:: Rvalue :: Ref ( region, _, ref place) = * rhs {
652
600
if is_unsafe_place ( self . tcx , self . mir , place) { return ; }
653
- let index = self . location_map . get ( & location) . unwrap_or_else ( || {
601
+ let index = self . borrow_set . location_map . get ( & location) . unwrap_or_else ( || {
654
602
panic ! ( "could not find BorrowIndex for location {:?}" , location) ;
655
603
} ) ;
656
604
@@ -661,7 +609,7 @@ impl<'a, 'gcx, 'tcx> BitDenotation for Borrows<'a, 'gcx, 'tcx> {
661
609
return
662
610
}
663
611
664
- assert ! ( self . region_map. get( region) . unwrap_or_else( || {
612
+ assert ! ( self . borrow_set . region_map. get( region) . unwrap_or_else( || {
665
613
panic!( "could not find BorrowIndexs for region {:?}" , region) ;
666
614
} ) . contains( & index) ) ;
667
615
sets. gen ( & ReserveOrActivateIndex :: reserved ( * index) ) ;
@@ -739,7 +687,7 @@ impl<'a, 'gcx, 'tcx> BitDenotation for Borrows<'a, 'gcx, 'tcx> {
739
687
// and hence most of these loans will already be dead -- but, in some cases
740
688
// like unwind paths, we do not always emit `EndRegion` statements, so we
741
689
// add some kills here as a "backup" and to avoid spurious error messages.
742
- for ( borrow_index, borrow_data) in self . borrows . iter_enumerated ( ) {
690
+ for ( borrow_index, borrow_data) in self . borrow_set . borrows . iter_enumerated ( ) {
743
691
if let ReScope ( scope) = borrow_data. region {
744
692
// Check that the scope is not actually a scope from a function that is
745
693
// a parent of our closure. Note that the CallSite scope itself is
0 commit comments