@@ -44,6 +44,7 @@ use std::io::{self, Write};
44
44
use std:: option;
45
45
use std:: path:: Path ;
46
46
use std:: str:: FromStr ;
47
+ use std:: mem;
47
48
48
49
use rustc:: hir:: map as hir_map;
49
50
use rustc:: hir:: map:: blocks;
@@ -618,52 +619,53 @@ impl UserIdentifiedItem {
618
619
}
619
620
}
620
621
621
- struct ReplaceBodyWithLoop {
622
+ // Note: Also used by librustdoc, see PR #43348. Consider moving this struct elsewhere.
623
+ pub struct ReplaceBodyWithLoop {
622
624
within_static_or_const : bool ,
623
625
}
624
626
625
627
impl ReplaceBodyWithLoop {
626
- fn new ( ) -> ReplaceBodyWithLoop {
628
+ pub fn new ( ) -> ReplaceBodyWithLoop {
627
629
ReplaceBodyWithLoop { within_static_or_const : false }
628
630
}
631
+
632
+ fn run < R , F : FnOnce ( & mut Self ) -> R > ( & mut self , is_const : bool , action : F ) -> R {
633
+ let old_const = mem:: replace ( & mut self . within_static_or_const , is_const) ;
634
+ let ret = action ( self ) ;
635
+ self . within_static_or_const = old_const;
636
+ ret
637
+ }
629
638
}
630
639
631
640
impl fold:: Folder for ReplaceBodyWithLoop {
632
641
fn fold_item_kind ( & mut self , i : ast:: ItemKind ) -> ast:: ItemKind {
633
- match i {
634
- ast:: ItemKind :: Static ( ..) |
635
- ast:: ItemKind :: Const ( ..) => {
636
- self . within_static_or_const = true ;
637
- let ret = fold:: noop_fold_item_kind ( i, self ) ;
638
- self . within_static_or_const = false ;
639
- return ret;
640
- }
641
- _ => fold:: noop_fold_item_kind ( i, self ) ,
642
- }
642
+ let is_const = match i {
643
+ ast:: ItemKind :: Static ( ..) | ast:: ItemKind :: Const ( ..) => true ,
644
+ ast:: ItemKind :: Fn ( _, _, ref constness, _, _, _) =>
645
+ constness. node == ast:: Constness :: Const ,
646
+ _ => false ,
647
+ } ;
648
+ self . run ( is_const, |s| fold:: noop_fold_item_kind ( i, s) )
643
649
}
644
650
645
651
fn fold_trait_item ( & mut self , i : ast:: TraitItem ) -> SmallVector < ast:: TraitItem > {
646
- match i. node {
647
- ast:: TraitItemKind :: Const ( ..) => {
648
- self . within_static_or_const = true ;
649
- let ret = fold:: noop_fold_trait_item ( i, self ) ;
650
- self . within_static_or_const = false ;
651
- return ret;
652
- }
653
- _ => fold:: noop_fold_trait_item ( i, self ) ,
654
- }
652
+ let is_const = match i. node {
653
+ ast:: TraitItemKind :: Const ( ..) => true ,
654
+ ast:: TraitItemKind :: Method ( ast:: MethodSig { ref constness, .. } , _) =>
655
+ constness. node == ast:: Constness :: Const ,
656
+ _ => false ,
657
+ } ;
658
+ self . run ( is_const, |s| fold:: noop_fold_trait_item ( i, s) )
655
659
}
656
660
657
661
fn fold_impl_item ( & mut self , i : ast:: ImplItem ) -> SmallVector < ast:: ImplItem > {
658
- match i. node {
659
- ast:: ImplItemKind :: Const ( ..) => {
660
- self . within_static_or_const = true ;
661
- let ret = fold:: noop_fold_impl_item ( i, self ) ;
662
- self . within_static_or_const = false ;
663
- return ret;
664
- }
665
- _ => fold:: noop_fold_impl_item ( i, self ) ,
666
- }
662
+ let is_const = match i. node {
663
+ ast:: ImplItemKind :: Const ( ..) => true ,
664
+ ast:: ImplItemKind :: Method ( ast:: MethodSig { ref constness, .. } , _) =>
665
+ constness. node == ast:: Constness :: Const ,
666
+ _ => false ,
667
+ } ;
668
+ self . run ( is_const, |s| fold:: noop_fold_impl_item ( i, s) )
667
669
}
668
670
669
671
fn fold_block ( & mut self , b : P < ast:: Block > ) -> P < ast:: Block > {
0 commit comments