@@ -219,7 +219,7 @@ impl<'a> AstValidator<'a> {
219219 }
220220 }
221221 }
222- TyKind :: AnonymousStruct ( ref fields, ..) | TyKind :: AnonymousUnion ( ref fields, ..) => {
222+ TyKind :: AnonStruct ( ref fields, ..) | TyKind :: AnonUnion ( ref fields, ..) => {
223223 // self.with_banned_assoc_ty_bound(|this| {
224224 walk_list ! ( self , visit_struct_field_def, fields)
225225 // });
@@ -229,18 +229,17 @@ impl<'a> AstValidator<'a> {
229229 }
230230
231231 fn visit_struct_field_def ( & mut self , field : & ' a FieldDef ) {
232- if let Some ( ident) = field. ident {
233- if ident. name == kw:: Underscore {
234- self . check_anonymous_field ( field) ;
232+ if let Some ( ident) = field. ident &&
233+ ident. name == kw:: Underscore {
234+ self . check_unnamed_field_ty ( & field. ty , field . span ) ;
235235 self . visit_vis ( & field. vis ) ;
236236 self . visit_ident ( ident) ;
237237 self . visit_ty_common ( & field. ty ) ;
238238 self . walk_ty ( & field. ty ) ;
239239 walk_list ! ( self , visit_attribute, & field. attrs) ;
240- return ;
241- }
240+ } else {
241+ self . visit_field_def ( field ) ;
242242 }
243- self . visit_field_def ( field) ;
244243 }
245244
246245 fn err_handler ( & self ) -> & rustc_errors:: Handler {
@@ -280,63 +279,48 @@ impl<'a> AstValidator<'a> {
280279 }
281280 }
282281
283- fn check_anonymous_field ( & self , field : & FieldDef ) {
284- let FieldDef { ty, .. } = field;
285- match & ty. kind {
286- TyKind :: AnonymousStruct ( ..) | TyKind :: AnonymousUnion ( ..) => {
287- // We already checked for `kw::Underscore` before calling this function,
288- // so skip the check
289- }
290- TyKind :: Path ( ..) => {
291- // If the anonymous field contains a Path as type, we can't determine
292- // if the path is a valid struct or union, so skip the check
293- }
294- _ => {
295- let msg = "unnamed fields can only have struct or union types" ;
296- let label = "not a struct or union" ;
297- self . err_handler ( )
298- . struct_span_err ( field. span , msg)
299- . span_label ( ty. span , label)
300- . emit ( ) ;
301- }
282+ fn check_unnamed_field_ty ( & self , ty : & Ty , span : Span ) {
283+ if matches ! (
284+ & ty. kind,
285+ // We already checked for `kw::Underscore` before calling this function,
286+ // so skip the check
287+ TyKind :: AnonStruct ( ..) | TyKind :: AnonUnion ( ..)
288+ // If the anonymous field contains a Path as type, we can't determine
289+ // if the path is a valid struct or union, so skip the check
290+ | TyKind :: Path ( ..)
291+ ) {
292+ return ;
302293 }
294+ let msg = "unnamed fields can only have struct or union types" ;
295+ let label = "not a struct or union" ;
296+ self . err_handler ( ) . struct_span_err ( span, msg) . span_label ( ty. span , label) . emit ( ) ;
303297 }
304298
305- fn deny_anonymous_struct ( & self , ty : & Ty ) {
306- match & ty. kind {
307- TyKind :: AnonymousStruct ( ..) => {
308- self . err_handler ( )
309- . struct_span_err (
310- ty. span ,
311- "anonymous structs are not allowed outside of unnamed struct or union fields" ,
312- )
313- . span_label ( ty. span , "anonymous struct declared here" )
314- . emit ( ) ;
315- }
316- TyKind :: AnonymousUnion ( ..) => {
317- self . err_handler ( )
299+ fn deny_anon_struct_or_union ( & self , ty : & Ty ) {
300+ let struct_or_union = match & ty. kind {
301+ TyKind :: AnonStruct ( ..) => "struct" ,
302+ TyKind :: AnonUnion ( ..) => "union" ,
303+ _ => return ,
304+ } ;
305+ self . err_handler ( )
318306 . struct_span_err (
319307 ty. span ,
320- "anonymous unions are not allowed outside of unnamed struct or union fields" ,
308+ format ! ( "anonymous {struct_or_union}s are not allowed outside of unnamed struct or union fields" ) ,
321309 )
322- . span_label ( ty. span , "anonymous union declared here" )
310+ . span_label ( ty. span , format ! ( "anonymous {struct_or_union} declared here" ) )
323311 . emit ( ) ;
324- }
325- _ => { }
326- }
327312 }
328313
329- fn deny_anonymous_field ( & self , field : & FieldDef ) {
330- if let Some ( ident) = field. ident {
331- if ident. name == kw:: Underscore {
314+ fn deny_unnamed_field ( & self , field : & FieldDef ) {
315+ if let Some ( ident) = field. ident &&
316+ ident. name == kw:: Underscore {
332317 self . err_handler ( )
333318 . struct_span_err (
334319 field. span ,
335320 "anonymous fields are not allowed outside of structs or unions" ,
336321 )
337322 . span_label ( ident. span , "anonymous field declared here" )
338323 . emit ( ) ;
339- }
340324 }
341325 }
342326
@@ -866,7 +850,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
866850 fn visit_ty ( & mut self , ty : & ' a Ty ) {
867851 self . visit_ty_common ( ty) ;
868852 tracing:: info!( ?ty) ;
869- self . deny_anonymous_struct ( ty) ;
853+ self . deny_anon_struct_or_union ( ty) ;
870854 self . walk_ty ( ty)
871855 }
872856
@@ -881,7 +865,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
881865 }
882866
883867 fn visit_field_def ( & mut self , field : & ' a FieldDef ) {
884- self . deny_anonymous_field ( field) ;
868+ self . deny_unnamed_field ( field) ;
885869 visit:: walk_field_def ( self , field)
886870 }
887871
0 commit comments