@@ -44,6 +44,7 @@ use std::io::{self, Write};
4444use std:: option;
4545use std:: path:: Path ;
4646use std:: str:: FromStr ;
47+ use std:: mem;
4748
4849use rustc:: hir:: map as hir_map;
4950use rustc:: hir:: map:: blocks;
@@ -618,52 +619,53 @@ impl UserIdentifiedItem {
618619 }
619620}
620621
621- struct ReplaceBodyWithLoop {
622+ // Note: Also used by librustdoc, see PR #43348. Consider moving this struct elsewhere.
623+ pub struct ReplaceBodyWithLoop {
622624 within_static_or_const : bool ,
623625}
624626
625627impl ReplaceBodyWithLoop {
626- fn new ( ) -> ReplaceBodyWithLoop {
628+ pub fn new ( ) -> ReplaceBodyWithLoop {
627629 ReplaceBodyWithLoop { within_static_or_const : false }
628630 }
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+ }
629638}
630639
631640impl fold:: Folder for ReplaceBodyWithLoop {
632641 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) )
643649 }
644650
645651 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) )
655659 }
656660
657661 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) )
667669 }
668670
669671 fn fold_block ( & mut self , b : P < ast:: Block > ) -> P < ast:: Block > {
0 commit comments