@@ -123,9 +123,11 @@ impl<'tcx> QueryJob<'tcx> {
123123 let mut cycle = Vec :: new ( ) ;
124124
125125 while let Some ( job) = current_job {
126- cycle. insert ( 0 , job. info . clone ( ) ) ;
126+ cycle. push ( job. info . clone ( ) ) ;
127127
128128 if ptr:: eq ( & * job, self ) {
129+ cycle. reverse ( ) ;
130+
129131 // This is the end of the cycle
130132 // The span entry we included was for the usage
131133 // of the cycle itself, and not part of the cycle
@@ -368,13 +370,11 @@ fn remove_cycle<'tcx>(
368370 // Reverse the stack so earlier entries require later entries
369371 stack. reverse ( ) ;
370372
371- // Extract the spans and queries into separate arrays
372- let mut spans: Vec < _ > = stack. iter ( ) . map ( |e| e. 0 ) . collect ( ) ;
373- let queries = stack. into_iter ( ) . map ( |e| e. 1 ) ;
373+ // The stack is a vector of pairs of spans and queries
374+ let ( mut spans, queries) : ( Vec < _ > , Vec < _ > ) = stack. into_iter ( ) . unzip ( ) ;
374375
375376 // Shift the spans so that queries are matched with the span for their waitee
376- let last = spans. pop ( ) . unwrap ( ) ;
377- spans. insert ( 0 , last) ;
377+ spans. rotate_right ( 1 ) ;
378378
379379 // Zip them back together
380380 let mut stack: Vec < _ > = spans. into_iter ( ) . zip ( queries) . collect ( ) ;
@@ -414,10 +414,10 @@ fn remove_cycle<'tcx>(
414414 stable_hasher. finish ( )
415415 } ) . unwrap ( ) . as_ptr ( ) ;
416416
417- // Shift the stack until our entry point is first
418- while stack[ 0 ] . 1 . as_ptr ( ) != entry_point {
419- let last = stack . pop ( ) . unwrap ( ) ;
420- stack. insert ( 0 , last ) ;
417+ // Shift the stack so that our entry point is first
418+ let entry_point_pos = stack. iter ( ) . position ( | ( _ , query ) | query . as_ptr ( ) == entry_point) ;
419+ if let Some ( pos ) = entry_point_pos {
420+ stack. rotate_right ( pos ) ;
421421 }
422422
423423 // Create the cycle error
0 commit comments