@@ -40,6 +40,9 @@ enum SelfSemantic {
40
40
enum DisallowTildeConstContext < ' a > {
41
41
TraitObject ,
42
42
Fn ( FnKind < ' a > ) ,
43
+ Trait ( Span ) ,
44
+ Impl ( Span ) ,
45
+ Item ,
43
46
}
44
47
45
48
struct AstValidator < ' a > {
@@ -110,18 +113,6 @@ impl<'a> AstValidator<'a> {
110
113
self . disallow_tilde_const = old;
111
114
}
112
115
113
- fn with_tilde_const_allowed ( & mut self , f : impl FnOnce ( & mut Self ) ) {
114
- self . with_tilde_const ( None , f)
115
- }
116
-
117
- fn with_banned_tilde_const (
118
- & mut self ,
119
- ctx : DisallowTildeConstContext < ' a > ,
120
- f : impl FnOnce ( & mut Self ) ,
121
- ) {
122
- self . with_tilde_const ( Some ( ctx) , f)
123
- }
124
-
125
116
fn check_type_alias_where_clause_location (
126
117
& mut self ,
127
118
ty_alias : & TyAlias ,
@@ -173,7 +164,7 @@ impl<'a> AstValidator<'a> {
173
164
self . with_impl_trait ( Some ( t. span ) , |this| visit:: walk_ty ( this, t) )
174
165
}
175
166
TyKind :: TraitObject ( ..) => self
176
- . with_banned_tilde_const ( DisallowTildeConstContext :: TraitObject , |this| {
167
+ . with_tilde_const ( Some ( DisallowTildeConstContext :: TraitObject ) , |this| {
177
168
visit:: walk_ty ( this, t)
178
169
} ) ,
179
170
TyKind :: Path ( qself, path) => {
@@ -845,11 +836,9 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
845
836
846
837
this. visit_vis ( & item. vis ) ;
847
838
this. visit_ident ( item. ident ) ;
848
- if let Const :: Yes ( _) = constness {
849
- this. with_tilde_const_allowed ( |this| this. visit_generics ( generics) ) ;
850
- } else {
851
- this. visit_generics ( generics) ;
852
- }
839
+ let disallowed = matches ! ( constness, Const :: No )
840
+ . then ( || DisallowTildeConstContext :: Impl ( item. span ) ) ;
841
+ this. with_tilde_const ( disallowed, |this| this. visit_generics ( generics) ) ;
853
842
this. visit_trait_ref ( t) ;
854
843
this. visit_ty ( self_ty) ;
855
844
@@ -863,10 +852,10 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
863
852
polarity,
864
853
defaultness,
865
854
constness,
866
- generics : _ ,
855
+ generics,
867
856
of_trait : None ,
868
857
self_ty,
869
- items : _ ,
858
+ items,
870
859
} ) => {
871
860
let error =
872
861
|annotation_span, annotation, only_trait : bool | errors:: InherentImplCannot {
@@ -898,6 +887,14 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
898
887
if let & Const :: Yes ( span) = constness {
899
888
self . err_handler ( ) . emit_err ( error ( span, "`const`" , true ) ) ;
900
889
}
890
+
891
+ self . visit_vis ( & item. vis ) ;
892
+ self . visit_ident ( item. ident ) ;
893
+ self . with_tilde_const ( None , |this| this. visit_generics ( generics) ) ;
894
+ self . visit_ty ( self_ty) ;
895
+ walk_list ! ( self , visit_assoc_item, items, AssocCtxt :: Impl ) ;
896
+ walk_list ! ( self , visit_attribute, & item. attrs) ;
897
+ return ; // Avoid visiting again.
901
898
}
902
899
ItemKind :: Fn ( box Fn { defaultness, sig, generics, body } ) => {
903
900
self . check_defaultness ( item. span , * defaultness) ;
@@ -978,8 +975,10 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
978
975
// context for the supertraits.
979
976
this. visit_vis ( & item. vis ) ;
980
977
this. visit_ident ( item. ident ) ;
981
- this. visit_generics ( generics) ;
982
- this. with_tilde_const_allowed ( |this| {
978
+ let disallowed =
979
+ ( !is_const_trait) . then ( || DisallowTildeConstContext :: Trait ( item. span ) ) ;
980
+ this. with_tilde_const ( disallowed, |this| {
981
+ this. visit_generics ( generics) ;
983
982
walk_list ! ( this, visit_param_bound, bounds, BoundKind :: SuperTraits )
984
983
} ) ;
985
984
walk_list ! ( this, visit_assoc_item, items, AssocCtxt :: Trait ) ;
@@ -999,16 +998,11 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
999
998
}
1000
999
}
1001
1000
ItemKind :: Struct ( vdata, generics) => match vdata {
1002
- // Duplicating the `Visitor` logic allows catching all cases
1003
- // of `Anonymous(Struct, Union)` outside of a field struct or union.
1004
- //
1005
- // Inside `visit_ty` the validator catches every `Anonymous(Struct, Union)` it
1006
- // encounters, and only on `ItemKind::Struct` and `ItemKind::Union`
1007
- // it uses `visit_ty_common`, which doesn't contain that specific check.
1008
1001
VariantData :: Struct ( fields, ..) => {
1009
1002
self . visit_vis ( & item. vis ) ;
1010
1003
self . visit_ident ( item. ident ) ;
1011
1004
self . visit_generics ( generics) ;
1005
+ // Permit `Anon{Struct,Union}` as field type.
1012
1006
walk_list ! ( self , visit_struct_field_def, fields) ;
1013
1007
walk_list ! ( self , visit_attribute, & item. attrs) ;
1014
1008
return ;
@@ -1024,6 +1018,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
1024
1018
self . visit_vis ( & item. vis ) ;
1025
1019
self . visit_ident ( item. ident ) ;
1026
1020
self . visit_generics ( generics) ;
1021
+ // Permit `Anon{Struct,Union}` as field type.
1027
1022
walk_list ! ( self , visit_struct_field_def, fields) ;
1028
1023
walk_list ! ( self , visit_attribute, & item. attrs) ;
1029
1024
return ;
@@ -1212,15 +1207,18 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
1212
1207
if let Some ( reason) = & self . disallow_tilde_const =>
1213
1208
{
1214
1209
let reason = match reason {
1215
- DisallowTildeConstContext :: TraitObject => {
1216
- errors:: TildeConstReason :: TraitObject
1217
- }
1218
1210
DisallowTildeConstContext :: Fn ( FnKind :: Closure ( ..) ) => {
1219
1211
errors:: TildeConstReason :: Closure
1220
1212
}
1221
1213
DisallowTildeConstContext :: Fn ( FnKind :: Fn ( _, ident, ..) ) => {
1222
1214
errors:: TildeConstReason :: Function { ident : ident. span }
1223
1215
}
1216
+ & DisallowTildeConstContext :: Trait ( span) => errors:: TildeConstReason :: Trait { span } ,
1217
+ & DisallowTildeConstContext :: Impl ( span) => errors:: TildeConstReason :: Impl { span } ,
1218
+ DisallowTildeConstContext :: TraitObject => {
1219
+ errors:: TildeConstReason :: TraitObject
1220
+ }
1221
+ DisallowTildeConstContext :: Item => errors:: TildeConstReason :: Item ,
1224
1222
} ;
1225
1223
self . err_handler ( )
1226
1224
. emit_err ( errors:: TildeConstDisallowed { span : bound. span ( ) , reason } ) ;
@@ -1328,7 +1326,6 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
1328
1326
|| matches ! ( fk. ctxt( ) , Some ( FnCtxt :: Assoc ( _) ) if self . in_const_trait_or_impl) ;
1329
1327
1330
1328
let disallowed = ( !tilde_const_allowed) . then ( || DisallowTildeConstContext :: Fn ( fk) ) ;
1331
-
1332
1329
self . with_tilde_const ( disallowed, |this| visit:: walk_fn ( this, fk) ) ;
1333
1330
}
1334
1331
@@ -1397,18 +1394,6 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
1397
1394
}
1398
1395
1399
1396
match & item. kind {
1400
- AssocItemKind :: Type ( box TyAlias { generics, bounds, ty, .. } )
1401
- if ctxt == AssocCtxt :: Trait =>
1402
- {
1403
- self . visit_vis ( & item. vis ) ;
1404
- self . visit_ident ( item. ident ) ;
1405
- walk_list ! ( self , visit_attribute, & item. attrs) ;
1406
- self . with_tilde_const_allowed ( |this| {
1407
- this. visit_generics ( generics) ;
1408
- walk_list ! ( this, visit_param_bound, bounds, BoundKind :: Bound ) ;
1409
- } ) ;
1410
- walk_list ! ( self , visit_ty, ty) ;
1411
- }
1412
1397
AssocItemKind :: Fn ( box Fn { sig, generics, body, .. } )
1413
1398
if self . in_const_trait_or_impl
1414
1399
|| ctxt == AssocCtxt :: Trait
@@ -1552,7 +1537,7 @@ pub fn check_crate(
1552
1537
in_const_trait_or_impl : false ,
1553
1538
has_proc_macro_decls : false ,
1554
1539
outer_impl_trait : None ,
1555
- disallow_tilde_const : None ,
1540
+ disallow_tilde_const : Some ( DisallowTildeConstContext :: Item ) ,
1556
1541
is_impl_trait_banned : false ,
1557
1542
lint_buffer : lints,
1558
1543
} ;
0 commit comments