1
1
use crate :: borrow_check:: location:: { LocationIndex , LocationTable } ;
2
+ use crate :: dataflow:: indexes:: MovePathIndex ;
3
+ use crate :: dataflow:: move_paths:: { LookupResult , MoveData } ;
2
4
use crate :: util:: liveness:: { categorize, DefUse } ;
3
- use rustc:: mir:: visit:: { PlaceContext , Visitor } ;
4
- use rustc:: mir:: { Body , Local , Location } ;
5
+ use rustc:: mir:: visit:: { MutatingUseContext , PlaceContext , Visitor } ;
6
+ use rustc:: mir:: { Body , Local , Location , Place } ;
5
7
use rustc:: ty:: subst:: Kind ;
6
8
use rustc:: ty:: Ty ;
7
9
8
10
use super :: TypeChecker ;
9
11
10
12
type VarPointRelations = Vec < ( Local , LocationIndex ) > ;
13
+ type MovePathPointRelations = Vec < ( MovePathIndex , LocationIndex ) > ;
11
14
12
- struct LivenessPointFactsExtractor < ' me > {
15
+ struct UseFactsExtractor < ' me > {
13
16
var_defined : & ' me mut VarPointRelations ,
14
17
var_used : & ' me mut VarPointRelations ,
15
18
location_table : & ' me LocationTable ,
16
19
var_drop_used : & ' me mut VarPointRelations ,
20
+ move_data : & ' me MoveData < ' me > ,
21
+ path_accessed_at : & ' me mut MovePathPointRelations ,
17
22
}
18
23
19
24
// A Visitor to walk through the MIR and extract point-wise facts
20
- impl LivenessPointFactsExtractor < ' _ > {
25
+ impl UseFactsExtractor < ' _ > {
21
26
fn location_to_index ( & self , location : Location ) -> LocationIndex {
22
27
self . location_table . mid_index ( location)
23
28
}
@@ -36,9 +41,21 @@ impl LivenessPointFactsExtractor<'_> {
36
41
debug ! ( "LivenessFactsExtractor::insert_drop_use()" ) ;
37
42
self . var_drop_used . push ( ( local, self . location_to_index ( location) ) ) ;
38
43
}
44
+
45
+ fn insert_path_access ( & mut self , path : MovePathIndex , location : Location ) {
46
+ debug ! ( "LivenessFactsExtractor::insert_path_access({:?}, {:?})" , path, location) ;
47
+ self . path_accessed_at . push ( ( path, self . location_to_index ( location) ) ) ;
48
+ }
49
+
50
+ fn place_to_mpi ( & self , place : & Place < ' _ > ) -> Option < MovePathIndex > {
51
+ match self . move_data . rev_lookup . find ( place. as_ref ( ) ) {
52
+ LookupResult :: Exact ( mpi) => Some ( mpi) ,
53
+ LookupResult :: Parent ( mmpi) => mmpi,
54
+ }
55
+ }
39
56
}
40
57
41
- impl Visitor < ' tcx > for LivenessPointFactsExtractor < ' _ > {
58
+ impl Visitor < ' tcx > for UseFactsExtractor < ' _ > {
42
59
fn visit_local ( & mut self , & local: & Local , context : PlaceContext , location : Location ) {
43
60
match categorize ( context) {
44
61
Some ( DefUse :: Def ) => self . insert_def ( local, location) ,
@@ -47,6 +64,24 @@ impl Visitor<'tcx> for LivenessPointFactsExtractor<'_> {
47
64
_ => ( ) ,
48
65
}
49
66
}
67
+
68
+ fn visit_place ( & mut self , place : & Place < ' tcx > , context : PlaceContext , location : Location ) {
69
+ self . super_place ( place, context, location) ;
70
+ match context {
71
+ PlaceContext :: NonMutatingUse ( _) => {
72
+ if let Some ( mpi) = self . place_to_mpi ( place) {
73
+ self . insert_path_access ( mpi, location) ;
74
+ }
75
+ }
76
+
77
+ PlaceContext :: MutatingUse ( MutatingUseContext :: Borrow ) => {
78
+ if let Some ( mpi) = self . place_to_mpi ( place) {
79
+ self . insert_path_access ( mpi, location) ;
80
+ }
81
+ }
82
+ _ => ( ) ,
83
+ }
84
+ }
50
85
}
51
86
52
87
fn add_var_uses_regions ( typeck : & mut TypeChecker < ' _ , ' tcx > , local : Local , ty : Ty < ' tcx > ) {
@@ -60,24 +95,27 @@ fn add_var_uses_regions(typeck: &mut TypeChecker<'_, 'tcx>, local: Local, ty: Ty
60
95
} ) ;
61
96
}
62
97
63
- pub ( super ) fn populate_var_liveness_facts (
98
+ pub ( super ) fn populate_access_facts (
64
99
typeck : & mut TypeChecker < ' _ , ' tcx > ,
65
- mir : & Body < ' tcx > ,
100
+ body : & Body < ' tcx > ,
66
101
location_table : & LocationTable ,
102
+ move_data : & MoveData < ' _ > ,
67
103
) {
68
104
debug ! ( "populate_var_liveness_facts()" ) ;
69
105
70
106
if let Some ( facts) = typeck. borrowck_context . all_facts . as_mut ( ) {
71
- LivenessPointFactsExtractor {
107
+ UseFactsExtractor {
72
108
var_defined : & mut facts. var_defined ,
73
109
var_used : & mut facts. var_used ,
74
110
var_drop_used : & mut facts. var_drop_used ,
111
+ path_accessed_at : & mut facts. path_accessed_at ,
75
112
location_table,
113
+ move_data,
76
114
}
77
- . visit_body ( mir ) ;
115
+ . visit_body ( body ) ;
78
116
}
79
117
80
- for ( local, local_decl) in mir . local_decls . iter_enumerated ( ) {
118
+ for ( local, local_decl) in body . local_decls . iter_enumerated ( ) {
81
119
add_var_uses_regions ( typeck, local, local_decl. ty ) ;
82
120
}
83
121
}
0 commit comments