1
+ use rustc_data_structures:: captures:: Captures ;
1
2
use rustc_data_structures:: graph:: dominators:: { self , Dominators } ;
2
3
use rustc_data_structures:: graph:: { self , GraphSuccessors , WithNumNodes , WithStartNode } ;
3
4
use rustc_index:: bit_set:: BitSet ;
4
5
use rustc_index:: { IndexSlice , IndexVec } ;
5
- use rustc_middle:: mir:: { self , BasicBlock , BasicBlockData , Terminator , TerminatorKind } ;
6
+ use rustc_middle:: mir:: { self , BasicBlock , Terminator , TerminatorKind } ;
6
7
7
8
use std:: cmp:: Ordering ;
8
9
use std:: ops:: { Index , IndexMut } ;
@@ -80,10 +81,9 @@ impl CoverageGraph {
80
81
// intentionally omits unwind paths.
81
82
// FIXME(#78544): MIR InstrumentCoverage: Improve coverage of `#[should_panic]` tests and
82
83
// `catch_unwind()` handlers.
83
- let mir_cfg_without_unwind = ShortCircuitPreorder :: new ( & mir_body, bcb_filtered_successors) ;
84
84
85
85
let mut basic_blocks = Vec :: new ( ) ;
86
- for ( bb , data ) in mir_cfg_without_unwind {
86
+ for bb in short_circuit_preorder ( mir_body , bcb_filtered_successors ) {
87
87
if let Some ( last) = basic_blocks. last ( ) {
88
88
let predecessors = & mir_body. basic_blocks . predecessors ( ) [ bb] ;
89
89
if predecessors. len ( ) > 1 || !predecessors. contains ( last) {
@@ -109,7 +109,7 @@ impl CoverageGraph {
109
109
}
110
110
basic_blocks. push ( bb) ;
111
111
112
- let term = data . terminator ( ) ;
112
+ let term = mir_body [ bb ] . terminator ( ) ;
113
113
114
114
match term. kind {
115
115
TerminatorKind :: Return { .. }
@@ -553,66 +553,28 @@ pub(super) fn find_loop_backedges(
553
553
backedges
554
554
}
555
555
556
- pub struct ShortCircuitPreorder <
557
- ' a ,
558
- ' tcx ,
559
- F : Fn ( & ' a mir:: Body < ' tcx > , & ' a TerminatorKind < ' tcx > ) -> Box < dyn Iterator < Item = BasicBlock > + ' a > ,
560
- > {
556
+ fn short_circuit_preorder < ' a , ' tcx , F , Iter > (
561
557
body : & ' a mir:: Body < ' tcx > ,
562
- visited : BitSet < BasicBlock > ,
563
- worklist : Vec < BasicBlock > ,
564
558
filtered_successors : F ,
565
- }
566
-
567
- impl <
568
- ' a ,
569
- ' tcx ,
570
- F : Fn ( & ' a mir:: Body < ' tcx > , & ' a TerminatorKind < ' tcx > ) -> Box < dyn Iterator < Item = BasicBlock > + ' a > ,
571
- > ShortCircuitPreorder < ' a , ' tcx , F >
572
- {
573
- pub fn new (
574
- body : & ' a mir:: Body < ' tcx > ,
575
- filtered_successors : F ,
576
- ) -> ShortCircuitPreorder < ' a , ' tcx , F > {
577
- let worklist = vec ! [ mir:: START_BLOCK ] ;
578
-
579
- ShortCircuitPreorder {
580
- body,
581
- visited : BitSet :: new_empty ( body. basic_blocks . len ( ) ) ,
582
- worklist,
583
- filtered_successors,
584
- }
585
- }
586
- }
587
-
588
- impl <
589
- ' a ,
590
- ' tcx ,
591
- F : Fn ( & ' a mir:: Body < ' tcx > , & ' a TerminatorKind < ' tcx > ) -> Box < dyn Iterator < Item = BasicBlock > + ' a > ,
592
- > Iterator for ShortCircuitPreorder < ' a , ' tcx , F >
559
+ ) -> impl Iterator < Item = BasicBlock > + Captures < ' a > + Captures < ' tcx >
560
+ where
561
+ F : Fn ( & ' a mir:: Body < ' tcx > , & ' a TerminatorKind < ' tcx > ) -> Iter ,
562
+ Iter : Iterator < Item = BasicBlock > ,
593
563
{
594
- type Item = ( BasicBlock , & ' a BasicBlockData < ' tcx > ) ;
564
+ let mut visited = BitSet :: new_empty ( body. basic_blocks . len ( ) ) ;
565
+ let mut worklist = vec ! [ mir:: START_BLOCK ] ;
595
566
596
- fn next ( & mut self ) -> Option < ( BasicBlock , & ' a BasicBlockData < ' tcx > ) > {
597
- while let Some ( idx ) = self . worklist . pop ( ) {
598
- if !self . visited . insert ( idx ) {
567
+ std :: iter :: from_fn ( move || {
568
+ while let Some ( bb ) = worklist. pop ( ) {
569
+ if !visited. insert ( bb ) {
599
570
continue ;
600
571
}
601
572
602
- let data = & self . body [ idx] ;
603
-
604
- if let Some ( ref term) = data. terminator {
605
- self . worklist . extend ( ( self . filtered_successors ) ( & self . body , & term. kind ) ) ;
606
- }
573
+ worklist. extend ( filtered_successors ( body, & body[ bb] . terminator ( ) . kind ) ) ;
607
574
608
- return Some ( ( idx , data ) ) ;
575
+ return Some ( bb ) ;
609
576
}
610
577
611
578
None
612
- }
613
-
614
- fn size_hint ( & self ) -> ( usize , Option < usize > ) {
615
- let size = self . body . basic_blocks . len ( ) - self . visited . count ( ) ;
616
- ( size, Some ( size) )
617
- }
579
+ } )
618
580
}
0 commit comments