99// except according to those terms.
1010
1111use borrow_check:: borrow_set:: BorrowData ;
12- use borrow_check:: nll:: region_infer:: { Cause , RegionInferenceContext } ;
12+ use borrow_check:: nll:: region_infer:: Cause ;
1313use borrow_check:: { Context , MirBorrowckCtxt , WriteKind } ;
14- use rustc:: mir:: visit:: { MirVisitable , PlaceContext , Visitor } ;
15- use rustc:: mir:: { Local , Location , Mir , Place } ;
16- use rustc_data_structures:: fx:: FxHashSet ;
14+ use rustc:: mir:: Place ;
1715use rustc_errors:: DiagnosticBuilder ;
18- use util:: liveness:: { self , DefUse , LivenessMode } ;
16+
17+ mod find_use;
1918
2019impl < ' cx , ' gcx , ' tcx > MirBorrowckCtxt < ' cx , ' gcx , ' tcx > {
2120 /// Adds annotations to `err` explaining *why* the borrow contains the
@@ -45,7 +44,7 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
4544 let borrow_region_vid = regioncx. to_region_vid ( borrow. region ) ;
4645 if let Some ( cause) = regioncx. why_region_contains_point ( borrow_region_vid, context. loc ) {
4746 match cause {
48- Cause :: LiveVar ( local, location) => match find_regular_use (
47+ Cause :: LiveVar ( local, location) => match find_use :: regular_use (
4948 mir, regioncx, borrow, location, local,
5049 ) {
5150 Some ( p) => {
@@ -60,7 +59,7 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
6059 }
6160 } ,
6261
63- Cause :: DropVar ( local, location) => match find_drop_use (
62+ Cause :: DropVar ( local, location) => match find_use :: drop_use (
6463 mir, regioncx, borrow, location, local,
6564 ) {
6665 Some ( p) => match & mir. local_decls [ local] . name {
@@ -124,129 +123,3 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
124123 }
125124 }
126125}
127-
128- fn find_regular_use < ' gcx , ' tcx > (
129- mir : & ' gcx Mir ,
130- regioncx : & ' tcx RegionInferenceContext ,
131- borrow : & ' tcx BorrowData ,
132- start_point : Location ,
133- local : Local ,
134- ) -> Option < Location > {
135- let mut uf = UseFinder {
136- mir,
137- regioncx,
138- borrow,
139- start_point,
140- local,
141- liveness_mode : LivenessMode {
142- include_regular_use : true ,
143- include_drops : false ,
144- } ,
145- } ;
146-
147- uf. find ( )
148- }
149-
150- fn find_drop_use < ' gcx , ' tcx > (
151- mir : & ' gcx Mir ,
152- regioncx : & ' tcx RegionInferenceContext ,
153- borrow : & ' tcx BorrowData ,
154- start_point : Location ,
155- local : Local ,
156- ) -> Option < Location > {
157- let mut uf = UseFinder {
158- mir,
159- regioncx,
160- borrow,
161- start_point,
162- local,
163- liveness_mode : LivenessMode {
164- include_regular_use : false ,
165- include_drops : true ,
166- } ,
167- } ;
168-
169- uf. find ( )
170- }
171-
172- struct UseFinder < ' gcx , ' tcx > {
173- mir : & ' gcx Mir < ' gcx > ,
174- regioncx : & ' tcx RegionInferenceContext < ' tcx > ,
175- borrow : & ' tcx BorrowData < ' tcx > ,
176- start_point : Location ,
177- local : Local ,
178- liveness_mode : LivenessMode ,
179- }
180-
181- impl < ' gcx , ' tcx > UseFinder < ' gcx , ' tcx > {
182- fn find ( & mut self ) -> Option < Location > {
183- let mut stack = vec ! [ ] ;
184- let mut visited = FxHashSet ( ) ;
185-
186- stack. push ( self . start_point ) ;
187- while let Some ( p) = stack. pop ( ) {
188- if !self . regioncx . region_contains_point ( self . borrow . region , p) {
189- continue ;
190- }
191-
192- if !visited. insert ( p) {
193- continue ;
194- }
195-
196- let block_data = & self . mir [ p. block ] ;
197- let ( defined, used) = self . def_use ( p, block_data. visitable ( p. statement_index ) ) ;
198-
199- if used {
200- return Some ( p) ;
201- } else if !defined {
202- if p. statement_index < block_data. statements . len ( ) {
203- stack. push ( Location {
204- statement_index : p. statement_index + 1 ,
205- ..p
206- } ) ;
207- } else {
208- stack. extend ( block_data. terminator ( ) . successors ( ) . map ( |& basic_block| {
209- Location {
210- statement_index : 0 ,
211- block : basic_block,
212- }
213- } ) ) ;
214- }
215- }
216- }
217-
218- None
219- }
220-
221- fn def_use ( & self , location : Location , thing : & dyn MirVisitable < ' tcx > ) -> ( bool , bool ) {
222- let mut visitor = DefUseVisitor {
223- defined : false ,
224- used : false ,
225- local : self . local ,
226- liveness_mode : self . liveness_mode ,
227- } ;
228-
229- thing. apply ( location, & mut visitor) ;
230-
231- ( visitor. defined , visitor. used )
232- }
233- }
234-
235- struct DefUseVisitor {
236- defined : bool ,
237- used : bool ,
238- local : Local ,
239- liveness_mode : LivenessMode ,
240- }
241-
242- impl < ' tcx > Visitor < ' tcx > for DefUseVisitor {
243- fn visit_local ( & mut self , & local: & Local , context : PlaceContext < ' tcx > , _: Location ) {
244- if local == self . local {
245- match liveness:: categorize ( context, self . liveness_mode ) {
246- Some ( DefUse :: Def ) => self . defined = true ,
247- Some ( DefUse :: Use ) => self . used = true ,
248- None => ( ) ,
249- }
250- }
251- }
252- }
0 commit comments