@@ -592,97 +592,48 @@ pub fn build_output_filenames(
592
592
//
593
593
// [#34511]: https://github.com/rust-lang/rust/issues/34511#issuecomment-322340401
594
594
pub struct ReplaceBodyWithLoop < ' a , ' b > {
595
- within_static_or_const : bool ,
595
+ ignore_item : bool ,
596
596
nested_blocks : Option < Vec < ast:: Block > > ,
597
597
resolver : & ' a mut Resolver < ' b > ,
598
598
}
599
599
600
600
impl < ' a , ' b > ReplaceBodyWithLoop < ' a , ' b > {
601
601
pub fn new ( resolver : & ' a mut Resolver < ' b > ) -> ReplaceBodyWithLoop < ' a , ' b > {
602
- ReplaceBodyWithLoop { within_static_or_const : false , nested_blocks : None , resolver }
602
+ ReplaceBodyWithLoop { ignore_item : false , nested_blocks : None , resolver }
603
603
}
604
604
605
- fn run < R , F : FnOnce ( & mut Self ) -> R > ( & mut self , is_const : bool , action : F ) -> R {
606
- let old_const = mem:: replace ( & mut self . within_static_or_const , is_const ) ;
605
+ fn run < R , F : FnOnce ( & mut Self ) -> R > ( & mut self , ignore_item : bool , action : F ) -> R {
606
+ let old_ignore_item = mem:: replace ( & mut self . ignore_item , ignore_item ) ;
607
607
let old_blocks = self . nested_blocks . take ( ) ;
608
608
let ret = action ( self ) ;
609
- self . within_static_or_const = old_const ;
609
+ self . ignore_item = old_ignore_item ;
610
610
self . nested_blocks = old_blocks;
611
611
ret
612
612
}
613
613
614
- fn should_ignore_fn ( ret_ty : & ast:: FnRetTy ) -> bool {
615
- if let ast:: FnRetTy :: Ty ( ref ty) = ret_ty {
616
- fn involves_impl_trait ( ty : & ast:: Ty ) -> bool {
617
- match ty. kind {
618
- ast:: TyKind :: ImplTrait ( ..) => true ,
619
- ast:: TyKind :: Slice ( ref subty)
620
- | ast:: TyKind :: Array ( ref subty, _)
621
- | ast:: TyKind :: Ptr ( ast:: MutTy { ty : ref subty, .. } )
622
- | ast:: TyKind :: Rptr ( _, ast:: MutTy { ty : ref subty, .. } )
623
- | ast:: TyKind :: Paren ( ref subty) => involves_impl_trait ( subty) ,
624
- ast:: TyKind :: Tup ( ref tys) => any_involves_impl_trait ( tys. iter ( ) ) ,
625
- ast:: TyKind :: Path ( _, ref path) => {
626
- path. segments . iter ( ) . any ( |seg| match seg. args . as_deref ( ) {
627
- None => false ,
628
- Some ( & ast:: GenericArgs :: AngleBracketed ( ref data) ) => {
629
- data. args . iter ( ) . any ( |arg| match arg {
630
- ast:: AngleBracketedArg :: Arg ( arg) => match arg {
631
- ast:: GenericArg :: Type ( ty) => involves_impl_trait ( ty) ,
632
- ast:: GenericArg :: Lifetime ( _)
633
- | ast:: GenericArg :: Const ( _) => false ,
634
- } ,
635
- ast:: AngleBracketedArg :: Constraint ( c) => match c. kind {
636
- ast:: AssocTyConstraintKind :: Bound { .. } => true ,
637
- ast:: AssocTyConstraintKind :: Equality { ref ty } => {
638
- involves_impl_trait ( ty)
639
- }
640
- } ,
641
- } )
642
- }
643
- Some ( & ast:: GenericArgs :: Parenthesized ( ref data) ) => {
644
- any_involves_impl_trait ( data. inputs . iter ( ) )
645
- || ReplaceBodyWithLoop :: should_ignore_fn ( & data. output )
646
- }
647
- } )
648
- }
649
- _ => false ,
650
- }
651
- }
652
-
653
- fn any_involves_impl_trait < ' a , I : Iterator < Item = & ' a P < ast:: Ty > > > ( mut it : I ) -> bool {
654
- it. any ( |subty| involves_impl_trait ( subty) )
655
- }
656
-
657
- involves_impl_trait ( ty)
658
- } else {
659
- false
660
- }
661
- }
662
-
663
- fn is_sig_const ( sig : & ast:: FnSig ) -> bool {
614
+ fn should_ignore_fn ( sig : & ast:: FnSig ) -> bool {
664
615
matches ! ( sig. header. constness, ast:: Const :: Yes ( _) )
665
- || ReplaceBodyWithLoop :: should_ignore_fn ( & sig. decl . output )
616
+ || matches ! ( & sig. decl. output, ast :: FnRetTy :: Ty ( ty ) if ty . contains_impl_trait ( ) )
666
617
}
667
618
}
668
619
669
620
impl < ' a > MutVisitor for ReplaceBodyWithLoop < ' a , ' _ > {
670
621
fn visit_item_kind ( & mut self , i : & mut ast:: ItemKind ) {
671
- let is_const = match i {
622
+ let ignore_item = match i {
672
623
ast:: ItemKind :: Static ( ..) | ast:: ItemKind :: Const ( ..) => true ,
673
- ast:: ItemKind :: Fn ( _, ref sig, _, _) => Self :: is_sig_const ( sig) ,
624
+ ast:: ItemKind :: Fn ( _, ref sig, _, _) => Self :: should_ignore_fn ( sig) ,
674
625
_ => false ,
675
626
} ;
676
- self . run ( is_const , |s| noop_visit_item_kind ( i, s) )
627
+ self . run ( ignore_item , |s| noop_visit_item_kind ( i, s) )
677
628
}
678
629
679
630
fn flat_map_trait_item ( & mut self , i : P < ast:: AssocItem > ) -> SmallVec < [ P < ast:: AssocItem > ; 1 ] > {
680
- let is_const = match i. kind {
631
+ let ignore_item = match i. kind {
681
632
ast:: AssocItemKind :: Const ( ..) => true ,
682
- ast:: AssocItemKind :: Fn ( _, ref sig, _, _) => Self :: is_sig_const ( sig) ,
633
+ ast:: AssocItemKind :: Fn ( _, ref sig, _, _) => Self :: should_ignore_fn ( sig) ,
683
634
_ => false ,
684
635
} ;
685
- self . run ( is_const , |s| noop_flat_map_assoc_item ( i, s) )
636
+ self . run ( ignore_item , |s| noop_flat_map_assoc_item ( i, s) )
686
637
}
687
638
688
639
fn flat_map_impl_item ( & mut self , i : P < ast:: AssocItem > ) -> SmallVec < [ P < ast:: AssocItem > ; 1 ] > {
@@ -723,6 +674,11 @@ impl<'a> MutVisitor for ReplaceBodyWithLoop<'a, '_> {
723
674
}
724
675
}
725
676
677
+ if self . ignore_item {
678
+ noop_visit_block ( b, self ) ;
679
+ return ;
680
+ }
681
+
726
682
let empty_block = stmt_to_block ( BlockCheckMode :: Default , None , self . resolver ) ;
727
683
let loop_expr = P ( ast:: Expr {
728
684
kind : ast:: ExprKind :: Loop ( P ( empty_block) , None ) ,
@@ -738,39 +694,35 @@ impl<'a> MutVisitor for ReplaceBodyWithLoop<'a, '_> {
738
694
kind : ast:: StmtKind :: Expr ( loop_expr) ,
739
695
} ;
740
696
741
- if self . within_static_or_const {
742
- noop_visit_block ( b, self )
743
- } else {
744
- visit_clobber ( b. deref_mut ( ) , |b| {
745
- let mut stmts = vec ! [ ] ;
746
- for s in b. stmts {
747
- let old_blocks = self . nested_blocks . replace ( vec ! [ ] ) ;
697
+ visit_clobber ( b. deref_mut ( ) , |b| {
698
+ let mut stmts = vec ! [ ] ;
699
+ for s in b. stmts {
700
+ let old_blocks = self . nested_blocks . replace ( vec ! [ ] ) ;
748
701
749
- stmts. extend ( self . flat_map_stmt ( s) . into_iter ( ) . filter ( |s| s. is_item ( ) ) ) ;
702
+ stmts. extend ( self . flat_map_stmt ( s) . into_iter ( ) . filter ( |s| s. is_item ( ) ) ) ;
750
703
751
- // we put a Some in there earlier with that replace(), so this is valid
752
- let new_blocks = self . nested_blocks . take ( ) . unwrap ( ) ;
753
- self . nested_blocks = old_blocks;
754
- stmts. extend ( new_blocks. into_iter ( ) . map ( |b| block_to_stmt ( b, self . resolver ) ) ) ;
755
- }
704
+ // we put a Some in there earlier with that replace(), so this is valid
705
+ let new_blocks = self . nested_blocks . take ( ) . unwrap ( ) ;
706
+ self . nested_blocks = old_blocks;
707
+ stmts. extend ( new_blocks. into_iter ( ) . map ( |b| block_to_stmt ( b, self . resolver ) ) ) ;
708
+ }
756
709
757
- let mut new_block = ast:: Block { stmts, ..b } ;
710
+ let mut new_block = ast:: Block { stmts, ..b } ;
758
711
759
- if let Some ( old_blocks) = self . nested_blocks . as_mut ( ) {
760
- //push our fresh block onto the cache and yield an empty block with `loop {}`
761
- if !new_block. stmts . is_empty ( ) {
762
- old_blocks. push ( new_block) ;
763
- }
712
+ if let Some ( old_blocks) = self . nested_blocks . as_mut ( ) {
713
+ //push our fresh block onto the cache and yield an empty block with `loop {}`
714
+ if !new_block. stmts . is_empty ( ) {
715
+ old_blocks. push ( new_block) ;
716
+ }
764
717
765
- stmt_to_block ( b. rules , Some ( loop_stmt) , & mut self . resolver )
766
- } else {
767
- //push `loop {}` onto the end of our fresh block and yield that
768
- new_block. stmts . push ( loop_stmt) ;
718
+ stmt_to_block ( b. rules , Some ( loop_stmt) , & mut self . resolver )
719
+ } else {
720
+ //push `loop {}` onto the end of our fresh block and yield that
721
+ new_block. stmts . push ( loop_stmt) ;
769
722
770
- new_block
771
- }
772
- } )
773
- }
723
+ new_block
724
+ }
725
+ } )
774
726
}
775
727
776
728
// in general the pretty printer processes unexpanded code, so
0 commit comments