@@ -52,7 +52,8 @@ struct AstValidator<'a> {
52
52
/// Are we inside a trait impl?
53
53
in_trait_impl : bool ,
54
54
55
- in_const_trait_impl : bool ,
55
+ /// Are we inside a const trait defn or impl?
56
+ in_const_trait_or_impl : bool ,
56
57
57
58
has_proc_macro_decls : bool ,
58
59
@@ -78,11 +79,19 @@ impl<'a> AstValidator<'a> {
78
79
f : impl FnOnce ( & mut Self ) ,
79
80
) {
80
81
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
+ ) ;
83
86
f ( self ) ;
84
87
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;
86
95
}
87
96
88
97
fn with_banned_impl_trait ( & mut self , f : impl FnOnce ( & mut Self ) ) {
@@ -933,23 +942,26 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
933
942
}
934
943
}
935
944
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
+ }
943
954
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 ) ;
951
964
} ) ;
952
- walk_list ! ( self , visit_assoc_item, items, AssocCtxt :: Trait ) ;
953
965
walk_list ! ( self , visit_attribute, & item. attrs) ;
954
966
return ; // Avoid visiting again
955
967
}
@@ -1278,7 +1290,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
1278
1290
1279
1291
let tilde_const_allowed =
1280
1292
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 ) ;
1282
1294
1283
1295
let disallowed = ( !tilde_const_allowed) . then ( || DisallowTildeConstContext :: Fn ( fk) ) ;
1284
1296
@@ -1363,7 +1375,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
1363
1375
walk_list ! ( self , visit_ty, ty) ;
1364
1376
}
1365
1377
AssocItemKind :: Fn ( box Fn { sig, generics, body, .. } )
1366
- if self . in_const_trait_impl
1378
+ if self . in_const_trait_or_impl
1367
1379
|| ctxt == AssocCtxt :: Trait
1368
1380
|| matches ! ( sig. header. constness, Const :: Yes ( _) ) =>
1369
1381
{
@@ -1510,7 +1522,7 @@ pub fn check_crate(
1510
1522
features,
1511
1523
extern_mod : None ,
1512
1524
in_trait_impl : false ,
1513
- in_const_trait_impl : false ,
1525
+ in_const_trait_or_impl : false ,
1514
1526
has_proc_macro_decls : false ,
1515
1527
outer_impl_trait : None ,
1516
1528
disallow_tilde_const : None ,
0 commit comments