@@ -52,7 +52,8 @@ struct AstValidator<'a> {
5252 /// Are we inside a trait impl?
5353 in_trait_impl : bool ,
5454
55- in_const_trait_impl : bool ,
55+ /// Are we inside a const trait defn or impl?
56+ in_const_trait_or_impl : bool ,
5657
5758 has_proc_macro_decls : bool ,
5859
@@ -78,11 +79,19 @@ impl<'a> AstValidator<'a> {
7879 f : impl FnOnce ( & mut Self ) ,
7980 ) {
8081 let old = mem:: replace ( & mut self . in_trait_impl , is_in) ;
81- let old_const =
82- mem:: replace ( & mut self . in_const_trait_impl , matches ! ( constness, Some ( Const :: Yes ( _) ) ) ) ;
82+ let old_const = mem:: replace (
83+ & mut self . in_const_trait_or_impl ,
84+ matches ! ( constness, Some ( Const :: Yes ( _) ) ) ,
85+ ) ;
8386 f ( self ) ;
8487 self . in_trait_impl = old;
85- self . in_const_trait_impl = old_const;
88+ self . in_const_trait_or_impl = old_const;
89+ }
90+
91+ fn with_in_trait ( & mut self , is_const : bool , f : impl FnOnce ( & mut Self ) ) {
92+ let old = mem:: replace ( & mut self . in_const_trait_or_impl , is_const) ;
93+ f ( self ) ;
94+ self . in_const_trait_or_impl = old;
8695 }
8796
8897 fn with_banned_impl_trait ( & mut self , f : impl FnOnce ( & mut Self ) ) {
@@ -933,23 +942,26 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
933942 }
934943 }
935944 ItemKind :: Trait ( box Trait { is_auto, generics, bounds, items, .. } ) => {
936- if * is_auto == IsAuto :: Yes {
937- // Auto traits cannot have generics, super traits nor contain items.
938- self . deny_generic_params ( generics, item. ident . span ) ;
939- self . deny_super_traits ( bounds, item. ident . span ) ;
940- self . deny_where_clause ( & generics. where_clause , item. ident . span ) ;
941- self . deny_items ( items, item. ident . span ) ;
942- }
945+ let is_const_trait = attr:: contains_name ( & item. attrs , sym:: const_trait) ;
946+ self . with_in_trait ( is_const_trait, |this| {
947+ if * is_auto == IsAuto :: Yes {
948+ // Auto traits cannot have generics, super traits nor contain items.
949+ this. deny_generic_params ( generics, item. ident . span ) ;
950+ this. deny_super_traits ( bounds, item. ident . span ) ;
951+ this. deny_where_clause ( & generics. where_clause , item. ident . span ) ;
952+ this. deny_items ( items, item. ident . span ) ;
953+ }
943954
944- // Equivalent of `visit::walk_item` for `ItemKind::Trait` that inserts a bound
945- // context for the supertraits.
946- self . visit_vis ( & item. vis ) ;
947- self . visit_ident ( item. ident ) ;
948- self . visit_generics ( generics) ;
949- self . with_tilde_const_allowed ( |this| {
950- walk_list ! ( this, visit_param_bound, bounds, BoundKind :: SuperTraits )
955+ // Equivalent of `visit::walk_item` for `ItemKind::Trait` that inserts a bound
956+ // context for the supertraits.
957+ this. visit_vis ( & item. vis ) ;
958+ this. visit_ident ( item. ident ) ;
959+ this. visit_generics ( generics) ;
960+ this. with_tilde_const_allowed ( |this| {
961+ walk_list ! ( this, visit_param_bound, bounds, BoundKind :: SuperTraits )
962+ } ) ;
963+ walk_list ! ( this, visit_assoc_item, items, AssocCtxt :: Trait ) ;
951964 } ) ;
952- walk_list ! ( self , visit_assoc_item, items, AssocCtxt :: Trait ) ;
953965 walk_list ! ( self , visit_attribute, & item. attrs) ;
954966 return ; // Avoid visiting again
955967 }
@@ -1278,7 +1290,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
12781290
12791291 let tilde_const_allowed =
12801292 matches ! ( fk. header( ) , Some ( FnHeader { constness: ast:: Const :: Yes ( _) , .. } ) )
1281- || matches ! ( fk. ctxt( ) , Some ( FnCtxt :: Assoc ( _) ) ) ;
1293+ || matches ! ( fk. ctxt( ) , Some ( FnCtxt :: Assoc ( _) ) if self . in_const_trait_or_impl ) ;
12821294
12831295 let disallowed = ( !tilde_const_allowed) . then ( || DisallowTildeConstContext :: Fn ( fk) ) ;
12841296
@@ -1363,7 +1375,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
13631375 walk_list ! ( self , visit_ty, ty) ;
13641376 }
13651377 AssocItemKind :: Fn ( box Fn { sig, generics, body, .. } )
1366- if self . in_const_trait_impl
1378+ if self . in_const_trait_or_impl
13671379 || ctxt == AssocCtxt :: Trait
13681380 || matches ! ( sig. header. constness, Const :: Yes ( _) ) =>
13691381 {
@@ -1510,7 +1522,7 @@ pub fn check_crate(
15101522 features,
15111523 extern_mod : None ,
15121524 in_trait_impl : false ,
1513- in_const_trait_impl : false ,
1525+ in_const_trait_or_impl : false ,
15141526 has_proc_macro_decls : false ,
15151527 outer_impl_trait : None ,
15161528 disallow_tilde_const : None ,
0 commit comments