@@ -219,7 +219,7 @@ impl<'a> AstValidator<'a> {
219
219
}
220
220
}
221
221
}
222
- TyKind :: AnonymousStruct ( ref fields, ..) | TyKind :: AnonymousUnion ( ref fields, ..) => {
222
+ TyKind :: AnonStruct ( ref fields, ..) | TyKind :: AnonUnion ( ref fields, ..) => {
223
223
// self.with_banned_assoc_ty_bound(|this| {
224
224
walk_list ! ( self , visit_struct_field_def, fields)
225
225
// });
@@ -229,18 +229,17 @@ impl<'a> AstValidator<'a> {
229
229
}
230
230
231
231
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 ) ;
235
235
self . visit_vis ( & field. vis ) ;
236
236
self . visit_ident ( ident) ;
237
237
self . visit_ty_common ( & field. ty ) ;
238
238
self . walk_ty ( & field. ty ) ;
239
239
walk_list ! ( self , visit_attribute, & field. attrs) ;
240
- return ;
241
- }
240
+ } else {
241
+ self . visit_field_def ( field ) ;
242
242
}
243
- self . visit_field_def ( field) ;
244
243
}
245
244
246
245
fn err_handler ( & self ) -> & rustc_errors:: Handler {
@@ -280,63 +279,48 @@ impl<'a> AstValidator<'a> {
280
279
}
281
280
}
282
281
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 ;
302
293
}
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 ( ) ;
303
297
}
304
298
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 ( )
318
306
. struct_span_err (
319
307
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" ) ,
321
309
)
322
- . span_label ( ty. span , "anonymous union declared here" )
310
+ . span_label ( ty. span , format ! ( "anonymous {struct_or_union} declared here" ) )
323
311
. emit ( ) ;
324
- }
325
- _ => { }
326
- }
327
312
}
328
313
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 {
332
317
self . err_handler ( )
333
318
. struct_span_err (
334
319
field. span ,
335
320
"anonymous fields are not allowed outside of structs or unions" ,
336
321
)
337
322
. span_label ( ident. span , "anonymous field declared here" )
338
323
. emit ( ) ;
339
- }
340
324
}
341
325
}
342
326
@@ -866,7 +850,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
866
850
fn visit_ty ( & mut self , ty : & ' a Ty ) {
867
851
self . visit_ty_common ( ty) ;
868
852
tracing:: info!( ?ty) ;
869
- self . deny_anonymous_struct ( ty) ;
853
+ self . deny_anon_struct_or_union ( ty) ;
870
854
self . walk_ty ( ty)
871
855
}
872
856
@@ -881,7 +865,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
881
865
}
882
866
883
867
fn visit_field_def ( & mut self , field : & ' a FieldDef ) {
884
- self . deny_anonymous_field ( field) ;
868
+ self . deny_unnamed_field ( field) ;
885
869
visit:: walk_field_def ( self , field)
886
870
}
887
871
0 commit comments