@@ -81,6 +81,12 @@ struct AstValidator<'a> {
81
81
}
82
82
83
83
impl < ' a > AstValidator < ' a > {
84
+ fn with_in_trait_impl ( & mut self , is_in : bool , f : impl FnOnce ( & mut Self ) ) {
85
+ let old = mem:: replace ( & mut self . in_trait_impl , is_in) ;
86
+ f ( self ) ;
87
+ self . in_trait_impl = old;
88
+ }
89
+
84
90
fn with_banned_impl_trait ( & mut self , f : impl FnOnce ( & mut Self ) ) {
85
91
let old = mem:: replace ( & mut self . is_impl_trait_banned , true ) ;
86
92
f ( self ) ;
@@ -737,28 +743,29 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
737
743
ref self_ty,
738
744
items : _,
739
745
} => {
740
- let old_in_trait_impl = mem:: replace ( & mut self . in_trait_impl , true ) ;
741
-
742
- self . invalid_visibility ( & item. vis , None ) ;
743
- if let TyKind :: Err = self_ty. kind {
744
- self . err_handler ( )
745
- . struct_span_err ( item. span , "`impl Trait for .. {}` is an obsolete syntax" )
746
- . help ( "use `auto trait Trait {}` instead" )
746
+ self . with_in_trait_impl ( true , |this| {
747
+ this. invalid_visibility ( & item. vis , None ) ;
748
+ if let TyKind :: Err = self_ty. kind {
749
+ this. err_handler ( )
750
+ . struct_span_err (
751
+ item. span ,
752
+ "`impl Trait for .. {}` is an obsolete syntax" ,
753
+ )
754
+ . help ( "use `auto trait Trait {}` instead" )
755
+ . emit ( ) ;
756
+ }
757
+ if unsafety == Unsafety :: Unsafe && polarity == ImplPolarity :: Negative {
758
+ struct_span_err ! (
759
+ this. session,
760
+ item. span,
761
+ E0198 ,
762
+ "negative impls cannot be unsafe"
763
+ )
747
764
. emit ( ) ;
748
- }
749
- if unsafety == Unsafety :: Unsafe && polarity == ImplPolarity :: Negative {
750
- struct_span_err ! (
751
- self . session,
752
- item. span,
753
- E0198 ,
754
- "negative impls cannot be unsafe"
755
- )
756
- . emit ( ) ;
757
- }
758
-
759
- visit:: walk_item ( self , item) ;
765
+ }
760
766
761
- self . in_trait_impl = old_in_trait_impl;
767
+ visit:: walk_item ( this, item) ;
768
+ } ) ;
762
769
return ; // Avoid visiting again.
763
770
}
764
771
ItemKind :: Impl {
@@ -1142,7 +1149,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
1142
1149
}
1143
1150
}
1144
1151
1145
- visit:: walk_assoc_item ( self , item, ctxt) ;
1152
+ self . with_in_trait_impl ( false , |this| visit:: walk_assoc_item ( this , item, ctxt) ) ;
1146
1153
}
1147
1154
}
1148
1155
0 commit comments