11use crate :: borrow_check:: location:: { LocationIndex , LocationTable } ;
2+ use crate :: dataflow:: indexes:: MovePathIndex ;
3+ use crate :: dataflow:: move_paths:: { LookupResult , MoveData } ;
24use 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 } ;
57use rustc:: ty:: subst:: Kind ;
68use rustc:: ty:: Ty ;
79
810use super :: TypeChecker ;
911
1012type VarPointRelations = Vec < ( Local , LocationIndex ) > ;
13+ type MovePathPointRelations = Vec < ( MovePathIndex , LocationIndex ) > ;
1114
12- struct LivenessPointFactsExtractor < ' me > {
15+ struct UseFactsExtractor < ' me > {
1316 var_defined : & ' me mut VarPointRelations ,
1417 var_used : & ' me mut VarPointRelations ,
1518 location_table : & ' me LocationTable ,
19+ var_drop_used : & ' me mut VarPointRelations ,
20+ move_data : & ' me MoveData < ' me > ,
21+ path_accessed_at : & ' me mut MovePathPointRelations ,
1622}
1723
1824// A Visitor to walk through the MIR and extract point-wise facts
19- impl LivenessPointFactsExtractor < ' _ > {
25+ impl UseFactsExtractor < ' _ > {
2026 fn location_to_index ( & self , location : Location ) -> LocationIndex {
2127 self . location_table . mid_index ( location)
2228 }
@@ -30,15 +36,50 @@ impl LivenessPointFactsExtractor<'_> {
3036 debug ! ( "LivenessFactsExtractor::insert_use()" ) ;
3137 self . var_used . push ( ( local, self . location_to_index ( location) ) ) ;
3238 }
39+
40+ fn insert_drop_use ( & mut self , local : Local , location : Location ) {
41+ debug ! ( "LivenessFactsExtractor::insert_drop_use()" ) ;
42+ self . var_drop_used . push ( ( local, self . location_to_index ( location) ) ) ;
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+ }
3356}
3457
35- impl Visitor < ' tcx > for LivenessPointFactsExtractor < ' _ > {
58+ impl Visitor < ' tcx > for UseFactsExtractor < ' _ > {
3659 fn visit_local ( & mut self , & local: & Local , context : PlaceContext , location : Location ) {
3760 match categorize ( context) {
3861 Some ( DefUse :: Def ) => self . insert_def ( local, location) ,
3962 Some ( DefUse :: Use ) => self . insert_use ( local, location) ,
63+ Some ( DefUse :: Drop ) => self . insert_drop_use ( local, location) ,
64+ _ => ( ) ,
65+ }
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+ }
4082 _ => ( ) ,
41- // NOTE: Drop handling is now done in trace()
4283 }
4384 }
4485}
@@ -54,23 +95,27 @@ fn add_var_uses_regions(typeck: &mut TypeChecker<'_, 'tcx>, local: Local, ty: Ty
5495 } ) ;
5596}
5697
57- pub ( super ) fn populate_var_liveness_facts (
98+ pub ( super ) fn populate_access_facts (
5899 typeck : & mut TypeChecker < ' _ , ' tcx > ,
59- mir : & Body < ' tcx > ,
100+ body : & Body < ' tcx > ,
60101 location_table : & LocationTable ,
102+ move_data : & MoveData < ' _ > ,
61103) {
62104 debug ! ( "populate_var_liveness_facts()" ) ;
63105
64106 if let Some ( facts) = typeck. borrowck_context . all_facts . as_mut ( ) {
65- LivenessPointFactsExtractor {
107+ UseFactsExtractor {
66108 var_defined : & mut facts. var_defined ,
67109 var_used : & mut facts. var_used ,
110+ var_drop_used : & mut facts. var_drop_used ,
111+ path_accessed_at : & mut facts. path_accessed_at ,
68112 location_table,
113+ move_data,
69114 }
70- . visit_body ( mir ) ;
115+ . visit_body ( body ) ;
71116 }
72117
73- for ( local, local_decl) in mir . local_decls . iter_enumerated ( ) {
118+ for ( local, local_decl) in body . local_decls . iter_enumerated ( ) {
74119 add_var_uses_regions ( typeck, local, local_decl. ty ) ;
75120 }
76121}
0 commit comments