@@ -81,6 +81,12 @@ struct AstValidator<'a> {
8181}
8282
8383impl < ' 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+
8490 fn with_banned_impl_trait ( & mut self , f : impl FnOnce ( & mut Self ) ) {
8591 let old = mem:: replace ( & mut self . is_impl_trait_banned , true ) ;
8692 f ( self ) ;
@@ -737,28 +743,29 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
737743 ref self_ty,
738744 items : _,
739745 } => {
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+ )
747764 . 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+ }
760766
761- self . in_trait_impl = old_in_trait_impl;
767+ visit:: walk_item ( this, item) ;
768+ } ) ;
762769 return ; // Avoid visiting again.
763770 }
764771 ItemKind :: Impl {
@@ -1142,7 +1149,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
11421149 }
11431150 }
11441151
1145- visit:: walk_assoc_item ( self , item, ctxt) ;
1152+ self . with_in_trait_impl ( false , |this| visit:: walk_assoc_item ( this , item, ctxt) ) ;
11461153 }
11471154}
11481155
0 commit comments