Skip to content

Commit 28b89e0

Browse files
committed
Improve the code style
1 parent 1686551 commit 28b89e0

File tree

11 files changed

+108
-94
lines changed

11 files changed

+108
-94
lines changed

compiler/rustc_ast/src/ast.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2093,9 +2093,9 @@ pub enum TyKind {
20932093
/// A tuple (`(A, B, C, D,...)`).
20942094
Tup(ThinVec<P<Ty>>),
20952095
/// An anonymous struct type i.e. `struct { foo: Type }`
2096-
AnonymousStruct(ThinVec<FieldDef>),
2096+
AnonStruct(ThinVec<FieldDef>),
20972097
/// An anonymous union type i.e. `union { bar: Type }`
2098-
AnonymousUnion(ThinVec<FieldDef>),
2098+
AnonUnion(ThinVec<FieldDef>),
20992099
/// A path (`module::module::...::Type`), optionally
21002100
/// "qualified", e.g., `<Vec<T> as SomeTrait>::SomeType`.
21012101
///

compiler/rustc_ast/src/mut_visit.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -509,7 +509,7 @@ pub fn noop_visit_ty<T: MutVisitor>(ty: &mut P<Ty>, vis: &mut T) {
509509
visit_vec(bounds, |bound| vis.visit_param_bound(bound));
510510
}
511511
TyKind::MacCall(mac) => vis.visit_mac_call(mac),
512-
TyKind::AnonymousStruct(fields) | TyKind::AnonymousUnion(fields) => {
512+
TyKind::AnonStruct(fields) | TyKind::AnonUnion(fields) => {
513513
fields.flat_map_in_place(|field| vis.flat_map_field_def(field));
514514
}
515515
}

compiler/rustc_ast/src/visit.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,7 @@ pub fn walk_ty<'a, V: Visitor<'a>>(visitor: &mut V, typ: &'a Ty) {
438438
TyKind::Infer | TyKind::ImplicitSelf | TyKind::Err => {}
439439
TyKind::MacCall(mac) => visitor.visit_mac_call(mac),
440440
TyKind::Never | TyKind::CVarArgs => {}
441-
TyKind::AnonymousStruct(ref fields, ..) | TyKind::AnonymousUnion(ref fields, ..) => {
441+
TyKind::AnonStruct(ref fields, ..) | TyKind::AnonUnion(ref fields, ..) => {
442442
walk_list!(visitor, visit_field_def, fields)
443443
}
444444
}

compiler/rustc_ast_lowering/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1296,13 +1296,13 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
12961296
// FIXME(unnamed_fields): IMPLEMENTATION IN PROGRESS
12971297
#[allow(rustc::untranslatable_diagnostic)]
12981298
#[allow(rustc::diagnostic_outside_of_impl)]
1299-
TyKind::AnonymousStruct(ref _fields) => hir::TyKind::Err(
1299+
TyKind::AnonStruct(ref _fields) => hir::TyKind::Err(
13001300
self.tcx.sess.span_err(t.span, "anonymous structs are unimplemented"),
13011301
),
13021302
// FIXME(unnamed_fields): IMPLEMENTATION IN PROGRESS
13031303
#[allow(rustc::untranslatable_diagnostic)]
13041304
#[allow(rustc::diagnostic_outside_of_impl)]
1305-
TyKind::AnonymousUnion(ref _fields) => hir::TyKind::Err(
1305+
TyKind::AnonUnion(ref _fields) => hir::TyKind::Err(
13061306
self.tcx.sess.span_err(t.span, "anonymous unions are unimplemented"),
13071307
),
13081308
TyKind::Slice(ty) => hir::TyKind::Slice(self.lower_ty(ty, itctx)),

compiler/rustc_ast_passes/src/ast_validation.rs

+34-50
Original file line numberDiff line numberDiff line change
@@ -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

compiler/rustc_ast_pretty/src/pprust/state.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1053,11 +1053,11 @@ impl<'a> State<'a> {
10531053
}
10541054
self.pclose();
10551055
}
1056-
ast::TyKind::AnonymousStruct(fields) => {
1056+
ast::TyKind::AnonStruct(fields) => {
10571057
self.head("struct");
10581058
self.print_record_struct_body(&fields, ty.span);
10591059
}
1060-
ast::TyKind::AnonymousUnion(fields) => {
1060+
ast::TyKind::AnonUnion(fields) => {
10611061
self.head("union");
10621062
self.print_record_struct_body(&fields, ty.span);
10631063
}

compiler/rustc_parse/src/parser/item.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use super::diagnostics::{dummy_arg, ConsumeClosingDelim};
2-
use super::ty::{AllowPlus, RecoverAnonymousStructOrUnion, RecoverQPath, RecoverReturnSign};
2+
use super::ty::{AllowPlus, MaybeRecoverAnonStructOrUnion, RecoverQPath, RecoverReturnSign};
33
use super::{AttrWrapper, FollowedByType, ForceCollect, Parser, PathStyle, TrailingToken};
44
use crate::errors::{self, MacroExpandsToAdtField};
55
use crate::fluent_generated as fluent;
@@ -1675,7 +1675,7 @@ impl<'a> Parser<'a> {
16751675
adt_ty,
16761676
ident_span,
16771677
parsed_where,
1678-
RecoverAnonymousStructOrUnion::No,
1678+
MaybeRecoverAnonStructOrUnion::Parse,
16791679
)
16801680
}
16811681

@@ -1684,7 +1684,7 @@ impl<'a> Parser<'a> {
16841684
adt_ty: &str,
16851685
ident_span: Span,
16861686
parsed_where: bool,
1687-
recover_anonymous_struct_or_union: RecoverAnonymousStructOrUnion,
1687+
maybe_recover_anon_struct_or_union: MaybeRecoverAnonStructOrUnion,
16881688
) -> PResult<'a, (ThinVec<FieldDef>, /* recovered */ bool)> {
16891689
let mut fields = ThinVec::new();
16901690
let mut recovered = false;
@@ -1705,7 +1705,9 @@ impl<'a> Parser<'a> {
17051705
//
17061706
// Instead, the error should be thrown and handled by the caller
17071707
// `parse_anonymous_struct_or_union`.
1708-
if recover_anonymous_struct_or_union == RecoverAnonymousStructOrUnion::Yes {
1708+
if maybe_recover_anon_struct_or_union
1709+
== MaybeRecoverAnonStructOrUnion::Recover
1710+
{
17091711
return Err(err);
17101712
}
17111713
err.span_label(ident_span, format!("while parsing this {adt_ty}"));

0 commit comments

Comments
 (0)