@@ -253,19 +253,52 @@ impl<'a, 'tcx> Visitor<'tcx> for GatherBorrows<'a, 'tcx> {
253253 }
254254
255255 let region = region. as_var ( ) ;
256-
257- let borrow = BorrowData {
256+ let borrow = |activation_location| BorrowData {
258257 kind,
259258 region,
260259 reserve_location : location,
261- activation_location : TwoPhaseActivation :: NotTwoPhase ,
260+ activation_location,
262261 borrowed_place,
263262 assigned_place : * assigned_place,
264263 } ;
265- let ( idx, _) = self . location_map . insert_full ( location, borrow) ;
266- let idx = BorrowIndex :: from ( idx) ;
267264
268- self . insert_as_pending_if_two_phase ( location, assigned_place, kind, idx) ;
265+ let idx = if !kind. is_two_phase_borrow ( ) {
266+ debug ! ( " -> {:?}" , location) ;
267+ let ( idx, _) = self
268+ . location_map
269+ . insert_full ( location, borrow ( TwoPhaseActivation :: NotTwoPhase ) ) ;
270+ BorrowIndex :: from ( idx)
271+ } else {
272+ // When we encounter a 2-phase borrow statement, it will always
273+ // be assigning into a temporary TEMP:
274+ //
275+ // TEMP = &foo
276+ //
277+ // so extract `temp`.
278+ let Some ( temp) = assigned_place. as_local ( ) else {
279+ span_bug ! (
280+ self . body. source_info( location) . span,
281+ "expected 2-phase borrow to assign to a local, not `{:?}`" ,
282+ assigned_place,
283+ ) ;
284+ } ;
285+
286+ // Consider the borrow not activated to start. When we find an activation, we'll update
287+ // this field.
288+ let ( idx, _) = self
289+ . location_map
290+ . insert_full ( location, borrow ( TwoPhaseActivation :: NotActivated ) ) ;
291+ let idx = BorrowIndex :: from ( idx) ;
292+
293+ // Insert `temp` into the list of pending activations. From
294+ // now on, we'll be on the lookout for a use of it. Note that
295+ // we are guaranteed that this use will come after the
296+ // assignment.
297+ let prev = self . pending_activations . insert ( temp, idx) ;
298+ assert_eq ! ( prev, None , "temporary associated with multiple two phase borrows" ) ;
299+
300+ idx
301+ } ;
269302
270303 self . local_map . entry ( borrowed_place. local ) . or_default ( ) . insert ( idx) ;
271304 }
@@ -334,62 +367,3 @@ impl<'a, 'tcx> Visitor<'tcx> for GatherBorrows<'a, 'tcx> {
334367 self . super_rvalue ( rvalue, location)
335368 }
336369}
337-
338- impl < ' a , ' tcx > GatherBorrows < ' a , ' tcx > {
339- /// If this is a two-phase borrow, then we will record it
340- /// as "pending" until we find the activating use.
341- fn insert_as_pending_if_two_phase (
342- & mut self ,
343- start_location : Location ,
344- assigned_place : & mir:: Place < ' tcx > ,
345- kind : mir:: BorrowKind ,
346- borrow_index : BorrowIndex ,
347- ) {
348- debug ! (
349- "Borrows::insert_as_pending_if_two_phase({:?}, {:?}, {:?})" ,
350- start_location, assigned_place, borrow_index,
351- ) ;
352-
353- if !kind. allows_two_phase_borrow ( ) {
354- debug ! ( " -> {:?}" , start_location) ;
355- return ;
356- }
357-
358- // When we encounter a 2-phase borrow statement, it will always
359- // be assigning into a temporary TEMP:
360- //
361- // TEMP = &foo
362- //
363- // so extract `temp`.
364- let Some ( temp) = assigned_place. as_local ( ) else {
365- span_bug ! (
366- self . body. source_info( start_location) . span,
367- "expected 2-phase borrow to assign to a local, not `{:?}`" ,
368- assigned_place,
369- ) ;
370- } ;
371-
372- // Consider the borrow not activated to start. When we find an activation, we'll update
373- // this field.
374- {
375- let borrow_data = & mut self . location_map [ borrow_index. as_usize ( ) ] ;
376- borrow_data. activation_location = TwoPhaseActivation :: NotActivated ;
377- }
378-
379- // Insert `temp` into the list of pending activations. From
380- // now on, we'll be on the lookout for a use of it. Note that
381- // we are guaranteed that this use will come after the
382- // assignment.
383- let old_value = self . pending_activations . insert ( temp, borrow_index) ;
384- if let Some ( old_index) = old_value {
385- span_bug ! (
386- self . body. source_info( start_location) . span,
387- "found already pending activation for temp: {:?} \
388- at borrow_index: {:?} with associated data {:?}",
389- temp,
390- old_index,
391- self . location_map[ old_index. as_usize( ) ]
392- ) ;
393- }
394- }
395- }
0 commit comments